zoukankan      html  css  js  c++  java
  • web版本的用户登陆票据 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>

  • 相关阅读:
    二、一切都是对象
    一、对象导论
    CSS 属性大全
    CSS颜色代码大全
    CSS Position 定位属性
    CSS Box Model 盒子模型
    ThreadLocal
    Java 模拟死锁
    byte 最小值为啥是最小是 -128 ;int最小值为啥是 -2147483648
    cmd 查看端口号占用情况
  • 原文地址:https://www.cnblogs.com/zhuqun/p/12035020.html
Copyright © 2011-2022 走看看