gpt4 book ai didi

asp.net-mvc - UseCookieAuthentication 与 UseExternalSignInCookie

转载 作者:行者123 更新时间:2023-12-01 16:16:08 25 4
gpt4 key购买 nike

我使用 Owin 通过 Google oAuth 进行授权。以下是我的 cookie 的配置方式:

// Enable the application to use a cookie to store information for the signed in user
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Authentication/Login")
});
// Use a cookie to temporarily store information about a user logging in with a third party login provider
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

所以我同时使用 UseCookieAuthentication 和 UseExternalSignInCookie ,这似乎是多余的。我应该为 IAuthenticationManager 方法(SignIn、SingOUT 等)指定这两个 AuthenticationType 中的哪一个?或者我应该只保留其中之一?

更新。最让我困惑的是 SignIn 方法:

private async Task SignInAsync(ApplicationUser user, bool isPersistent)
{
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
}

因此从ExternalCookie 注销,但登录ApplicationCookie。

最佳答案

如果您希望 Google 登录正常工作,您需要所有这些。这就是它的工作原理。在 OWIN 管道中,您拥有三个中间件组件:(1) 以主动模式运行的 cookie 身份验证中间件,(2) 以被动模式运行的 cookie 身份验证中间件的另一个实例,以及 (3) Google 身份验证中间件。就会像这样。

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
...
}); // Active

app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie); // Passive

app.UseGoogleAuthentication(...);

出现 401 错误时,您的用户会被重定向到 Google。在那里,您的用户登录,Google 验证凭据。然后,Google 将用户重定向回您的应用程序。此时,Google 身份验证中间件获取登录信息,应用授权(读取外部 cookie)并短路 OWIN 管道并重定向到外部回调 URL,该 URL 对应于 ExternalLoginCallback AccountController的行动方法。因此,此时,当请求由于重定向而到达您的应用程序时,您将获得带有用户名和电子邮件声明的外部 Cookie。

为了读取此 cookie 并从 Google 检索数据(用户名等),您可以使用以被动模式运行的 cookie 身份验证中间件。由于该中间件以被动模式运行,因此必须告知它读取 cookie。这就是调用 AuthenticationManager.GetExternalLoginInfoAsync() 时发生的情况制造于ExternalLoginCallback Action 方法。此时,外部 Cookie 的身份已建立,该身份仅包含来自 Google 的姓名和电子邮件声明。

通常,此时您需要从应用程序数据存储中检索用户特定信息,并向身份添加更多声明。那么,您调用Signout在外部 cookie 中间件上,这也将确保外部 cookie 不再因过期而被发回。因此,使用当时可用的身份信息,UserManager.FindAsync被称为 ExternalLoginCallback操作方法,该方法应向用户返回所有应用程序特定的声明。使用这个新身份,您可以调用 SignIn在以主动模式运行的 cookie 身份验证中间件上。这可确保创建新的 cookie。与外部 cookie 相比,这个新 cookie 包含所有应用程序特定的声明。随后,您取回此 cookie,并且在主动模式下运行的 cookie 身份验证中间件会主动读取 cookie 并使用所有应用程序特定声明的完整列表来建立身份。

因此,如果您不调用 Signin,您将不会创建包含所有应用程序特定声明的 cookie。但接下来就取决于你是否使用其他机制了。开箱即用的行为是通过调用 SignIn 创建包含所有应用程序特定声明的本地 cookie。随后由以主动模式运行的 cookie 中间件读取。

更新:我创建了一篇博客文章来解释如何在不使用两个 cookie 中间件实例的情况下摆脱困境。 http://lbadri.wordpress.com/2014/10/14/barebones-asp-net-mvc-google-signin-through-owin-middleware/

关于asp.net-mvc - UseCookieAuthentication 与 UseExternalSignInCookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26166826/

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