How does **WebSecurity.CreateUserAndAccount()** checks that username is already present in database?

875 views Asked by At

I am working on mvc4 default internet project, which uses the above mentioned method for creating user account.This method is working fine for me but I am not able to understand how it checks that username is already present in default database.

I am relatively new to MVC 4, please help me.

1

There are 1 answers

0
Mike Brind On BEST ANSWER

The WebSecurity class is a wrapper around the SimpleMembershipProvider class. WebSecurity.CreateUserAndAccount makes an indirect call to SimpleMembershipProvider.CreateAccount which includes the following code:

// Step 2: Check if the user exists in the Membership table: Error if yes.
var result = db.QuerySingle(@"SELECT COUNT(*) FROM [" + MembershipTableName + "] WHERE UserId = @0", uid);
if (result[0] > 0)
{
    throw new MembershipCreateUserException(MembershipCreateStatus.DuplicateUserName);
}

You can check the source code for WebSecurity here: http://aspnetwebstack.codeplex.com/SourceControl/latest#src/WebMatrix.WebData/WebSecurity.cs