zoukankan      html  css  js  c++  java
  • asp.net 登陆验证 Form表单验证的3种方式 FormsAuthentication.SetAuthCookie;FormsAuthentication.RedirectFromLoginPage;FormsAuthenticationTicket

    我们在登陆成功后,使用下面的3种方法,都是同一个目的:创建身份验证票并将其附加到 Cookie,

    当我们用Forms认证方式的时候,可以使用HttpContext.Current.User.Identity.IsAuthenticated  (或者也可以用 Request.IsAuthenticated ,这个实际上也是调用的是User.Identity.IsAuthenticated来验证)来判断是否登陆;而这个判断就是依赖于这个Cookie里的信息判断用户是否登陆。 
    FormsAuthentication.SignOut用来清除这个Cookie标记

    Form身份认证依赖Cookie,Asp.net就是每次检查我们在配置文件中指定的Cookie名称,并解密这个Cookie来判断当前请求用户的登录状态

    使用下面3种方法的前提是在web.config里面设置为表单验证

    <authentication mode="Forms">
                <forms name=".MyCookie" loginUrl="Login.aspx" protection="All" timeout="60"/>
            </authentication> 
    

    1:FormsAuthentication.SetAuthCookie

    演示:

    FormsAuthentication.SetAuthCookie(UserInfo.UserName, false, FormsAuthentication.FormsCookiePath);
    

    2:FormsAuthenticationTicket

    ////创建身份验证票
       FormsAuthenticationTicket AuTicket =new FormsAuthenticationTicket(
     1, UserInfo.UserName, DateTime.Now, DateTime.Now.AddMinutes(30),
     false, Request.UserHostAddress);
     ////将票据加密
      string authTicket = FormsAuthentication.Encrypt(AuTicket);
     ////将加密后的票据保存为cookie 
     HttpCookie coo =new HttpCookie(FormsAuthentication.FormsCookieName, authTicket);
     coo.Secure =false;
     coo.Expires = AuTicket.Expiration;
     coo.Path = FormsAuthentication.FormsCookiePath;
     ////加入新cookie 
     Response.Cookies.Add(coo);
    

    3:FormsAuthentication.RedirectFromLoginPage

    FormsAuthentication.RedirectFromLoginPage(UserInfo.UserName, false);
    

    注释: 

    名称说明
    FormsAuthentication.RedirectFromLoginPage (String, Boolean) 将经过身份验证的用户重定向回最初请求的 URL 或默认 URL。
    FormsAuthentication.RedirectFromLoginPage (String, Boolean, String) 使用 Forms 身份验证 Cookie 的指定 Cookie 路径,将经过身份验证的用户重定向回最初请求的 URL 或默认 URL。

    FormsAuthentication.RedirectFromLoginPage的第二个参数,true表示保留持久cookie,过期时间就是web.config里的时间,如果是false则关闭浏览器就过期。

    这一行代码实现你在填写登录名和密码后,成功就转到原先你想到的页面。

    这后面的参数“false”说明是否永久保留cookie。True则表示永久保留,下次访问就不用输入密码了,否则断开本次链接后,下次还需要输入密码。这次参数也可以由用户选择,因为考虑到安全性,可以在用户名或密码的傍边放个checkbox,原来的语句可以为:

    System.Web.Security.FormsAuthentication.RedirectFromLoginPage(this.txtname.Text,this.CheckBox.Checked);

    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、页面重定向

      

      

      

      

  • 相关阅读:
    TCP协议-如何保证传输可靠性
    计算机网络基础(未完待续)
    计算机理论基础
    计算机网络
    操作系统与计算机网络
    Linux系统中的vi/vim指令【详解】
    Linux面试笔试题带答案【详解】
    关于梦想(五)
    Jmeter的安装教程【图文】
    关于梦想(四)
  • 原文地址:https://www.cnblogs.com/YanYongSong/p/5044450.html
Copyright © 2011-2022 走看看