Advertisement
Guest User

MVC Roles

a guest
Oct 6th, 2015
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.11 KB | None | 0 0
  1.         protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)
  2.         {            
  3.             if (FormsAuthentication.CookiesSupported == true)
  4.             {
  5.                 if (Request.Cookies[FormsAuthentication.FormsCookieName] != null)
  6.                 {
  7.                     try
  8.                     {
  9.                         string username = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name;
  10.                         string roles = String.Empty;
  11.  
  12.                         if (admins.Contains(username.ToLower()))
  13.                             roles += "admin;";
  14.  
  15.                         if (publishers.Contains(username.ToLower()))
  16.                             roles += "publisher;";
  17.  
  18.  
  19.                         HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(
  20.                           new System.Security.Principal.GenericIdentity(username, "Forms"), roles.Split(';'));
  21.                     }
  22.                     catch (Exception)
  23.                     {
  24.  
  25.                     }
  26.                 }
  27.             }
  28.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement