Asp.Net基于forms的验证机制,记录一下...
来源:互联网 发布:java api手机版闪退 编辑:程序博客网 时间:2024/06/05 16:40
最近在看asp.net forum,对其中的验证机制看得模模糊糊,看完构建安全的 ASP.NET 应用程序中的表单身份验证部分,思路就很清晰了,稍做了点记录,以便查阅:
构建基于forms的验证机制过程如下:
1,设置IIS为可匿名访问和asp.net web.config中设置为form验证
2,检索数据存储验证用户,并检索角色(如果不是基于角色可不用)
3,使用FormsAuthenticationTicket创建一个Cookie并回发到客户端,并存储
角色到票中,如:
FormsAuthentication.SetAuthCookie(Username,true | false)
cookies保存时间:
HttpContext.Current.Response.Cookies[FormsAuthentication.FormsCookieName].Expires=DateTime.Now.AddDays(1)
如果需要存储角色,采用:
FormsAuthenticationTicket authTicket = new
FormsAuthenticationTicket(
1, // version
txtUserName.Text, // user name
DateTime.Now, // creation
DateTime.Now.AddMinutes(20),// Expiration
false, // Persistent
roles ); // User data
roles是一个角色字符串数组
string encryptedTicket = FormsAuthentication.Encrypt(authTicket); //加密
存入Cookie
HttpCookie authCookie =
new HttpCookie(FormsAuthentication.FormsCookieName,
encryptedTicket);
Response.Cookies.Add(authCookie);
4,在Application_AuthenticateRequest事件中处理程序中(Global.asax)中,使用
票创建IPrincipal对象并存在HttpContext.User中
代码:
HttpCookie authCookie = Context.Request.Cookies[FormsAuthentication.FormsCookieName];
FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);//解密
string[] roles = authTicket.UserData.Split(new char[]{';'});//根据存入时的格式分解,;或|....
Context.User = new GenericPrincipal(Context.User.Identity, Roles);//存在HttpContext.User中
5,需要对某些页面进行角色控制,有两种方法:
5.1,web.config中加
<location path="EditPost.aspx">
<system.web>
<authorization>
<allow roles="RoleName" />
<deny users="?" />
</authorization>
</system.web>
</location>
5.2,把只能是某种角色访问的文件放在同一目录下,在此目录下添加一个web.config
<configuration>
<system.web>
<authorization>
<allow roles="RoleName" />
<deny users="*" />
</authorization>
</system.web>
</configuration>
说明:子目录的web.config设置优先于父目录的web.config设置
这里有一篇更全面的总结
- Asp.Net基于forms的验证机制,记录一下...
- Asp.Net基于forms的验证机制,记录一下...
- Asp.Net基于forms的验证机制
- Asp.Net基于forms的验证机制
- Asp.Net基于forms的验证机制
- Asp.Net基于forms的验证机制
- asp.net FormsAuthenticationTicket基于forms的验证
- Asp.net中基于Forms验证的角色验证授权
- Asp.net中基于Forms验证的角色验证授权
- Asp.net中基于Forms验证的角色验证授权
- Asp.net中基于Forms验证的角色验证授权
- Asp.net中基于Forms验证的角色验证授权
- Asp.net中基于Forms验证的角色验证授权
- Asp.net中基于Forms验证的角色验证授权
- Asp.net中基于Forms验证的角色验证授权
- Asp.net中基于Forms验证的角色验证授权
- Asp.net中基于Forms验证的角色验证授权
- Asp.net中基于Forms验证的角色验证授权
- 汉诺塔的汇编源代码
- cdo发送图片邮件
- DELPHI面向对象支持特点--保护级类成员的应用
- ...
- RecapQuiz of Software Engineering Foundation-PartⅠ
- Asp.Net基于forms的验证机制,记录一下...
- RecapQuiz of Software Engineering Foundation-PartⅡ
- 系统级C语言程序设计(中断原理简介)
- If "Super Girl" were run by CCTV...
- test
- 熊庆来的故事
- 苏步青的故事
- 数学之神──阿基米德
- Ant语法元素