Authorizing sections of a view in MVC

Posted by Duk on Stack Overflow See other posts from Stack Overflow or by Duk
Published on 2010-03-26T18:24:40Z Indexed on 2010/03/26 18:33 UTC
Read the original article Hit count: 480

I was wondering if it's possible to authorize parts of a view inside the view.

For example, I understand how to authorize the entire controller in this method

<HandleError()> _
Public Class HomeController
  Inherits System.Web.Mvc.Controller

  Function Index()
    Return View()
  End Function

  <Authorize(Roles:="Administrators")> _
  Function AdministratorSecrets()
    Return View()
  End Function

End Class

But what Id like to do is have it so if the admin is logged in, they can see additional links in my navigation.

Something along the lines of

            <ul id="menu">              
                <li><%= Html.ActionLink("Home", "Index", "Home")%></li>
                <li><%= Html.ActionLink("About", "About", "Home")%></li>
                <Authorize(Roles:="Administrators")> _
                <li><%= Html.ActionLink("Admin", "Admin", "Home")%></li>
            </ul>

Obviously that won't work, but it gives an idea of what I'm trying to accomplish.

Any ideas?

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about mvc