Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>General advice about roles</strong></p> <p>The way I have done this is to create a custom principal and to store the extra required information in there. In your example, this would at least include the roles for the user. <em>That way you avoid making lots of extra trips to the user store (which is likely a SQL database).</em></p> <p>Have a look a this question of mine in which I give the code which I am using successfully: <a href="https://stackoverflow.com/questions/8263845/is-this-custom-principal-in-base-controller-asp-net-mvc-3-terribly-inefficient">Is this Custom Principal in Base Controller ASP.NET MVC 3 terribly inefficient?</a></p> <p>Note that I am storing the custom principal in the cache rather than in the session (just being paranoid about session hijacking). </p> <p>I like this approach as it is very extensible. For example, I have since extended this to expose Facebook credentials for when the user logs in via Facebook.</p> <p>Just remember that if you are caching data you need to remember to update it when it changes!</p> <p><strong>Answer to your question</strong></p> <p>Just to add, in your specific case, you should probably store this extra information in a <code>ViewModel</code> and then your view would say things like:</p> <pre><code>@if(ShowReports) { &lt;li id="ReportTab"&gt;@Html.ActionLink("Reports", "Index", "Reports")&lt;/li&gt; } @if(ShowDashboard) { &lt;li id="DashboardTab"&gt;@Html.ActionLink("Dashboard", "Dashboard", "Admin")&lt;/li&gt; } @if(ShowAdmin { &lt;li id="AdminTab"&gt;@Html.ActionLink("Admin", "Index", "Admin")&lt;/li&gt; } </code></pre> <p>with the ViewModel Code saying something like: </p> <pre><code>public bool ShowReports {get;set;} public bool ShowDashboard {get;set;} public bool ShowAdmin {get;set;} public void SetViewModel() { if (User.Identity.IsAuthenticated) { if (HttpContext.Current.User.IsInRole("Reporters")) { ShowReports = true; } if (HttpContext.Current.User.IsInRole("Administrators")) { ShowDashboard = true; ShowAdmin = true; } } } </code></pre> <p>I actually tend to take this one step further and create a <code>ReportsLink</code> in my <code>ViewModel</code> and set it to contain the link if the user is authorised or to be an empty string if they are not. Then the view just says:</p> <pre><code>@Model.ReportsLink @Model.DashboardLink @Model.AdminLink </code></pre> <p>In that case the pertinent part of the ViewModel might be like this:</p> <pre><code>ReportLink = new MvcHtmlString(HtmlHelper.GenerateLink(HttpContext.Current.Request.RequestContext, System.Web.Routing.RouteTable.Routes, "linktext", "routename", "actionname", "controllername", null, null)); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload