gpt4 book ai didi

asp.net-core - CustomRequestCultureProvider : context. User.Identity.IsAuthenticated 始终为 false

转载 作者:行者123 更新时间:2023-12-02 04:52:00 25 4
gpt4 key购买 nike

我正在尝试在本地化管道中实现 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com