码迷,mamicode.com
首页 > 其他好文 > 详细

权限管理

时间:2015-09-07 10:50:41      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:

我要将一个文件夹只能让一个用户组访问怎么办?

可否在网站根目录下的web.config里这样设置:

<location path="admin">

    <system.web>

      <authorization>

        <allow roles="adminer">

        </allow>

        <deny users="*">

        </deny>

      </authorization>

    </system.web>

  </location>

(注:最后经过测试,这样是可以的,这里只是为了说明web.config是分层,可覆盖的)

最后我想到,在每个文件夹下加一个web.config重写

<authorization/>部分

如:

a文件夹下web.config:

<?xml version="1.0" encoding="utf-8" ?>

<configuration> 

  <system.web>

      <authorization>

            <allow  roles = "admin"/>

            <deny   user  = "?"/>

    </authorization>

 </system.web>

</configuration>

 

b文件夹下的web.congfig

<?xml version="1.0" encoding="utf-8" ?>

<configuration> 

  <system.web>

      <authorization>

            <allow  roles="teacher"/>

              <deny users = "?" roles = "student,admin"/>

    </authorization>

 </system.web>

</configuration>

这样就对不同的文件夹实现不同的授权了

 

基于角色的验证终于完成了

如下实现方法:

1.在web.config里这样设置:

<system.web>

       <authorization>

              <allow = “roleslist”

              <deny users = “?”/>

       </authorization>

</system.webconfig>

登陆成功以后生成一个票据,票据里存储有用户的用户名和角色等信息,将票据发送到客户端,并跳转到请求的页面。

如:

//如果通过验证

if (IsPass)

{

     FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,studentinfo.userid,DateTime.Now,DateTime.Now.AddMinutes(30),false,studentinfo.role,"/");

     string CodeTicket = FormsAuthentication.Encrypt(ticket);

     HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName,CodeTicket);

     Context.Response.Cookies.Add(cookie);

     Context.Response.Redirect(RedirectTarget);

}

 

2.在Gloabal.asa.cs里的Application_AuthenticateRequest中获取客户端的cookie中的role信息,并生成GenericPrincipal对象保存在Application.Conext.User里

如:

protected void Application_AuthenticateRequest(Object sender, 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信息

     }

}

 

数据库设计图:

技术分享

 


原文参考:.net用户角色与访问权限控制

     大型门户网站的RBAC用户权限管理

      

权限管理

标签:

原文地址:http://www.cnblogs.com/qtxy/p/4788095.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!