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>

  • 相关阅读:
    Java栈、堆、方法区
    Java冒泡算法
    JDK8 API下载
    java 对两个整数变量的值进行互换。三种方法
    Java中double相减精度的问题,和解决方法
    window.onload问题
    ES6中class详解
    YOLO V2
    YOLO V1
    ORB-SLAM2源码解析
  • 原文地址:https://www.cnblogs.com/zhuqun/p/12035020.html
Copyright © 2011-2022 走看看