另请参见
https://github.com/aspnet/AspNetCore.Docs/issues/13019
几个星期后,我感到困惑,筋疲力尽,全力以赴。
我在.NET 4.6中进行了这项工作,并且正在使用控制器(而不是Razor Pages或脚手架上的身份信息)将其全部移到新的.NET Core 2.2项目中。
我设法使整个Google登录名正常运行并创建了帐户和身份。
但是,问题在于登录后,cookie出现在Chrome中,并根据请求发送,但是HttpContext.User
没有声明,HttpContext.User.Identity.IsAuthenticated == false
。
我正在使用Cosmos DB,因此我具有自定义登录和用户管理器类以及数据访问权限。
我尽力将必要的代码移到了一个全新的.NET Core 2.2应用程序中,然后通过Razor Pages支架添加了身份。进行编译后,这几乎可以立即进行,只是一项将FullName声明纳入我的用户实体的调整。
但是,它遇到了同样的问题!
这是我的“剃刀页面”中的设置。我也曾尝试设置全局授权策略,以防强制索赔主体使用。
我以为app.UseAuthentication()
只不过是将Cookie信息“提升”为ClaimsPrincipal
所需的所有条件,
请帮助。
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddSingleton(typeof(ApplicationContext));
services.AddSingleton<RepositoryFactory, AzureTableStorageRepositoryFactory>();
services.AddTransient(typeof(AzureStorageOperationContext));
services.AddTransient<IAccountEntityRepository, AzureAccountEntityRepository>();
services.AddTransient<IOpenIdentityEntityRepository, AzureOpenIdentityEntityRepository>();
services.AddIdentityCore<AspNetCoreGoogleLoginUser>()
.AddUserStore<TimesheetsUserStore>()
.AddSignInManager<SignInManager<AspNetCoreGoogleLoginUser>>();
// If you want to tweak Identity cookies, they're no longer part of IdentityOptions.
services.ConfigureApplicationCookie(options => options.LoginPath = "/Identity/Account/LogIn");
services.AddAuthentication(authOptions =>
{
authOptions.DefaultAuthenticateScheme = IdentityConstants.ApplicationScheme;
authOptions.DefaultChallengeScheme = IdentityConstants.ApplicationScheme;
authOptions.DefaultSignInScheme = IdentityConstants.ExternalScheme;
})
.AddGoogle(googleOptions =>
{
this.Configuration.Bind("OAuth2:Providers:Google", googleOptions);
googleOptions.ClaimActions.MapJsonKey(ClaimTypes.NameIdentifier, "sub", "string");
})
.AddIdentityCookies();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
app.UseAuthentication();
}