ASP.NET Core 5 MVC:httpContext.User 未在自定义 CultureProvider 中进行身份验证
ASP.NET Core 5 MVC : httpContext.User not authenticated in custom CultureProvider
我必须在我的 Web 应用程序中添加本地化。
一个请求是 grpc 服务将为所有用户提供默认语言,所以在启动时我试图读取该值但不知道如何在新 CustomRequestCultureProvider
public static CultureInfo[] supportedCultures = new[] { new CultureInfo("it-IT"), new CultureInfo("en-US")};
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<ISpaClient, GrpcSpaClient>();
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddMvc()
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization();
services.Configure<RequestLocalizationOptions>(options =>
{
options.DefaultRequestCulture = new RequestCulture(culture: supportedCultures[0].ToString(), uiCulture: supportedCultures[0].ToString());
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
options.RequestCultureProviders.Clear();
options.RequestCultureProviders.Add(new MyCustomRequestCultureProvider());
});
}
我的习惯class
public class MyCustomRequestCultureProvider : RequestCultureProvider
{
public override async Task<ProviderCultureResult> DetermineProviderCultureResult(HttpContext httpContext)
{
await Task.Yield();
if (httpContext == null)
{
throw new ArgumentNullException(nameof(httpContext));
}
if (!httpContext.User.Identity.IsAuthenticated)
{
return null;
}
var culture = httpContext.User
.Claims
.FirstOrDefault(c => c.Type == TipoClaim.linguaPredefinita.ToString())?
.Value;
if (culture == null)
{
return null;
}
return new ProviderCultureResult(culture);
}
}
但是这里 httpContext.User
永远不会 Authenticated
,即使页面是
我最好的猜测是你在 UseAuthentication 之前调用了 UseRequestLocalization。
我必须在我的 Web 应用程序中添加本地化。
一个请求是 grpc 服务将为所有用户提供默认语言,所以在启动时我试图读取该值但不知道如何在新 CustomRequestCultureProvider
public static CultureInfo[] supportedCultures = new[] { new CultureInfo("it-IT"), new CultureInfo("en-US")};
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<ISpaClient, GrpcSpaClient>();
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddMvc()
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization();
services.Configure<RequestLocalizationOptions>(options =>
{
options.DefaultRequestCulture = new RequestCulture(culture: supportedCultures[0].ToString(), uiCulture: supportedCultures[0].ToString());
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
options.RequestCultureProviders.Clear();
options.RequestCultureProviders.Add(new MyCustomRequestCultureProvider());
});
}
我的习惯class
public class MyCustomRequestCultureProvider : RequestCultureProvider
{
public override async Task<ProviderCultureResult> DetermineProviderCultureResult(HttpContext httpContext)
{
await Task.Yield();
if (httpContext == null)
{
throw new ArgumentNullException(nameof(httpContext));
}
if (!httpContext.User.Identity.IsAuthenticated)
{
return null;
}
var culture = httpContext.User
.Claims
.FirstOrDefault(c => c.Type == TipoClaim.linguaPredefinita.ToString())?
.Value;
if (culture == null)
{
return null;
}
return new ProviderCultureResult(culture);
}
}
但是这里 httpContext.User
永远不会 Authenticated
,即使页面是
我最好的猜测是你在 UseAuthentication 之前调用了 UseRequestLocalization。