登录时string userName = "zhang";
            string userRole = "admin";
            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, 
                userName, 
                DateTime.Now, 
                DateTime.Now.AddMinutes(30), 
                false, 
                userRole, 
                "/");
            string hashTicket = FormsAuthentication.Encrypt(ticket);            HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashTicket);
            Response.Cookies.Add(cookie);
Application_AuthorizeRequest(object sender, System.EventArgs e)时HttpApplication App = (HttpApplication)sender;
        HttpContext Ctx = App.Context; //获取本次Http请求相关的HttpContext对象
        if (Ctx.Request.IsAuthenticated == true) //验证过的用户才进行role的处理
        {
            FormsIdentity Id = (FormsIdentity)Ctx.User.Identity;
            FormsAuthenticationTicket Ticket = Id.Ticket; //取得身份验证票
            string[] Roles = Ticket.UserData.Split(','); //将身份验证票中的role数据转成字符串数组
            Ctx.User = new GenericPrincipal(Id, Roles); //将原有的Identity加上角色信息新建一个GenericPrincipal表示当前用户,这样当前用户就拥有了role信息     
        }Web.config<authentication mode="Forms" >
  <forms name="DemoAuth" loginUrl="Web/login.aspx" timeout="30" path="/" />
</authentication>
<authorization>
  <deny users="?"/>
</authorization><location path="Demo1">
  <system.web>
    <authorization>
      <allow roles="admin"/>
      <deny users="*"/>
    </authorization>
  </system.web>
</location>跳转时//在这里roleOk已经是true了,为什么还是跳不进去,郁闷!待解!急!
        bool roleOk = HttpContext.Current.User.IsInRole("admin");
        Response.Redirect("Demo1/demo1.aspx");
高手指教!是不是Web.config设置的不对?

解决方案 »

  1.   

    肯定是Web.config的问题    
    <allow roles="admin"/>
          <deny users="*"/>
      

  2.   

    roleOk无论是什么值都会跳转的,
    至于结果吗,就不好说了。。
      

  3.   


    我知道“roleOk无论是什么值都会跳转的”,加个bool roleOk = HttpContext.Current.User.IsInRole("admin")只是为了让看帖的人明白。我的意思是既然当前user的角色是admin,那为什么还进不了"Demo1/demo1.aspx"。如果谁知道是Web.config有问题,那请写出正确的,不要说句“web.config”有问题就走人!!
      

  4.   

    修改
    <authorization>
      <deny users="?"/>
    </authorization>为
    <authorization>
    <allow users="*"/>
    </authorization>
    参考