Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As long as you are using the standard authentication method that places the user object in the context it shouldn't be too hard. In your view, use Html.RenderPartial and the appropriate controller to accomplish what you want like this...</p> <p>/Home/Index View:</p> <pre><code>&lt;div id="menu"&gt; &lt;ul&gt; &lt;li&gt;Page 1&lt;/li&gt; &lt;li&gt;Page 2&lt;/li&gt; &lt;% Html.RenderPartial("/Home/AdminView"); %&gt; &lt;/ul&gt; &lt;/div&gt; </code></pre> <p>/Home/Admin View:</p> <pre><code>&lt;li&gt;Admin 1&lt;/li&gt; &lt;li&gt;Admin 2&lt;/li&gt; </code></pre> <p>/Shared/Empty View:</p> <pre><code>&lt;!--Empty view for non-viewable admin stuff--&gt; </code></pre> <p>/Home Controller:</p> <pre><code>public ActionResult AdminView() { //Check if user is admin //This should be your own logic... I usually have a few static methods that the //Users or User object has to assist with checking if (Models.User.IsAdmin(HttpContext.User.Identity.Name)) { return View(); } else { return View("Empty"); } } </code></pre> <p>That should work... It's how I do it. If it's wrong, hopefully someone smarter posts a better answer so we can both learn</p> <p><strong>EDIT:</strong></p> <p>Just thought of something else... If your user object implements IPrincipal, you could extract it from the context, cast it to your user type and have the information right in the user class...</p> <p>Kind of like this</p> <pre><code>class User : IPrincipal { //Implement IPrincipal stuff public string Role { get; set; } } </code></pre> <p>Then the admin view logic could look like this:</p> <pre><code>public ActionResult AdminView() { //Check if user is admin //This should be your own logic... I usually have a few static methods that the //Users or User object has to assist with checking if ( ((Model.User)HttpContext.User).Role =="Admin" ) { return View(); } else { return View("Empty"); } } </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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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