如何在initialize()方法ASP.net MVC 5中使用userManager

时间:2017-07-12 14:32:17

标签: asp.net asp.net-mvc asp.net-identity asp.net-identity-2

(略过TL; DR版本)

我试图让用户使用他的个人资料中的选项设置我的网站首选语言。我把它全部工作在99%:AccountController在用户更新他的个人资料并设置cookie"语言"时保存文化。然后,该网站将完美地改变其显示的语言。但是,我仍然需要在用户首次登录时设置网站语言,而无需更新其个人资料以创建cookie。

所以,我试图通过覆盖我的基本控制器的Initialize()函数在应用程序周期的早期设置cookie。问题是我无法访问用户首选项,因为UserManager为空。有没有办法从baseController的initialize()函数访问保存在数据库中的用户语言首选项?

TL; DR版本:userManager在baseController的initialize()函数中为null。如何从那里访问当前用户变量?

     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);

    }

1 个答案:

答案 0 :(得分:0)

我刚刚再次阅读了这个问题。我的答案有一些细微的变化。

你根本不应该使用UserManager。只需使用该语言添加对ClaimsIdentity的声明即可。这也可以防止额外调用数据库。

将语言属性添加到AppicationUser。确保将其添加为声明:

public class ApplicationUser : IdentityUser
{
    public string Language { get; set; }

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);

        // Add custom user claims here
        userIdentity.AddClaim(new Claim("Language", Language));

        return userIdentity;
    }
}

在初始化中,您可以阅读如下声明:

 protected override void Initialize(RequestContext requestContext)
{
    try
    {
        // Try claims collection first
        var culture = (System.Security.Claims.ClaimsIdentity)User.Identity).FindFirst("Language").Value;

        // otherwise try cookie
        if (culture == null)
            culture = requestContext.HttpContext.GetOwinContext().Request.Cookies["language"];

        if (culture == null)
        {
            // user is not logged in and there is no cookie
            culture = "fr-CA";
            HttpCookie language = new HttpCookie("language");
            language.Value = culture;
            Response.Cookies.Add(language);
        }
        Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo(culture);
        Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(culture);
    }
    catch (Exception e)
    {
    }
    base.Initialize(requestContext);
}

像这样的东西。仍然没有测试它,但这是个主意。 如果不清楚,请告诉我。