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>

  • 相关阅读:
    JQuery高级
    Filter&Listener
    关于用户信息的一个综合案例
    JSP,EL和JSTL
    Cookie和Session
    Request和Response
    Servlet和HTTP请求协议
    Tomcat
    xml
    数组模拟堆
  • 原文地址:https://www.cnblogs.com/zhuqun/p/12035020.html
Copyright © 2011-2022 走看看