0

I've created the simple membership tables myself so that I have the ability to update the username field(can't do this if you just let it autocreate it for you).

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

When I run this line of code I keep getting the error message that the Username already exists. What is weird is that there is no username in the table with the same name and it does it for every registration. It has something to do with me creating the tables myself probably but I can't see where it's having this problem.

It does add it to my UserProfile table (called it UserSecurity) but doesn't include the user in the membership table.

Edit: The line above is using the webpages_Membership table. I need it to use my UserMembership table instead.

zms6445
  • 317
  • 6
  • 22
  • 1
    Could you post the complete code? Maybe a dumb question, but have you verified that your model is being populated and .UserName isn't a blank string(or null)? – Ryan O'Neill Mar 11 '13 at 18:06
  • I've just found out that the line i showed in my code is using webpages_Membership and not my own Membership table. How do I make it use my Membership table? – zms6445 Mar 11 '13 at 18:09
  • Username is not null and the model is being populated – zms6445 Mar 11 '13 at 18:13
  • or followup this link from that you can add membership table in sql server http://weblogs.asp.net/sukumarraju/archive/2009/10/02/installing-asp-net-membership-services-database-in-sql-server-expreess.aspx – A.Goutam Mar 11 '13 at 18:15
  • I think you miss to add asp.net membership table in your db – A.Goutam Mar 11 '13 at 18:15
  • A.Goutam I'm using Simplemembership not the old provider. – zms6445 Mar 11 '13 at 18:18
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/25975/discussion-between-a-goutam-and-zms6445) – A.Goutam Mar 11 '13 at 18:18

1 Answers1

0

Edit: The line above is using the webpages_Membership table. I need it to use my UserMembership table instead.

If you want Simple Membership Provider to use a different table than the default one you need to change the table that WebSecurity.InitializeDatabaseConnection points to.

In solution explorer, go to Filters -> InitializeSimpleMembershipAttribute.cs and use this instead:

WebSecurity.InitializeDatabaseConnection("MyContext", "TableToPointTo", 
"UserIdColumn", "UserNameColumn", autoCreateTables: true);
MattSull
  • 5,514
  • 5
  • 46
  • 68
  • I had this. I had autoCreateTables: false instead though. Now it uses my UserSecurity instead of UserProfile table. But it still continues to use the other tables for roles and membership. – zms6445 Mar 11 '13 at 18:35
  • So do you want your UserSecurity table to store all account related data, roles etc? – MattSull Mar 11 '13 at 18:48
  • No, I was told that I needed to create the simplemembership tables manually so that I would be able to allow users to change their username in the UserProfile table. Read my other question that lead me to attempting this http://stackoverflow.com/questions/15342352/how-to-change-username-using-mvc4-and-simplemembership – zms6445 Mar 11 '13 at 18:52
  • I may be wrong, but I don't think manually creating those tables is the solution to allow users to change their usernames. You would probably be better off writing your own membership provider. SimpleMembershipProvider inherits from ExtendedMembershipProvider. Here's a link to the SimpleMembershipProvider source code that MVC4 uses, look at the change password methods, they may give you some guidance. http://aspnetwebstack.codeplex.com/SourceControl/changeset/view/bf897646cb14#src/WebMatrix.WebData/SimpleMembershipProvider.cs – MattSull Mar 11 '13 at 19:19