CustomRequestCultureProvider:context.User.Identity.IsAuthenticated 始终为 false
CustomRequestCultureProvider: context.User.Identity.IsAuthenticated is always false
我正在尝试在我的本地化管道中实施 CustomRequestCultureProvider
,以从数据库中检索用户文化。要识别用户,我需要 ClaimsPrincipal
,但出于某种原因,即使用户已通过身份验证,context.User.Identity.IsAuthenticated
始终为 false。
这是相关代码(被截断以突出我的问题:身份验证已经在工作;本地化代码基于 localization tutorial)。
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new[]
{
new CultureInfo("fr"),
new CultureInfo("en"),
};
options.DefaultRequestCulture = new RequestCulture("fr");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
// we insert at 2, because we want to first check the query, then the cookie, then the DB, then the headers
options.RequestCultureProviders.Insert(2, new CustomRequestCultureProvider(async context =>
{
// ----->> ISSUE IS HERE: this is always false!
if (context.User.Identity.IsAuthenticated)
return new ProviderCultureResult("en");
return null;
}));
});
services
.AddLocalization(options => options.ResourcesPath = "Resources")
.AddMvc()
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
;
return services;
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseRequestLocalization();
app.UseStaticFiles();
app.UseAuthentication();
app.UseMvcWithDefaultRoute();
return app;
}
}
我应该怎么做才能在我的 CustomRequestCultureProvider
中检索 ClaimsPrincipal
?
注意:更改注册顺序(在 app.UseRequestLocalization
之前调用 app.UseAuthentification
无法解决问题)。
我设法通过手动调用身份验证管道使其工作。我怀疑我需要这样做,因为 Authorize
属性稍后会在中间件管道中触发,因此检索 ClaimsPrincipal
还为时过早(我可能错了)。
无论如何,这是我修改 CustomRequestCultureProvider
的方式:
// we insert at 2, because we want to first check the query, then the cookie, then the DB, then the headers
options.RequestCultureProviders.Insert(2, new CustomRequestCultureProvider(async context =>
{
// retrieve the ClaimsPrincipal from the cookies
// note: this is dependent on how your authentication is set up
var authResult = await context.AuthenticateAsync(CookieAuthenticationDefaults.AuthenticationScheme);
if (!authResult.Succeeded)
return null;
var user = authResult.Principal;
if (user != null)
return new ProviderCultureResult("en");
return null;
}));
为了防止将来有人遇到这个问题,我在 ASP.NET Core 2.1 中遇到了类似的问题,结果证明我在插入 CustomRequestCultureProvider
后注册了身份验证中间件。为了修复它,我将 app.UseAuthentication()
移到了 options.RequestCultureProviders.Insert(...
上方
我正在尝试在我的本地化管道中实施 CustomRequestCultureProvider
,以从数据库中检索用户文化。要识别用户,我需要 ClaimsPrincipal
,但出于某种原因,即使用户已通过身份验证,context.User.Identity.IsAuthenticated
始终为 false。
这是相关代码(被截断以突出我的问题:身份验证已经在工作;本地化代码基于 localization tutorial)。
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new[]
{
new CultureInfo("fr"),
new CultureInfo("en"),
};
options.DefaultRequestCulture = new RequestCulture("fr");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
// we insert at 2, because we want to first check the query, then the cookie, then the DB, then the headers
options.RequestCultureProviders.Insert(2, new CustomRequestCultureProvider(async context =>
{
// ----->> ISSUE IS HERE: this is always false!
if (context.User.Identity.IsAuthenticated)
return new ProviderCultureResult("en");
return null;
}));
});
services
.AddLocalization(options => options.ResourcesPath = "Resources")
.AddMvc()
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
;
return services;
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseRequestLocalization();
app.UseStaticFiles();
app.UseAuthentication();
app.UseMvcWithDefaultRoute();
return app;
}
}
我应该怎么做才能在我的 CustomRequestCultureProvider
中检索 ClaimsPrincipal
?
注意:更改注册顺序(在 app.UseRequestLocalization
之前调用 app.UseAuthentification
无法解决问题)。
我设法通过手动调用身份验证管道使其工作。我怀疑我需要这样做,因为 Authorize
属性稍后会在中间件管道中触发,因此检索 ClaimsPrincipal
还为时过早(我可能错了)。
无论如何,这是我修改 CustomRequestCultureProvider
的方式:
// we insert at 2, because we want to first check the query, then the cookie, then the DB, then the headers
options.RequestCultureProviders.Insert(2, new CustomRequestCultureProvider(async context =>
{
// retrieve the ClaimsPrincipal from the cookies
// note: this is dependent on how your authentication is set up
var authResult = await context.AuthenticateAsync(CookieAuthenticationDefaults.AuthenticationScheme);
if (!authResult.Succeeded)
return null;
var user = authResult.Principal;
if (user != null)
return new ProviderCultureResult("en");
return null;
}));
为了防止将来有人遇到这个问题,我在 ASP.NET Core 2.1 中遇到了类似的问题,结果证明我在插入 CustomRequestCultureProvider
后注册了身份验证中间件。为了修复它,我将 app.UseAuthentication()
移到了 options.RequestCultureProviders.Insert(...