protected void Application_PostAuthenticateRequest(Object sender, EventArgs e) { if (FormsAuthentication.CookiesSupported != true) return; if (Request.Cookies[FormsAuthentication.FormsCookieName] == null) return; //let us take out the username now var formsAuthenticationTicket = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value); if (formsAuthenticationTicket == null) return; var username = formsAuthenticationTicket.Name; var roles = String.Empty; using (IDataContextAsync context = new OIDataContext()) using (IUnitOfWorkAsync unitOfWork = new UnitOfWork(context)) { IRepositoryAsync<Account> accountRepository = new Repository<Account>(context, unitOfWork); var user = accountRepository.Query(u => u.Username == username).Include(r => r.Role).Select().SingleOrDefault(); if (user != null) roles = user.Role.RoleType; } //Let us set the Pricipal with our user specific details HttpContext.Current.User = new System.Security.Principal.GenericPrincipal( new System.Security.Principal.GenericIdentity(username, "Forms"), roles.Split(';')); }
protected void Application_PostAuthenticateRequest(Object sender, EventArgs e) { if (FormsAuthentication.CookiesSupported != true) { return; } if (Request.Cookies[FormsAuthentication.FormsCookieName] == null) { return; } //let us take out the username now var formsAuthenticationTicket = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value); if (formsAuthenticationTicket == null) { return; } var username = formsAuthenticationTicket.Name; var roles = String.Empty; using (IDataContextAsync context = new OIDataContext()) using (IUnitOfWorkAsync unitOfWork = new UnitOfWork(context)) { IRepositoryAsync <Account> accountRepository = new Repository <Account>(context, unitOfWork); var user = accountRepository.Query(u => u.Username == username).Include(r => r.Role).Select().SingleOrDefault(); if (user != null) { roles = user.Role.RoleType; } } //Let us set the Pricipal with our user specific details HttpContext.Current.User = new System.Security.Principal.GenericPrincipal( new System.Security.Principal.GenericIdentity(username, "Forms"), roles.Split(';')); }