【发布时间】:2019-03-18 23:24:07
【问题描述】:
我有一个具有以下配置的 ASP.NET Core 2.x 项目:
services
.AddAuthentication(options => options.DefaultScheme = CookieAuthenticaitonDefaults.AuthenticationScheme)
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme)
.AddFacebook(ConfigureFacebook);
可以预见的是,当我从我的一项操作中调用时:
return Challenge(new AuthenticationProperties { RedirectUri = "/test" }, "Facebook");
... 然后,我浏览了 Facebook OAuth 序列。当我找到返回应用程序的方法时,HttpContext.User.Identity 会填充相关详细信息:
-
User.Identity.Name- Facebook 用户名。 -
User.Identity.AuthenticationType- 字符串"Facebook"。 -
User.Identity.IsAuthenticated-true。
这一切都很好,正如预期的那样。但是,如果我将以下内容添加到我的应用程序配置中
services.AddIdentity<MyUserType, MyRoleType>()
.AddEntityFrameworkStores<MyDbContext>();
突然之间,OAuth 流程以 User.Identity 匿名结束,没有其他任何更改。如果我们深入研究 IdentityServiceCollectionExtensions.cs,我们会发现:
options.DefaultAuthenticateScheme = IdentityConstants.ApplicationScheme; options.DefaultChallengeScheme = IdentityConstants.ApplicationScheme; options.DefaultSignInScheme = IdentityConstants.ExternalScheme;
除此之外......
这里发生了什么?为什么 Identity 会干扰 Cookie 过程,以及从 OAuth 提供者返回用户的正确方法是什么?
【问题讨论】:
标签: c# asp.net-core asp.net-identity