Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MembershipCreateUserException - The username supplied is invalid

On this line i am getting an exception -

OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, model.UserName);

System.Web.Security.MembershipCreateUserException: The username supplied is invalid.

The data going into this is

  • provider - "facebook"
  • providerUserId - "1321311387573991"
  • model.UserName - "Max Payne"

The initialization works fine using

WebSecurity.InitializeDatabaseConnection("club", "User", "UserID", "UserName", autoCreateTables: true);

I cannot find any examples of why it says the username is invalid? Is there a criteria somewhere that defines what is a correct user name?

like image 271
MoXplod Avatar asked Oct 12 '12 02:10

MoXplod


2 Answers

I was similarly looking for an explanation to this. I'm not sure I fully understand, but after experimentation, debugging, and watching intellitrace events, it seems CreateOrUpdateAccount is creating or updating an entry in the OAuthMembership table with just Provider, ProviderUserId, and UserId which is determined by querying [in my case] the UserProfile table based on this unique UserName. This way, if you call CreateOrUpdateAccount with a different provider and providerUserId, but the same username, then both provider sign ins are tied to the same user account in your app.

I had to add a UserProfile before I could create/update the corresponding OAuthMembership record. In the VS template, it looked something like this:

db.UserProfiles.Add(new UserProfile { UserName = model.UserName }); 
db.SaveChanges();

OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, model.UserName);
like image 152
anowell Avatar answered Nov 15 '22 22:11

anowell


I had the same problem. I solved it by specifying the correct connection string in SimpleMembershipInitializer and UsersContext class. I'm using ASP.NET MVC4

like image 35
Rexha Avatar answered Nov 15 '22 23:11

Rexha