当前位置: 代码迷 >> ASP.NET >> 如何设置让一个页面只有管理员角色才以访问
  详细解决方案

如何设置让一个页面只有管理员角色才以访问

热度:3255   发布时间:2013-02-25 00:00:00.0
怎么设置让一个页面只有管理员角色才以访问?
我网站的其他页面是登录过才能访问,但是有一个usermanage.aspx,只能让拥有“admin”角色的用户访问。
我的web.config是这样配置的:
XML code
<system.web>    <authorization>      <deny users="?"/>    </authorization></system.web><location path="userManage.aspx">    <system.web>      <authorization>          <allow roles="admin"/>          <deny users="*"/>      </authorization>    </system.web>  </location>

但是这样只有用户名为admin的帐户才能访问usermanage.aspx,用户名为001但是拥有“admin”角色的帐户也不能访问,
求高手指教,错误在哪?应该怎么写?

------解决方案--------------------------------------------------------
在global.asa中加入以下代码:
C# code
    void Application_AuthenticateRequest(object sender, EventArgs e)    {        HttpContext ctx = (sender as HttpApplication).Context;        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 System.Security.Principal.GenericPrincipal(Id, Roles); //将原有的Identity加上角色信息新建一个GenericPrincipal表示当前用户,这样当前用户就拥有了role信息           }    }
  相关解决方案