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



我正在做一个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 (+):

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

相关内容

  • 没有找到相关文章

最新更新