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

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-03 09:16:10

问题


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) ?


回答1:


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



来源:https://stackoverflow.com/questions/44956824/how-to-confirm-a-phone-number-in-asp-net-core-1-1mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!