<location path ="admin">
    <system.web >
      <authorization >
        <allow roles ="admin"/>
        <deny users ="*"/>
      </authorization>
    </system.web>
  </location>
中的角色不起作用?

解决方案 »

  1.   

    <location path="允许匿名访问的文件夹名称或者文件路径">
      <system.web>
      <authorization>
      <allow users="?"/>
      </authorization>
      </system.web>
      </location><location path="users">   
    <system.web>   
    <authorization>   
    <allow roles="User"/>   
    <deny users="*"/>   
    </authorization>   
    </system.web>   
    </location>   
      

  2.   

    为什么我的设置起不了效果?<deny users ="*"/>这个起作用。 <allow roles ="admin"/>
    这个不起作用。我的当前用户设置的权限为:
     FormsAuthenticationTicket ticket =
                        new FormsAuthenticationTicket(1,
                                                       "ad",
                                                       DateTime.Now,
                                                       DateTime.Now.AddMinutes(1),
                                                       false,
                                                       "admin,user");
                string emTicket = FormsAuthentication.Encrypt(ticket);
                HttpCookie UserCookie = new HttpCookie(FormsAuthentication.FormsCookieName, emTicket);
                Response.Cookies.Add(UserCookie);
                Context.Response.Redirect(FormsAuthentication.GetRedirectUrl(this.TextBox1.Text, false));
            } 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信息
                       }这个文件中能得到Roles 可是就是不起作用
      

  3.   

    必须定义一个类,继承RoleProvider,并且实现public override string[] GetRolesForUser(string username)
        {
            FormsIdentity Id = HttpContext.Current.User.Identity as FormsIdentity;
            if (Id != null)
            {
                return Id.Ticket.UserData.Split(new Char[] { ',' });
            }
            return null;    }
    这样就可以了。