完全相同的代码,在小组的两台电脑上,分别用Chrome和Microsoft Edge测试,用Visual Studio的调试和非调试模式分别运行,然后分别用不同的账号登录,会出现两个不同Value的Cookies,并且这两个Cookies的名称(.ASPXAUTH)、PATH、Domain都是一样的,只是值不同。但是用Microsoft IE浏览器,则不会出现这个问题。Visual
Studio版本都是2019
还是与上述环境、代码完全一样,在小组其他的电脑上,则不会有任何问题,浏览器用Chrome、Microsoft Edge,在调试和非调试模式都是保持同一个Cookies值,同步登录、注销。
HttpCookie authCookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
if (authCookie != null)
{
authCookie = null;
HttpContext.Current.Response.Cookies[FormsAuthentication.FormsCookieName].Value = null;
HttpContext.Current.Response.Cookies[FormsAuthentication.FormsCookieName].Expires =
System.DateTime.Now.AddMonths(-1); //设置上个月
}
//创建票据
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, Uname, DateTime.Now, DateTime.Now.AddMonths(1), true, "");
//加密票据
string newticket = FormsAuthentication.Encrypt(ticket);
//创建cookie
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, newticket);
cookie.Expires = DateTime.Now.AddMonths(1);
cookie.HttpOnly = true;
HttpContext.Current.Response.Cookies.Add(cookie);