예제 #1
0
        public async Task <IActionResult> UpdatePhoneNumber([FromBody] UpdateUserPhoneNumberRequest request)
        {
            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                return(NotFound());
            }
            var currentPhoneNumber = user.PhoneNumber ?? string.Empty;

            if (currentPhoneNumber.Equals(request.PhoneNumber, StringComparison.OrdinalIgnoreCase) && await _userManager.IsPhoneNumberConfirmedAsync(user))
            {
                ModelState.AddModelError(nameof(request.PhoneNumber).ToLower(), _userManager.MessageDescriber.UserAlreadyHasPhoneNumber(request.PhoneNumber));
                return(BadRequest(new ValidationProblemDetails(ModelState)));
            }
            var result = await _userManager.SetPhoneNumberAsync(user, request.PhoneNumber);

            if (!result.Succeeded)
            {
                return(BadRequest(result.Errors.AsValidationProblemDetails()));
            }
            if (!_identityServerApiEndpointsOptions.PhoneNumber.SendOtpOnUpdate)
            {
                return(NoContent());
            }
            var smsService = _smsServiceFactory.Create(request.DeliveryChannel);

            if (smsService == null)
            {
                throw new Exception($"No concrete implementation of {nameof(ISmsService)} is registered.");
            }
            var token = await _userManager.GenerateChangePhoneNumberTokenAsync(user, request.PhoneNumber);

            await smsService.SendAsync(request.PhoneNumber, string.Empty, _userManager.MessageDescriber.PhoneNumberVerificationMessage(token));

            return(NoContent());
        }
예제 #2
0
        public async Task <IActionResult> UpdatePhoneNumber([FromBody] UpdateUserPhoneNumberRequest request)
        {
            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                return(NotFound());
            }
            var currentPhoneNumber = await _userManager.GetPhoneNumberAsync(user);

            if (currentPhoneNumber.Equals(request.PhoneNumber, StringComparison.OrdinalIgnoreCase))
            {
                ModelState.AddModelError(nameof(request.PhoneNumber).ToLower(), _messageDescriber.UserAlreadyHasPhoneNumber(request.PhoneNumber));
                return(BadRequest(new ValidationProblemDetails(ModelState)));
            }
            var result = await _userManager.SetPhoneNumberAsync(user, request.PhoneNumber);

            if (!result.Succeeded)
            {
                return(BadRequest(result.Errors.ToValidationProblemDetails()));
            }
            if (!_identityServerApiEndpointsOptions.PhoneNumber.SendOtpOnUpdate)
            {
                return(NoContent());
            }
            if (_smsService == null)
            {
                var message = $"No concrete implementation of {nameof(ISmsService)} is registered. " +
                              $"Check {nameof(ServiceCollectionExtensions.AddSmsServiceYouboto)} extension on {nameof(IServiceCollection)} or provide your own implementation.";
                throw new Exception(message);
            }
            var token = await _userManager.GenerateChangePhoneNumberTokenAsync(user, request.PhoneNumber);

            await _smsService.SendAsync(request.PhoneNumber, string.Empty, _messageDescriber.PhoneNumberVerificationMessage(token));

            return(NoContent());
        }