c# - 更改 CookieDomain 后 FormsAuthentication.SignOut() 不工作

标签 c# forms-authentication

在 web.config 中,我们有以下内容:

<authentication mode="Forms">
  <forms loginUrl="~/login" timeout="43200" slidingExpiration="true" name=".PX" />
</authentication>

我们已将其更新为:

<authentication mode="Forms">
  <forms loginUrl="~/login" timeout="43200" slidingExpiration="true" name=".PX" enableCrossAppRedirects="true" domain="[websitename].com" />
</authentication>

问题是,当我们调用 FormsAuthentication.SignOut() 时,已经登录 的用户不再注销。

我现在不只是调用 FormsAuthentication.SignOut(),而是执行以下操作,但它仍然没有注销当前登录的用户:

private static void SignOut(HttpContextBase context)
{
    RemoveCookie(context, FormsAuthentication.FormsCookieName, FormsAuthentication.FormsCookiePath, FormsAuthentication.CookieDomain, true);
    RemoveCookie(context, FormsAuthentication.FormsCookieName, FormsAuthentication.FormsCookiePath, FormsAuthentication.CookieDomain, false);
    RemoveCookie(context, FormsAuthentication.FormsCookieName, FormsAuthentication.FormsCookiePath, null, true);
    RemoveCookie(context, FormsAuthentication.FormsCookieName, FormsAuthentication.FormsCookiePath, null, false);

    // clear cookies server side
    context.Request.Cookies.Clear();

    context.Session.Abandon();
    FormsAuthentication.SignOut();
}

private static void RemoveCookie(HttpContextBase context, string name, string path, string domain, bool httpOnly)
{
    context.Response.Cookies.Add(new HttpCookie(name, "NoCookie")
    {
        Path = path,
        Domain = domain,
        Secure = false,
        Shareable = false,
        HttpOnly = httpOnly,
        Expires = DateTime.Now.AddDays(-1d)
    });
}

最佳答案

FormsAuthentication.SignOut() 中有一个调用删除响应中所有以前的 cookie:context.Response.Cookies.RemoveCookie(FormsCookieName ); ( https://github.com/Microsoft/referencesource/blob/master/System.Web/Security/FormsAuthentication.cs#L421 )

更改所有内容的顺序似乎可以解决问题:

private static void SignOut(HttpContextBase context)
{
    context.Session.Abandon();
    FormsAuthentication.SignOut();

    RemoveCookie(context, FormsAuthentication.FormsCookieName, FormsAuthentication.FormsCookiePath, FormsAuthentication.CookieDomain, true);
    RemoveCookie(context, FormsAuthentication.FormsCookieName, FormsAuthentication.FormsCookiePath, FormsAuthentication.CookieDomain, false);
    RemoveCookie(context, FormsAuthentication.FormsCookieName, FormsAuthentication.FormsCookiePath, null, true);
    RemoveCookie(context, FormsAuthentication.FormsCookieName, FormsAuthentication.FormsCookiePath, null, false);

    // clear cookies server side
    context.Request.Cookies.Clear();
}

关于c# - 更改 CookieDomain 后 FormsAuthentication.SignOut() 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53197387/

相关文章:

java - 数据库用户的 JSF2 表单例份验证

c# - HttpPost 在 C# 中不工作,但在 Android 中工作正常

C# 多屏幕 View 单一窗体

c# - 查询相同数据上下文时得到不同结果

javascript - Web 服务器和客户端 cookie 上的应用程序池回收

c# - 表单例份验证 - 需要 SSL - 设置身份验证 Cookie 失败

c# - ASP.NET 2.0 站点中的 ASP.NET 4.0 应用程序能否使用相同的表单例份验证 cookie?

asp.net - 如何从单独的 .NET 应用程序获取当前上下文或用户身份?

c# - 使用 Microsoft 的 ILogger 时如何将范围包含在 Serilog 的控制台接收器中?

c# - 如何使用 CaSTLe Windsor 和 MS Test 将依赖项传递给对象?