Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think I've implemented something similar.<br> My solution, based on <a href="http://nerddinner.codeplex.com/" rel="noreferrer">NerdDinner</a> <a href="http://nerddinnerbook.s3.amazonaws.com/Intro.htm" rel="noreferrer">tutorial</a>, is following.</p> <p><strong>When you sign the user in</strong>, add code like this:</p> <pre><code>var authTicket = new FormsAuthenticationTicket( 1, // version userName, // user name DateTime.Now, // created DateTime.Now.AddMinutes(20), // expires rememberMe, // persistent? "Moderator;Admin" // can be used to store roles ); string encryptedTicket = FormsAuthentication.Encrypt(authTicket); var authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket); HttpContext.Current.Response.Cookies.Add(authCookie); </code></pre> <p><strong>Add following code to <code>Global.asax.cs</code>:</strong></p> <pre><code>protected void Application_AuthenticateRequest(Object sender, EventArgs e) { HttpCookie authCookie = Context.Request.Cookies[FormsAuthentication.FormsCookieName]; if (authCookie == null || authCookie.Value == "") return; FormsAuthenticationTicket authTicket; try { authTicket = FormsAuthentication.Decrypt(authCookie.Value); } catch { return; } // retrieve roles from UserData string[] roles = authTicket.UserData.Split(';'); if (Context.User != null) Context.User = new GenericPrincipal(Context.User.Identity, roles); } </code></pre> <p>After you've done this, you can <strong>use <a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.authorizeattribute.aspx" rel="noreferrer"><code>[Authorize]</code></a> attribute in your controller action code:</strong></p> <pre><code>[Authorize(Roles="Admin")] public ActionResult AdminIndex () </code></pre> <p>Please let me know if you have further questions.</p>
    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.
    3. 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