0

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.

zms6445
  • 317
  • 6
  • 22
  • I voted for this to be re-opened because the question is in the context of `SimpleMembership` instead of just `Membership`. – Mike Cole Sep 17 '13 at 20:15

1 Answers1

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);
kapsiR
  • 2,720
  • 28
  • 36
  • I think you can also use it, create the tables of the SimpleMembership at your own and then use the initializer with autoCreateTables: false - but you have to use the same table, as the simpleMembership provider would create automatically – kapsiR Mar 11 '13 at 15:46
  • So would creating just the UserProfile table be enough or must I recreate all the simplemembership tables? I'm not sure if I need to do anything different for the table that stores passwords by hashing it. – zms6445 Mar 11 '13 at 15:48
  • check out these articles too: http://weblogs.asp.net/jgalloway/archive/2012/08/29/simplemembership-membership-providers-universal-providers-and-the-new-asp-net-4-5-web-forms-and-asp-net-mvc-4-templates.aspx http://blog.osbornm.com/2010/07/21/using-simplemembership-with-asp.net-webpages/ – kapsiR Mar 11 '13 at 15:48
  • you have to create at least webpages_Membership table and the UserProfile table, which can have a different name – kapsiR Mar 11 '13 at 15:49
  • it also depends on what you need - if you need roles too, you have to create more – kapsiR Mar 11 '13 at 15:50
  • Your question about hashing: Hashing is be done by the simple membership itself and the hash is stored in the webpages_Membership as string (hashed and salted by System.Web.Helpers.Crypto.HashPassword) HashPassword is doing it by RFC 2898 – kapsiR Mar 11 '13 at 15:54
  • Simplemembership is just a scam to get us using yet another technology. The old one already has the tables you need and works perfectly. – The Muffin Man Mar 11 '13 at 15:54
  • I'm already too deep into simplemembership. I'm nearly done working with it for this project so I'll stick with it. – zms6445 Mar 11 '13 at 15:56
  • Hey kapsi. I ran into a problem. I created the tables with different names and it manages to create a new user the lin eWEbSecurity.CreateUserandAccount creates the user and throws a user already exists error every single time. Do you know what is wrong? – zms6445 Mar 11 '13 at 16:31
  • I don't understand your problem - you may provide more information or code ... – kapsiR Mar 11 '13 at 16:33
  • I've added the code to my question – zms6445 Mar 11 '13 at 17:30
  • You have to use the webpages_Membership table - otherwise it won't work – kapsiR Mar 12 '13 at 06:36