Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to confirm a phone number in ASP.Net Core 1.1MVC

I cannot figure out how to do a phone number confirmation in asp.net core 1.1

Identity service configuration contains explicit options to require confirmed email and/or phone number.

It can be done the following way:

services
    .AddIdentity<User, Role>(options =>
    {
        options.SignIn.RequireConfirmedEmail = true;
        options.SignIn.RequireConfirmedPhoneNumber = true;
     });

The validation of the email is quite straight forward as the UserManager contains explicit token generator and its validator:

var token = await _userManager.GenerateEmailConfirmationTokenAsync(user);

The generated token can be validated the following way:

var result = await _userManager.ConfirmEmailAsync(user, code);

The above line will switch the user.EmailConfirmed flag to true if the token is valid.

Now the problem is that I don't see a similar method to generate a phone validation token and its equivalent method to validate it (which in turn should switch the user.PhoneNumberConfirmed flag to true if successful).

User manager contains however few methods to do a user phone change:

_userManager.GenerateChangePhoneNumberTokenAsync();

and

_userManager.VerifyChangePhoneNumberTokenAsync();

But it seems these methods don't switch the user.PhoneNumberConfirmed flag.

Am I missing something? What would be the correct way to confirm the user phone number (in other words to set user.PhoneNumberConfirmed to true) ?

like image 768
Alexandre A. Avatar asked Jul 06 '17 18:07

Alexandre A.


1 Answers

Thanks to @tmg for pointing me to the source code.

According to it, the right way is indeed to use GenerateChangePhoneNumberTokenAsync in order to generate a token and ChangePhoneNumberAsync to validate it (which in turn sets PhoneNumberConfirmed to true if validation succeeds):

var token = await _userManager
    .GenerateChangePhoneNumberTokenAsync(user, user.PhoneNumber);

var result = await _userManager
    .ChangePhoneNumberAsync(user, user.PhoneNumber, token);

Remark: in ASP.NET Core 2.0.0, unfortunately, there is a bug (regression) and GenerateChangePhoneNumberTokenAsync does not generate an SMS-friendly token anymore and is currently useless:

GenerateChangePhoneNumberTokenAsync is not generating an SMS-friendly token in ASP.NET Core 2.0

like image 174
Alexandre A. Avatar answered Oct 01 '22 07:10

Alexandre A.