6

Is it possible to don't use Email to register and login in asp.net core identity?

Users just register by mobile number and login by SMS verification.

2 Answers 2

10

One possible way is to setup identity as two factor authentication. Instead of email use username to store the mobile number. To do this set RequireUniqueEmail = false in ApplicationUserManager.Create.

You'll need to add your own code to retrieve the number and validate it. Make sure it always has the same format as it should match the username.

In the code where the username is verified skip the password check (since password is null), but do check the number. Send an SMS with code and continue with the flow.

You can add your own logic to create and verify the code and how long it is valid.

2
  • Is there any sample to do it?
    – topcool
    Apr 1, 2019 at 16:59
  • @saeed 2FA with sms is deprecated, but the asp.net core 1.1 documentation may help you.
    – user4864425
    Apr 5, 2019 at 21:13
3

If you don't rely in Username in your application, (Like you don't show it to user ever), then you can set Username to a guid and skip the email part.

For registration,

var identityUser = new User
                   {
                     Username= Guid.NewGuid(),
                     PhoneNumber= phoneNumber
                   };
           
var result = await _signInManager.UserManager.CreateAsync(identityUser, user.Password);

For Login,

var user = await _signInManager.UserManager.Users
                   .SingleOrDefaultAsync(x => 
                      x.NormalizedEmail == usernameParam
                   || x.PhoneNumber== usernameParam);
if (user == null)
{
    return Forbid();
}

var result = await _signInManager.CheckPasswordSignInAsync(user, passwordParam, false);
if (result.Succeeded)
{
  //  return new TokenResponse(token, refreshToken);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.