当前位置:编程学习 > C#/ASP.NET >>

是哪种原因让我没法把角色附加到登录用户上???

 
    protected void Application_AuthenticateRequest(object sender, EventArgs e)
    {
        HttpApplication app = (HttpApplication)sender;
        if (app.Request.IsAuthenticated)
        {
            FormsIdentity identity = (FormsIdentity)app.User.Identity;
           
           
                app.Context.User = new System.Security.Principal.GenericPrincipal(identity, new string[] {"Admin","UpLoadFile"});
              
          //已运行到这里        }
    }

//登录后,我怎都测不到用户拥有Admin角色

--------------------编程问答-------------------- protected void _Application_AuthorizeRequest(object sender,EventArgs e)// Application_AuthenticateRequest(object sender, EventArgs e)
    {
  string cookieName = FormsAuthentication.FormsCookieName;
        HttpCookie authCookie = Context.Request.Cookies[cookieName];
        if (null == authCookie)
        {
            return;
        }
        FormsAuthenticationTicket authTicket = null;
        try
        {
            authTicket = FormsAuthentication.Decrypt(authCookie.Value);
        }
        catch (Exception ex)
        {
            return;
        }
        if (null == authTicket)
        {
            return;
        }

        FormsIdentity id = new FormsIdentity(authTicket);
       
        string[] roles = id.Ticket.UserData.Split('|'); //读出在登录时设置的角色列表。
        BBLC.BLL.Role.s = cookieName;
        System.Security.Principal.GenericPrincipal principal = new System.Security.Principal.GenericPrincipal(id, roles);


         Context.User = principal;//将验证信息附加到当前用户上下文。


        

        
    }
//这个是网上来的,也不行,是什么原因
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,