RedirectFromLoginPage和FormsAuthenticationTicket的区别

如果你对.net身份验证不是很清晰,请看本文。本文用简单明了的语言,让你对RedirectFromLoginPage和FormsAuthenticationTicket有一个完整的认识。 1)FormsAuthentication.RedirectFromLoginPage(UserName.Text, mycheckbox.Checked)用于基于用户的验证 此方法封装了生成身份验证票,写回客户端,浏览器重定向等一系列的动作 RedirectFromLoginPage()方法首先生成生成身份验证票,然后调用FormAuthenticaiton.Encrypt() 方法,该方法将身份验证票加密为字符串,然后生成身份验证Cookie,再将此Cookie加入到Response.Cookies中,等待发送到客户端。最后RedirectFromLoginPage方法调用FormsAuthentication.GetRedirectUrl 方法获取到用户原先请求的页面,重定向到这个页面。 1、在浏览器上创建一个cookie,其中包含一个验证令牌。 2、返回刚才您所请求的页面; 相当于这两句: FormsAuthentication.SetAuthCookie(UserName.Text,mycheckbox.Checked); Response.Redirect(FormsAuthentication.GetRedirectUrl(UserName.Text,mycheckbox.Checked); 也就是说FormsAuthentication.RedirectFromLoginPage方法相当于一个封装的方法,简化了很多细节。

2)FormsAuthenticationTicket,用于基于角色的身份验证 上面的非基于角色的方法中,用了FormsAuthentication.RedirectFromLoginPage 方法来完成生成身份验证票,写回客户端,浏览器重定向等一系列的动作。这个方法会用一些确省的设置来完成一系列的动作,在基于角色的验证中我们不能用这一个方法来实现,要分步的做,以便将一些定制的设置加进来:

1. 首先要根据用户标示,和用户属于的角色的字符串来创建身份验证票 public FormsAuthenticationTicket( int version, //设为1 string name, //用户标示 DateTime issueDate, //Cookie 的发出时间, 设置为 DateTime.Now  DateTime expiration, //过期时间 bool isPersistent, //是否持久性(根据需要设置,若是设置为持久性,在发出 cookie时,cookie的Expires设置一定要设置) string userData, //这里用上面准备好的用逗号分割的role字符串 string cookiePath // 设为"/",这要同发出cookie的路径一致,因为刷新cookie 要用这个路径 );

FormsAuthenticationTicket Ticket = new FormsAuthenticationTicket (1,"kent",DateTime.Now, DateTime.Now.AddMinutes(30), false,UserRoles,"/") ;

2. 生成身份验证票的Cookie 2.1 将身份验证票加密序列化成一个字符串 string HashTicket = FormsAuthentication.Encrypt (Ticket) ; 2.2 生成cookie HttpCookie UserCookie = new HttpCookie(FormsAuthentication.FormsCookieName, HashTicket) ; FormsAuthentication.FormsCookieName 是用来获取web.config中设置的身份验证cookie的名字,缺省为" .ASPXAUTH". 若身份验证票中的isPersistent属性设置为持久类,则这个cookie的Expires属性一定要设置,这样这个cookie才会被做为持久cookie保存到客户端的cookie文件中. 3. 将身份验证票Cookie输出到客户端 通过Response.Cookies.Add(UserCookie) 将身份验证票Cookie附加到输出的cookie集合中,发送到客户端. 4. 重定向到用户申请的初试页面.

验证部分代码(这部分代码是在login.aspx页面上点击了登录按钮事件处理代码):

private void Buttonlogin_Click(object sender, System.EventArgs e) {      string user = TextBoxUser.Text; //读取用户名      string password = TextBoxPassword.Text; //读取密码      if(Confirm(user,password) == true) //confirm方法用来验证用户合法性的     {          string userRoles = UserToRole(user); //调用UserToRole方法来获取role字符串          FormsAuthenticationTicket Ticket = new FormsAuthenticationTicket (1,user,DateTime.Now,          DateTime.Now.AddMinutes(30), false,userRoles,"/") ; //建立身份验证票对象          string HashTicket = FormsAuthentication.Encrypt (Ticket) ; //加密序列化验证票为字符串          HttpCookie UserCookie = new HttpCookie(FormsAuthentication.FormsCookieName, HashTicket) ; //生成Cookie           Context.Response.Cookies.Add (UserCookie) ; //输出Cookie          Context.Response.Redirect (Context.Request["ReturnUrl"]) ; // 重定向到用户申请的初始页面      }     else     {         // 用户身份未被确认时的代码     } } //此方法用来验证用户合法性的 private bool Confirm(string user,string password) {     //相应的代码 } //此方法用来获得的用户对应的所有的role用逗号分割的一个字符串 private string UserToRole(string user) {     //相应的代码 }

3)总结 身份验证5步走:
1、创建身份验证票
2、加密身份验证票
3、生成Cookie
4、Cookie输出到客户端
5、页面重定向

(以上部分内容来源于网络)

原文地址:https://www.cnblogs.com/zhwl/p/3380841.html