【问题标题】:Is it possible to change HttpContext.Current.User.Identity.Name after change username更改用户名后是否可以更改 HttpContext.Current.User.Identity.Name
【发布时间】:2015-10-22 11:26:21
【问题描述】:

我正在开发一个 ASP MVC 应用程序。并且想在不注销用户的情况下更改用户名。我正在使用身份提供程序版本 1.0.11。我的代码如下:

var updtUser = UserManager.FindById(model.UserId);

        updtUser.UserName = model.PrivateEMail;
        var res = await UserManager.UpdateAsync(updtUser);

        FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
               updtUser.UserName,
               DateTime.Now,
               DateTime.Now,
               false,
               "someData",
               FormsAuthentication.FormsCookiePath);

        string encTicket = FormsAuthentication.Encrypt(ticket);

        Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket));           

        return RedirectToAction("RedirectToDashbord", "Dashboard", new { area = "CRM"});

但是在这个操作之后 HttpContext.Current.User.Identity.Name 没有改变。任何帮助都会很棒

【问题讨论】:

  • 我认为您必须将它们注销并重新登录 - 我知道您仍然使用角色,因为它基于他们的身份验证 cookie

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


【解决方案1】:

您应该启用立即撤销 cookie (+):

app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login"),
            Provider = new CookieAuthenticationProvider
            {
                // Enables the application to validate the security stamp when the user logs in.
                // This is a security feature which is used when you change a password or add an external login to your account.  
                OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                    validateInterval: TimeSpan.FromSeconds(0),
                    regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
            }
        });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-22
    • 2011-08-28
    • 2017-01-29
    • 1970-01-01
    相关资源
    最近更新 更多