FormsCookieName保存登录用户名的使用


 

一,写一个类来实现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;

/// <summary>
///FormsCookieNameTest 的摘要说明
/// </summary>
public class FormsCookieNameTest
{
    /// <summary>
    /// FormsAuthenticationTicket
    /// </summary>
    /// <param name="uname"></param>
    public bool Login(string name)
    {
        if (!string.IsNullOrEmpty(name))
        {
            //FormsAuthentication.SetAuthCookie(uname,true);
            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket
                (1,
                    name,
                    DateTime.Now,
                    DateTime.Now.AddMinutes(20),
                    true,
                    "456",
                    "/"
                );
            var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket));
            cookie.HttpOnly = true;
            HttpContext.Current.Response.Cookies.Add(cookie);
            return true;
        }
        return false;
    }
}

 

 

二,C#中的FormsAuthenticationTicket解析

  //
        // 摘要:
        //     使用 cookie 名、版本、目录路径、发布日期、过期日期、持久性以及用户定义的数据初始化 System.Web.Security.FormsAuthenticationTicket
        //     类的新实例。
        //
        // 参数:
        //   version:
        //     票证的版本号。
        //
        //   name:
        //     与票证关联的用户名。
        //
        //   issueDate:
        //     票证发出时的本地日期和时间。
        //
        //   expiration:
        //     票证过期时的本地日期和时间。
        //
        //   isPersistent:
        //     如果票证将存储在持久性 Cookie 中(跨浏览器会话保存),则为 true;否则为 false。如果该票证存储在 URL 中,将忽略此值。
        //
        //   userData:
        //     存储在票证中的用户特定的数据。
        //
        //   cookiePath:
        //     票证存储在 Cookie 中时的路径。
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        public FormsAuthenticationTicket(int version, string name, DateTime issueDate, DateTime expiration, bool isPersistent, string userData, string cookiePusing System;

 

三,一个登陆保存cookie和删除cookie

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

public partial class FormsCookieName : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        FormsCookieNameTest  t= new FormsCookieNameTest();
        if (t.Login(TextBox1.Text))
        {
            Response.Redirect("~/FormsCookieName1.aspx");
        }
    }
}

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

public partial class FormsCookieName1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        //当在页面FormsCookieName2删除cookie,则这里则为空,就是退出登录时
        var cookie = Request.Cookies[FormsAuthentication.FormsCookieName];
        if (cookie != null)
        {
            var ticket = FormsAuthentication.Decrypt(cookie.Value);
            string role = ticket.UserData;
            TextBox1.Text = ticket.Name;
        }
        else {
            TextBox3.Text = "cookie删除成功";
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/FormsCookieName2.aspx");
    }
}

 

 protected void Page_Load(object sender, EventArgs e)
    {
        //删除
        Response.Cookies[FormsAuthentication.FormsCookieName].Expires = DateTime.Now.AddMinutes(-1);
        CookieExtensions.DeleteCookie(FormsAuthentication.FormsCookieName);
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/FormsCookieName1.aspx");
    }

 

 

 

这两个页面要注意的是:获取ticket.Name写在和你生成cookie的同一个页面的时候,会出现你获取的ticket.Name的值为上一个cookie的ticket.Name的值。

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM