C# 每台服务器上的不同HttpCookie行为
在我的应用程序上,有一个按钮负责将用户重定向到登录页面,当它在C# 每台服务器上的不同HttpCookie行为,c#,asp.net-mvc,cookies,httpcookie,C#,Asp.net Mvc,Cookies,Httpcookie,在我的应用程序上,有一个按钮负责将用户重定向到登录页面,当它在本地服务器上运行时,一切正常,调用注销方法后,它生成一个空值cookie并将其放在响应上: public ActionResult Logout() { HttpContext context = HttpContext.Current; context.Session.Abandon(); HttpCookie authenticationCookie = context.Request.Cookies[Fo
本地服务器上运行时,一切正常,调用注销
方法后,它生成一个空值cookie并将其放在响应上:
public ActionResult Logout()
{
HttpContext context = HttpContext.Current;
context.Session.Abandon();
HttpCookie authenticationCookie = context.Request.Cookies[FormsAuthentication.FormsCookieName];
if (authenticationCookie == null)
authenticationCookie = new HttpCookie(FormsAuthentication.FormsCookieName);
authenticationCookie.Value = null;
authenticationCookie.Secure = true;
authenticationCookie.HttpOnly = true;
authenticationCookie.Expires = DateTime.Now.AddDays(-1);
context.Response.Cookies.Add(authenticationCookie);
return Redirect("~");
}
现在,由于cookie没有值,它将重定向到登录页面
protected void Application_EndRequest(object sender, EventArgs e)
{
SUser.RedirectSsoAuthentication();
}
在global.asx中检查身份验证信息
protected void FormsAuthentication_OnAuthenticate(object sender, FormsAuthenticationEventArgs args)
{
SUser user = new SUser();
user.ValidateAuthentication(args);
}
现在它重定向到登录页面
protected void Application_EndRequest(object sender, EventArgs e)
{
SUser.RedirectSsoAuthentication();
}
但当应用程序在远程服务器上运行时,注销似乎不起作用,它只是重定向到根路径
可能是我这边的密码漏掉了什么
您还可以查看我的浏览器cookies
顺便说一句,改变浏览器并没有什么不同
我不确定,但它可能与每个域的cookie限制有关
正如您所看到的,响应cookie并没有值,但当您发送请求时,cookie值和注销前的值相同
在不同的浏览器或不同的帐户上试用时没有问题 最后,当代码按以下方式更改时,问题消失了:
public ActionResult Logout()
{
var cookies = HttpContext.Current.Response.Cookies;
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, "");
cookie.HttpOnly = true;
cookie.Secure =true;
cookie.Domain = "";
cookies.Add(cookie);
HttpContext.Current.Session.Abandon();
return Redirect("~");
}
没有人有解决办法!此问题偶尔出现,有时在两台服务器上都可以正常工作。