gpt4 book ai didi

c# - ASP.NET Identity 2 - 在 foo.com 上登录并在 username.foo.com 上注销时注销不起作用

转载 作者:行者123 更新时间:2023-11-30 21:34:56 25 4
gpt4 key购买 nike

我正在开发一个 Multi-Tenancy Web 应用程序,主要使用 .NET Framework 4.6(MVC 和 ASP.NET Identity 2.0)。这是我的实现:

用户访问 foo.com 进行登录。我在 foo.com Startup.Auth.cs 中使用以下代码:

var cookieOptions = new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/account/login"),
CookieDomain = ".foo.com"
};
app.UseCookieAuthentication(cookieOptions);

两个应用程序(foo.com 和 username.foo.com)上的机器 key 完全相同,这是我的示例机器 key :

<machineKey validationKey="xx" decryptionKey="xx" validation="SHA1" decryption="AES" />

要登录,我使用以下代码:

signInManager.SignIn(user, isPersistent: false, rememberBrowser: false);

我的另一个应用程序 username.abc.com 是 Multi-Tenancy 的,即用户名可以是任何东西。我在 username.abc.com Startup.Auth.cs 中使用以下代码:

var cookieOptions = new CookieAuthenticationOptions
{
ReturnUrlParameter = "redirectto",
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/account/login")
};
app.UseCookieAuthentication(cookieOptions);

注意,我没有使用 cookies 域,因为它可以是任何东西,或者用户可能已经开始使用他自己的域(假设用户仍在使用 foo.com 子域)。

使用此代码,用户成功登录并重定向到他的 username.foo.com,但是一旦他在 username.foo.com 上单击注销,页面就会重新加载,但没有任何反应。这是我在注销操作中使用的内容:

authenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
//authenticationManager.SignOut();

我们也有另一个选项可以从username.foo.com登录和注销,所以当用户直接从username.foo.com登录时,他可以成功注销。只有当用户从 foo.com 登录时才会出现问题。

从基础设施的角度来看,foo.com 没有负载均衡,但 username.foo.com 通过负载均衡器运行(在生产环境中)。但我认为这不会成为问题,因为我在具有相同问题的单个暂存环境中运行这两个应用程序。

我也尝试过自定义 CookieAuthenticationProvider 实现,但它有类似的问题。

请帮忙。

最佳答案

您不能从 username.foo.com 更改 .foo.com 的 cookie。它在 RFC2109 中.

4.3.2 Rejecting Cookies

  • A Set-Cookie from request-host y.x.foo.com for Domain=.foo.com would be rejected, because H is y.x and contains a dot.

您必须改变您的工作流程。您可以使用 redirect_url 将您的用户重定向到 foo.com 以注销,并在成功注销后将他们重定向到 redirect_url 中的 用户名.foo.com.

关于c# - ASP.NET Identity 2 - 在 foo.com 上登录并在 username.foo.com 上注销时注销不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49687274/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com