- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用 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/
我正在使用 ASP.NET Identity 2.0 并尝试将“.AspNet.ExternalCookie”cookie 的域设置为“.mydomain.com”,因为我想从另一个子域读取 cook
我使用 Owin 通过 Google oAuth 进行授权。以下是我的 cookie 的配置方式: // Enable the application to use a cookie to store
我有两个 VS 2013 MVC 5 SPA web 项目。两者都是新创建的并且没有被修改(即仍然是 vanilla 模板)。其中一种有效,一种无效。我能看到的唯一区别是,一个是在从 VS 2010
我是一名优秀的程序员,十分优秀!