2
我的应用程序在新文化中运行良好!到现在为止还挺好!登录后覆盖.Net Core“CultureInfo”
但是...我想覆盖的实际“的CultureInfo”在用户登录后,我必须这样做,因为我需要重置“请将CultureInfo.DateTimeFormat”与保存在我的数据库另一种。每个用户都有不同的“DateTimeFormat”,因此我必须在登录后执行此操作!例如在我的控制器 “的AccountController”
启动 - 配置:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseStaticFiles();
app.UseRequestLocalization(app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>().Value);
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationScheme = Configuration.GetValue<string>("Authentication:Name"),
LoginPath = new PathString("/Account/Login/"),
AccessDeniedPath = new PathString("/Account/Forbidden/"),
AutomaticAuthenticate = true,
AutomaticChallenge = true
});
app.UseSession();
app.UseMvc(routes =>
{
routes.MapRoute(
"default",
"{controller=Account}/{action=Login}/{id?}");
});
}
启动 - ConfigureServices:
public void ConfigureServices(IServiceCollection services)
{
services.AddLocalization(opt => { opt.ResourcesPath = "Resources"; });
// Add localization services.
services.Configure<RequestLocalizationOptions>(
opts =>
{
var cultureList = new List<string>();
Configuration.GetSection("Culture:Languages").Bind(cultureList);
var supportedCultures = cultureList.Select(lang => new CultureInfo(lang)).ToList();
opts.DefaultRequestCulture = new RequestCulture(Configuration.GetValue<string>("Culture:Default"));
// Formatting numbers, dates, etc.
opts.SupportedCultures = supportedCultures;
// UI strings that we have localized.
opts.SupportedUICultures = supportedCultures;
});
// Add framework services.
services.AddMvc()
.AddDataAnnotationsLocalization(options =>
{
options.DataAnnotationLocalizerProvider = (type, factory) =>
factory.Create(typeof(SharedResources));
})
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix, opts => { opts.ResourcesPath = "Resources"; });
}
的AccountController:
[HttpPost]
[AllowAnonymous]
public async Task<IActionResult> Login(LoginDto model, string returnUrl = null)
{
ViewData["ReturnUrl"] = returnUrl;
if (ModelState.IsValid)
{
// Validation credentials
var resultDto = await Repository.Get(model);
if (resultDto.Errors != null)
{
ViewBag.Error = _localizer["Token.Invalid"];
return View();
}
// Reset the CultureInfo
...
return RedirectToAction("Admin", "Dashboard");
}
// If we got this far, something failed, redisplay form
return View(model);
}
你不能存储本地化的要求和使用/加'CustomRequestCultureProvider'到您的供应商名单? – Tseng
我可以在登录后将用户文化保存为索赔...但是...如何在登录后使用CustomRequestCultureProvider? – JhobanyPena
一个简单的解决方案是将用户文化设置为线程。这不是最优雅的解决方案,但它非常防弹,易于编码。 'Thread.CurrentCulture' https://msdn.microsoft.com/en-us/library/system.threading.thread.currentculture(v=vs.110).aspx – Svek