Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you restrict certain HTML elements to certain roles using ASP.NET MVC?

I'm looking for the standard practice in specifying that a certain HTML element, like a "Create user" button should only be displayed when the user is logged-in and belongs to the role "Administrator."

For example, using Spring MVC in Java, the Spring Security tag library has a control that does just that:

<sec:authorize access="hasRole('ROLE_PRESIDENT')">
    <input type="button" value="Launch nuclear weapons"/>
</sec:authorize>

Whatever appears between the tags will only display when the user belongs to the role specified.

Does ASP.NET MVC have such feature?

like image 478
Scifiballer24 Avatar asked Nov 22 '11 05:11

Scifiballer24


1 Answers

For Razor view engine:

@if (User.IsInRole("ROLE_PRESIDENT")) {
    <input type="button" value="launch nuclear weapons" />
}

For Webforms view engine:

<% if (User.IsInRole("ROLE_PRESIDENT")) { %>
    <input type="button" value="launch nuclear weapons" />
<% } %>
like image 172
Jon Avatar answered Nov 15 '22 06:11

Jon