Change username in MVC 5

I am using ASP.NET MVC5 and Identity 2.0 (beta).

Can users change username?

I am trying to use the UserManager.UpdateAsync method, throwing an exception.

Regrads

Fran.

+7
identity username membership
source share
3 answers

Yes, this is possible using the UpdateAsync method, but you need to make sure that you update the email fields and username.

var user = userManager.FindById(userId); user.Email = email; user.UserName = email; var updateResult = await userManager.UpdateAsync(user); 

This method works for me successfully

+8
source share

This works for me:

  public async Task<ActionResult> ChangeUsername(string value) { if (UserManager.Users.Where(x => x.UserName == value).FirstOrDefault() == null) //chk for dupes { var user = UserManager.FindById(User.Identity.GetUserId()); user.UserName = value; var updateResult = await UserManager.UpdateAsync(user); store.Context.SaveChanges(); await SignInAsync(user,true);//user is cached until logout so do this to clear cache return Content("true"); } throw new HttpException(500, "Please select a different username"); } 
+1
source share

It may not be so pretty, but try the following:

 db.Database.ExecuteSqlCommand("update AspNetUsers set UserName=" + NewUserName + " where UserName = " + OldUserName); 
-4
source share

All Articles