(Skip this for a TL;DR version)
I am trying to let the user set his prefered language of my website using an option in his profile. I got it all working at 99%: AccountController saves the culture when the user updates his profile and set a cookie "language". The website will then change its displayed language perfectly. However, I still need to set the website language when the user sign-in the first time without him having to update his profile to create the cookie.
So, I am trying to set the cookie early in the app cycle by overriding the Initialize() function of my base controller. The problem is that I can't access the user preference as UserManager is null. Is there a way to access my user's language preference saved in the database from the initialize() function of the baseController?
TL;DR version: userManager is null in initialize() function of the baseController. How can I access the current user variables from there ?
protected override void Initialize(RequestContext requestContext)
{
try
{
String culture = requestContext.HttpContext.GetOwinContext().Request.Cookies["language"];
if(culture != null)
{
Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo(culture);
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(culture);
}
else
{
if (requestContext.HttpContext.User.Identity.IsAuthenticated)
{
String userName = requestContext.HttpContext.User.Identity.GetUserName();
ApplicationUser user = UserManager.FindByName(userName); //TODO: UserManager is NULLL !!!
HttpCookie language = new HttpCookie("language");
if (user.DefaultLanguage)
{
language.Value = "fr-CA";
}
else
{
language.Value = "en-CA";
}
Response.Cookies.Add(language);
}
}
}
catch (Exception e)
{
}
base.Initialize(requestContext);
}