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的值。

 

转载于:https://www.cnblogs.com/May-day/p/5880742.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值