【asp.net小札记】自定义文件夹访问权限
来源:互联网 发布:钱夫人淘宝店衣服批发 编辑:程序博客网 时间:2024/05/21 17:49
1、在用户登录时,添加以下函数:
private void WriteRoleToTicket(string username, string role) { //建立身份验证票对象 FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, username, DateTime.Now, DateTime.Now.AddMinutes(30), false, role, "/"); //加密序列化验证票为字符串 string hashTicket = FormsAuthentication.Encrypt(ticket); HttpCookie userCookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashTicket); //生成Cookie Response.Cookies.Add(userCookie); }
2、在global.asax中,添加事件:
void Application_AuthenticateRequest(object sender,EventArgs e) { // Extract the forms authentication cookie string cookieName = FormsAuthentication.FormsCookieName; HttpCookie authCookie = Context.Request.Cookies[cookieName]; if (null == authCookie) { // There is no authentication cookie. return; } FormsAuthenticationTicket authTicket = null; try { authTicket = FormsAuthentication.Decrypt(authCookie.Value); } catch (Exception ex) { // Log exception details (omitted for simplicity) return; } if (null == authTicket) { // Cookie failed to decrypt. return; } // When the ticket was created, the UserData property was assigned a // pipe delimited string of role names. string[] roles = authTicket.UserData.Split(new char[] { '|' }); // Create an Identity object FormsIdentity id = new FormsIdentity(authTicket); // This principal will flow throughout the request. System.Security.Principal.GenericPrincipal principal = new System.Security.Principal.GenericPrincipal(id, roles); // Attach the new principal object to the current HttpContext object Context.User = principal; }
3、在需要访问控制的文件夹中,添加web.config,添加节点,类似于以下内容:
<system.web> <authorization> <allow roles="专家"/> </authorization> </system.web>
0 0
- 【asp.net小札记】自定义文件夹访问权限
- 【asp.net 小札记】小知识点
- ASP.NET 访问文件夹权限不足,提升权限 /System.UnauthorizedAccessException 拒绝访问的处理方法
- 【asp.net小札记】发邮件
- 【asp.net小札记】实现进度条
- 【asp.net小札记】Repeater自定义添加排序按钮+显示图片
- ASP.NET 访问文件夹权限不足,System.UnauthorizedAccessException拒绝访问的处理方法
- ASP.NET访问权限管理
- ASP.NET访问权限管理
- Asp.net MVC自定义权限
- asp.net 下的访问文件或者文件夹无权限被拒绝
- asp.net项目中通过Web.config配置文件及文件夹的访问权限!
- asp.net项目中通过Web.config配置文件及文件夹的访问权限!
- 【asp.net小札记】jquery+ajax绑定数据库显示table
- 实战ASP.NET访问共享文件夹
- 如何解决xp文件夹权限,数据库只读,“ASP.NET 未被授权访问所请求的资源”
- [小技巧] virtualbox共享文件夹无访问权限问题解决方法
- asp.net(c#)访问Excel小例
- 网络流sap算法(whitecloud)
- KMP求周期uva10298
- 谈谈数据库中MyISAM与InnoDB区别
- 【Java图表绘制-JFreeChart】
- 做一个正气的杭电人 2500
- 【asp.net小札记】自定义文件夹访问权限
- TOJ 4620
- Windows 通过公钥访问Git Server
- 交换排序法
- myeclipse快捷工具
- Ubuntu 11.10 server 安装Git 服务器
- Floyd算法详讲
- Java 线程池的原理与实现
- Unity学习小结1 - 资源优化(1)