2015-10-22 1 views
1

Je travaille sur une application ASP MVC. Et voulez changer le nom d'utilisateur sans faire de déconnexion de l'utilisateur. J'utilise Identity Provider version 1.0.11. Mon code ressemble:Est-il possible de changer HttpContext.Current.User.Identity.Name après le changement de nom d'utilisateur

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

Mais après ces manipulations HttpContext.Current.User.Identity.Name ne change pas. Toute aide serait grande

+1

Je pense que vous devez les déconnecter et revenir dans - je sais que vous le faites avec des rôles de toute façon, parce qu'il est basé sur leur cookie d'authentification – ediblecode

Répondre

1

Vous devez activer la révocation immédiate des 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)) 
      } 
     });