Authorization of presentation sections in MVC

I was wondering if it is possible to allow parts of the view inside the view.

For example, I understand how to enable 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 kind of ID you like to do is if the administrator logs in, they can see additional links in my navigation.

Something along the lines

            <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 this will not work, but it gives an idea of ​​what I'm trying to accomplish.

Any ideas?

+3
source share
2 answers

Use something like this:

<% if(Roles.IsUserInRole("Administrator")){ %>
<span>HTML Code</span>
<% } %>
+4
source

It is best to send if stuff to a new html helper extension method.

0
source

Source: https://habr.com/ru/post/1738703/


All Articles