zoukankan      html  css  js  c++  java
  • FormsAuthenticationTicket学习笔记

    自己的代码,很乱给自己看 

                FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, "username", DateTime.Now, DateTime.Now.AddDays(365),
                 true, string.Format("{0}:{1}", "username", "password"), FormsAuthentication.FormsCookiePath);
                //这边的 string.Format("{0}:{1}", "username", "password"),也可以改为任何值,如密码或IP
                string ticString = FormsAuthentication.Encrypt(ticket);
                //将加密后的票据保存为cookie 
                HttpCookie coo = new HttpCookie(FormsAuthentication.FormsCookieName, ticString);
                //这里的IsPersistent不会自己判断,需要手工判断,但FormsAuthenticationTicket的expiration的值是有过期性的,无论cookie的Expires设置多久,只要expiration到期,即使cookie存在用户验证也将失败
                if (ticket.IsPersistent)
                {
                    coo.Expires = ticket.Expiration;
                }
                //使用加入了userdata的新cookie 
                Response.Cookies.Add(coo);
                //FormsAuthentication.SetAuthCookie("username", true);//这种是快速写法,用了这种写法就可以不用自己建ticket票据,也无法使用UserData等参数了
    
    
                //FormsAuthentication.SignOut//用来清除这个Cookie标记//FormsAuthentication.RedirectFromLoginPage(userID, createPersistentCookie);
    
            //    <authentication mode="Forms">
            //    <forms name=".MyCookie" loginUrl="Login.aspx" protection="All" timeout="60"/>
            //    </authentication> 

    总结:

    1.FormsAuthentication一共有两种生成并记录票具的方法:

      一种是自己new FormsAuthenticationTicket,然后自己保存到Cookie中,ticket的IsPersistent属性仅起到标识的作用,并不会去修改Cookies的到期时间,需要人工自己判断后设置cookies的到期时间(if (ticket.IsPersistent){coo.Expires = ticket.Expiration; })。

      另一种是使用FormsAuthentication.SetAuthCookie("username",IsPersistent: false);来快捷生成ticket,这种方法系统会自动新建一个cookies来保存ticket,并根据IsPersistent是否为true,以及webconfig中的timeout来确定cookies的到期时间。

    2.如果使用第一种方法,无论自己把cookies的时间设置为多久,只要ticket.Expiration到期了,及时cookies还存在,用户验证依然会失败。

    3.UserData是个好东西,可以用来存储IP地址,用于判断访问者是否是恶意截取cookies。

    推荐一篇好文:http://www.cnblogs.com/zxjyuan/archive/2009/08/21/1551196.html

  • 相关阅读:
    创造别人想要的东西(2)----创业15问
    创造别人想要的东西(1)----创业的本质
    有点迷茫
    普林斯顿大学算法公开课(1)----介绍
    工业控制系统信息安全与信息安全、功能安全的区别【转自工控工程网】
    十条概念区分信息安全与隐私保护【转自安全牛】
    NBT(NetBIOS Over TCP)名称解析概述
    SMB协议概述
    PCI DSS合规建设ASV扫描介绍
    信息安全等级测评师(初级 技术)
  • 原文地址:https://www.cnblogs.com/yeagen/p/3427465.html
Copyright © 2011-2022 走看看