MemberhipCreateUserException - Invalid username

On this line, I get an exception -

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

System.Web.Security.MembershipCreateUserException: Invalid username.

The data coming into this,

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

Initialization works using

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

I can’t find examples of why he says the username is invalid? Is there somewhere a criterion that determines the correct username?

+7
source share
6 answers

I also looked for an explanation for this. I'm not sure I fully understand, but after experimenting, debugging, and watching intellitrace events, it seems that CreateOrUpdateAccount creates or updates an entry in the OAuthMembership table using only Provider, ProviderUserId and UserId, which is determined by querying [in my case] the UserProfile table based this unique UserName. Thus, if you call CreateOrUpdateAccount with a different provider and the UserId provider, but with the same username, then both subscriber entries will be bound to the same user account in your application.

I had to add 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); 
+14
source

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

+5
source

You may already be logged into @localhost as someone on another site that you are developing. And you use a cookie with the same cookie name (default is.aspxauth). Therefore, when you call CreateOrUpdate, it tries to add another credential to your already "registered" profile. But the failure is because it is not in the database.

To fix this problem, you need to remove the authentication cookie from the browser and log in again.

+1
source

According to MSDN ( http://msdn.microsoft.com/en-us/library/82xx2e62.aspx ), a member class cannot support usernames with a comma or zero, spaces are in order (like non ascii).

The CreateUser method will return null if the password is empty or null, the username is an empty string or null or contains a comma (,) , the passwordQuestion is not null and represents an empty string, or passwordAnswer is not null and contains an empty string.

0
source

For me, this error occurred in my redefinition of Seed. Because rgnever indicated that the username that does not exist refers to the UserProfile table. To fix this, the user and account must be created before creating OAuth.

So, instead of having my Seed override the ONLY call:

 membership.CreateOrUpdateOAuthAccount(provider, providerid, providername); 

now it calls:

 membership.CreateUserAndAccount(providername, null); membership.CreateOrUpdateOAuthAccount(provider, providerid, providername); 

Note. A blank password means that the user cannot log in through the login form, as the password provided will never be zero. I use only OAuth, so for me this is not a problem.

0
source

The user also should not have a place such as "Max Payne"

-one
source

All Articles