How to change user name in ASP.NET 2.0 Membership Provider

This post was written by namwar on April 28, 2007
Posted Under: Uncategorized

Hi Guys,

Although changing the UserName in ASP.NET 2.0 Membership provider is not recommended and Microsoft has made it read only but sometime it becomes necessary to change the incorrect or misspelled login name or user name. To solve this issue, I have created the following stored procedure which can easily change the old Login Name to New Login Name. I have tested it and it is running perfectly fine. It is in SQL Server 2005 TSQL format.

NOTE: Before executing, replace tblUsers and its respective columns with your own User Table.

Following is the code:
Create Procedure uspUsers_ChangeLoginName
@p_OldName varchar(20),
@p_NewName varchar(20)

as
Begin
Set Nocount On
BEGIN TRY
–Check if old user exists
if exists(Select Login from tblUsers where Lower(Login)=Lower(@p_OldName))
Begin
Begin Transaction
Update tblUsers set Login=@p_NewName where Login=@p_OldName
Update aspnet_Users Set
UserName=@p_NewName,
LoweredUserName=Lower(@p_NewName)
Where LoweredUserName=Lower(@p_OldName)
Commit Transaction
Print @p_OldName + ‘ has been successfuly changed to ‘ + @p_NewName
End
Else
Begin
Print ‘Error: Old User Name “‘+@p_OldName + ‘” does not exist. Cannot change the User Name.’
End
END TRY
BEGIN CATCH
–Rollback any tranction, if it was started
If @@Trancount > 0 Rollback Transaction
Print ‘Error occured while trying to replace old UserName to New UserName:’
Print ‘Error No:’ + Convert(varchar,ERROR_NUMBER())
Print ‘Error Line:’ + Convert(varchar,ERROR_LINE())
Print ‘Error Message:’ + Convert(varchar,ERROR_MESSAGE())
END CATCH;
End

Reader Comments

I’ve been looking for a way to do this, but it may not be as simple as a database update.

At the following URL:
http://msmvps.com/blogs/omar/archive/2006/08/18/how-to-change-user-name-in-asp-net-2-0-membership-provider.aspx

a user “Thomas” posts “Works almost fine. It does update the username, but it also creates a new entry in aspnet_Users with the old username. It doesn’t create anything in the aspnet_membership. I don’t want to create a new user and copy the old values since I reference the UserId. Any ideas why it creates a new entry with the old username?”

“Omar” (site owner) answers “ASP.NET Issues a cookie with the user name. So, next time when a request hits with the specified cookie, ASP.NET creates a row in aspnet_users table.”

So it seems the cookie may cause some issues, and maybe you have to make sure the cookie is expired before updating the table. I have not personally verified this but I just want to make sure no one is misguided.

#1 
Written By Chris on February 25th, 2009 @ 4:33 pm

Hey Chris,

I have found the same challenge, however it only was a problem in production - not on my dev box. So, this means it’s a server configuration issue.

That being said, I realized that this only happened when I updated the username of the currently logged in user; i.g. me. If I update other users - no problem.

My Hypothesis is that the reason it was creating a new entry with the old username was because of the profile.save method that I ‘THINK’ is called at the end of the page lifecycle and is based on the current username cookie. Because it can’t find the respective user back in the db, it freaks out and creates a new user.

My solution is merely to log the current user out if they update their own username and force a re-login and renewal of the login context.

Hope this helps.

#2 
Written By Buck Dossey on May 27th, 2009 @ 3:50 am

I have recently come to this issue and this is how I solved it: after invoking the stored procedure for updating the user name, you only need to create authentication cookie with the new username and that way you will avoid forcing the user to relogin with the new username:

MembershipUser user = Membership.GetUser(); UpdateUsers.UpdateUserName(user.UserName,txtNewUserName.Text);
FormsAuthentication.SetAuthCookie(txtNewUserName.Text, true);

#3 
Written By seesharpgears on November 24th, 2009 @ 9:42 pm

Add a Comment

required, use real name
required, will not be published
optional, your blog address