更改用户名后是否可以更改 HttpContext.Current.User.Identity.Name

Is it possible to change HttpContext.Current.User.Identity.Name after change username

我正在开发 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 没有改变。任何帮助都会很棒

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

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