【问题标题】:Different HttpCookie Behavior on each server每个服务器上的不同 HttpCookie 行为
【发布时间】:2021-01-14 19:42:36
【问题描述】:

在我的应用程序中,一个按钮负责将用户重定向到登录页面,当它在 local server 上运行时,一切正常,在调用 Logout 方法后,它会生成一个空值 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 没有价值,它会重定向到登录页面

在 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();
}

但是当应用程序在remote server 上运行时,Logout 似乎不起作用,它只是重定向到根路径

可能是我这边的代码遗漏了什么?

你也可以看看我的浏览器cookies

顺便说一句,更换浏览器并没有任何区别

我不确定,但可能与每个域的 cookie 限制有关。

如您所见,在响应中 cookie 没有任何值,但是当您发送请求时,cook 值是注销前的值

在不同的浏览器或不同的帐户上尝试没有问题

【问题讨论】:

  • 没人有办法!
  • 偶尔会出现此问题,有时在两台服务器上都能正常工作。

标签: c# asp.net-mvc cookies httpcookie


【解决方案1】:

当代码按以下方式更改时,问题终于消失了:

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("~");

}

【讨论】:

    猜你喜欢
    • 2012-08-30
    • 1970-01-01
    • 2011-12-16
    • 1970-01-01
    • 2014-07-20
    • 1970-01-01
    • 2013-11-19
    • 2016-12-23
    • 1970-01-01
    相关资源
    最近更新 更多