CustomRequestCultureProvider:context.User.Identity.IsAuthenticated始终为false [英] CustomRequestCultureProvider: context.User.Identity.IsAuthenticated is always false
问题描述
我正在尝试在本地化管道中实现 CustomRequestCultureProvider
,以从数据库中检索用户区域.要识别用户,我需要 ClaimsPrincipal
,但是出于某种原因,即使用户已通过身份验证, context.User.Identity.IsAuthenticated
始终为false.
这里是相关代码(被截断以突出我的问题:身份验证已在起作用;本地化代码基于 我该如何在我的 注意:更改注册顺序(在 我设法通过手动调用身份验证管道来使其工作.我怀疑我需要这样做,因为 无论如何,这是我修改 I'm trying to implement a Here's the relevant code (truncated to highlight my issue: authentication is already working; localization code is based on the localization tutorial). What should I do to retrieve the Note: changing the registration order (calling I managed to make it work by manually calling the authentication pipeline. I suspect I need to do that because the In any case, here's how I modified my
这篇关于CustomRequestCultureProvider:context.User.Identity.IsAuthenticated始终为false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!公共类启动{公共无效ConfigureServices(IServiceCollection服务){services.Configure< RequestLocalizationOptions>(options =>{varsupportedCultures = new []{新的CultureInfo("fr"),新的CultureInfo("en"),};options.DefaultRequestCulture =新的RequestCulture("fr");options.SupportedCultures = supportCultures;options.SupportedUICultures = supportCultures;//我们在2处插入,因为我们要先检查查询,然后检查cookie,然后检查DB,然后再检查标题options.RequestCultureProviders.Insert(2,新的CustomRequestCultureProvider(异步上下文=>{//----->>问题在这里:这始终是错误的!如果(context.User.Identity.IsAuthenticated)返回新的ProviderCultureResult("en");返回null;}));});服务.AddLocalization(options => options.ResourcesPath =资源").AddMvc().AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix);退货服务;}公共无效的Configure(IApplicationBuilder应用程序,IHostingEnvironment env,ILoggerFactory loggerFactory){app.UseRequestLocalization();app.UseStaticFiles();app.UseAuthentication();app.UseMvcWithDefaultRoute();返回应用程序;}}
CustomRequestCultureProvider
中检索 ClaimsPrincipal
? app.UseRequestLocalization
之前调用 app.UseAuthentification
不能解决问题). Authorize
属性是在中间件管道中稍后触发的,因此现在检索 ClaimsPrincipal
(现在可能是错误的)还为时过早./p> CustomRequestCultureProvider
的方法:
//我们在2处插入,因为我们要先检查查询,然后是cookie,然后是DB,然后是标题options.RequestCultureProviders.Insert(2,新的CustomRequestCultureProvider(异步上下文=>{//从Cookie中检索ClaimsPrincipal//注意:这取决于身份验证的设置方式var authResult =等待context.AuthenticateAsync(CookieAuthenticationDefaults.AuthenticationScheme);如果(!authResult.成功)返回null;var user = authResult.Principal;如果(用户!= null)返回新的ProviderCultureResult("en");返回null;}));
CustomRequestCultureProvider
in my localization pipeline, to retrieve the user culture from the database. To identify the user, I need the ClaimsPrincipal
, but for some reason, even though the user is authenticated, context.User.Identity.IsAuthenticated
is always false.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;
}
}
ClaimsPrincipal
in my CustomRequestCultureProvider
?app.UseAuthentification
before app.UseRequestLocalization
doesn't fix the issue).Authorize
attribute is triggered later in the middleware pipeline, and thus it's too early to retrieve the ClaimsPrincipal
(I might be wrong).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;
}));