How to change username using mvc4 and simplemembership [duplicate]

一笑奈何 提交于 2019-12-11 15:09:04

问题


Why is it that you can't change a simplemembership username. I've tried

MembershipUser memUser = Membership.GetUser(existingUser);
memUser.UserName = model.UserName;
Membership.UpdateUser(memUser);

I get that MembershipUser.UserName is read only. There are no other available methods for changing the username.

Is there a way around this problem?

Edit: New problem. When the line

 var confirmationToken = WebSecurity.CreateUserAndAccount(model.UserName, Request["Password"], new { NameFirst = model.NameFirst, NameLast = model.NameLast, ContactId = newContact.ContactId },true);

is run, I get a MembershipCreateUserException and says the username is already in use (it really isn't). It manages to add the user to my UserSecurity table(UserProfile) but doesn't add it to the membership table. It's attempting to add the user in the webpages_Membership table and not the UserMembership table I created.


回答1:


You may not be able to edit the users name with the MembershipProvider. In SimpleMembership you have to add a UserProfile table to your Model, where the username is stored.

You specify the table on initialization:

WebSecurity.InitializeDatabaseConnection("DBname", "UserProfileTable", "IdColumn", "UsernameColumn", autoCreateTables: true);


来源:https://stackoverflow.com/questions/15342352/how-to-change-username-using-mvc4-and-simplemembership

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!