I am able to change a username by directly accessing the asp.net membership user tables. However, the old username is preserved in a new row and assigned a new UserID automatically by asp.net. How do I stop that from happening?
EDIT: only in the users table and roles table, not the membership table.
var mUser = dc.aspnet_Users
.Where(u => u.UserId == (Guid)user.ProviderUserKey)
.Select(u => u).SingleOrDefault();
mUser.UserName = newName;
mUser.LoweredUserName = newName.ToLower();
try
{
dc.SubmitChanges();
}
catch
{
...
}
Best Answer
Changing username is not supported by the sql membership provider in ASP.NET 2.0. You can still change the username but you have to use custom implementation.
Also you have to update the membership cookie with the new username in order to avoid recreation of the user with the same username but new UserId.
In the example below I use Linq to SQL to update the membership tables - I have data context called MembershipDataContext.