- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在本地化管道中实现 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
并不能解决问题)。
最佳答案
以防万一将来有人遇到这个问题,我在 ASP.NET Core 2.1 中遇到了类似的问题,结果发现我在插入 CustomRequestCultureProvider
后注册了身份验证中间件。为了修复它,我将 app.UseAuthentication()
移至 options.RequestCultureProviders.Insert(...
关于asp.net-core - CustomRequestCultureProvider : context. User.Identity.IsAuthenticated 始终为 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48786495/
我正在尝试在本地化管道中实现 CustomRequestCultureProvider,以从数据库中检索用户区域性。为了识别用户,我需要 ClaimsPrincipal,但由于某种原因,即使用户经过身
我是一名优秀的程序员,十分优秀!