问题描述
protectedvoidApplication_AuthenticateRequest(objectsender,EventArgse){HttpApplicationapp=(HttpApplication)sender;if(app.Request.IsAuthenticated){FormsIdentityidentity=(FormsIdentity)app.User.Identity;app.Context.User=newSystem.Security.Principal.GenericPrincipal(identity,newstring[]{"Admin","UpLoadFile"});//已运行到这里}}//登录后,我怎都测不到用户拥有Admin角色
解决方案
解决方案二:
protectedvoid_Application_AuthorizeRequest(objectsender,EventArgse)//Application_AuthenticateRequest(objectsender,EventArgse){stringcookieName=FormsAuthentication.FormsCookieName;HttpCookieauthCookie=Context.Request.Cookies[cookieName];if(null==authCookie){return;}FormsAuthenticationTicketauthTicket=null;try{authTicket=FormsAuthentication.Decrypt(authCookie.Value);}catch(Exceptionex){return;}if(null==authTicket){return;}FormsIdentityid=newFormsIdentity(authTicket);string[]roles=id.Ticket.UserData.Split('|');//读出在登录时设置的角色列表。BBLC.BLL.Role.s=cookieName;System.Security.Principal.GenericPrincipalprincipal=newSystem.Security.Principal.GenericPrincipal(id,roles);Context.User=principal;//将验证信息附加到当前用户上下文。}//这个是网上来的,也不行,是什么原因