Orchard Core 2.1.2 - UserManager and error No IUserTwoFactorTokenProvider<TUser> named 'Phone' is registered
#17221
-
Hi Everyone, While updating my SSO service to OC 2.1.2, I encountered another issue. I’m getting the error Before the update from 1.8.2 to 2.1.2, it was working without any problems, and it wasn’t necessary to register the Can you help me understand what I might be doing wrong? Maybe this provider is not anymore registered by Orchard Core, and how can I register it? The error occurs here, in the private readonly UserManager<IUser> _userManager;
[...]
string token = await _userManager.GenerateChangePhoneNumberTokenAsync(user, userApiModel.PhoneNumber); Here the entire method: public async Task<ApiResult<UserApiModel>> UpdateUserByEmailAsync(UserApiModel userApiModel)
{
_logger.LogInformation($"{nameof(UserHandler)}-{nameof(UpdateUserByEmailAsync)} started.");
// Retrieve the existing user by email
IUser? existingUser = await _userManager.FindByEmailAsync(userApiModel.Email);
if (existingUser == null)
{
_logger.LogWarning("User with email {Email} not found.", userApiModel.Email);
return new ApiResult<UserApiModel>(HttpStatusCode.NotFound, $"User with email: {userApiModel.Email} does not exist.");
}
// Cast to User to access properties like Email, PhoneNumber, etc.
User? user = existingUser as User;
if (user == null)
{
_logger.LogError("Failed to cast existing user to \"User\" type.");
return new ApiResult<UserApiModel>(HttpStatusCode.InternalServerError, "Failed to cast existing user to User type.");
}
// Update phone number if it has changed
if (user.PhoneNumber != userApiModel.PhoneNumber)
{
string token = await _userManager.GenerateChangePhoneNumberTokenAsync(user, userApiModel.PhoneNumber);
IdentityResult phoneNumberChangeResult = await _userManager.ChangePhoneNumberAsync(user, userApiModel.PhoneNumber, token);
if (!phoneNumberChangeResult.Succeeded)
{
_logger.LogError("Failed to change phone number for user {UserId}. Error: {Error}", user.Id, string.Join(", ", phoneNumberChangeResult.Errors.Select(e => e.Description)));
return new ApiResult<UserApiModel>(HttpStatusCode.InternalServerError, $"Failed to change phone number. Error: {string.Join(", ", phoneNumberChangeResult.Errors.Select(e => e.Description))}");
}
}
// Mapping and more [...]
_logger.LogInformation($"{nameof(UserHandler)}-{nameof(UpdateUserByEmailAsync)} completed successfully.");
return new ApiResult<UserApiModel>(HttpStatusCode.OK, userApiModel);
} Error log:
Thank you |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
@MikeAlhayek @Piedone sorry for mentioning you 🙏 Do you have any idea why in OC 2.1.2, the Thank you |
Beta Was this translation helpful? Give feedback.
@MarGraz
In OC 2, we don't register all token providers by default as we did before. In orchard, the only place to use the Phone number provider is in "OrchardCore.Users.2FA.Sms". So if you want that token provider in your custom project, you can either add dependency on "OrchardCore.Users.2FA.Sms" feature which provides it and also enable SMS two factor authentication. Or, configure the token provider in your own project "since your project seems to depend on it" like this
OrchardCore/src/OrchardCore.Modules/OrchardCore.Users/TwoFactorAuthenticationStartup.cs
Lines 202 to 208 in 21ab7d7