0

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.

tereško
  • 58,060
  • 25
  • 98
  • 150
  • 1
    _Maybe_ with a query (indirectly via MembershipProviderEx)? – Adriano Repetti Jul 16 '14 at 15:44
  • I don't see that it *does* check - [WebSecurity.CreateUserAndAccount Method](http://msdn.microsoft.com/en-us/library/webmatrix.webdata.websecurity.createuserandaccount(v=vs.111).aspx) – crashmstr Jul 16 '14 at 15:51
  • 1
    @crashmstr it does (documentation isn't very exhaustive there). Underlying MembershipProvider will check that and other conditions (password requirements, duplicated e-mail and other stuff) as per web.config settings. – Adriano Repetti Jul 16 '14 at 16:00
  • @Adriano It doesn't check for duplicate email , it is only checking for duplicate username and that is the main reason for my doubt. Actually I was trying to prevent duplicate email using Simplemembership provider but it did not work for me I had to write my own code for it. – user3845758 Jul 16 '14 at 17:42
  • @user3845758 WebSecurity are just shortcut methods for underlying membership providers. Underlying implementation can be instructed (as usual) to perform some checks (duplicated e-mails, password strength validation) through web.config options. – Adriano Repetti Jul 16 '14 at 18:00

1 Answers1

2

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

Mike Brind
  • 28,238
  • 6
  • 56
  • 88