コード例 #1
0
        public async Task <IActionResult> ChangeEmail([FromBody] ChangeEmailRequest request)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var user = await _userManager.FindByNameAsync(User.Identity.Name);

            if (user == null)
            {
                return(Ok());
            }

            var result = await _userManager.ChangeEmailAsync(user, request.NewEmailAddress, request.Code);

            if (!result.Succeeded)
            {
                return(BadRequest());
            }

            user.EmailConfirmed = false;
            await _userManager.UpdateAsync(user);

            var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

            string baseURL = $"{Request.Scheme}://{Request.Host.Value}";
            await _emailSender.SendConfirmationEmail(user, code, baseURL);

            return(await GetToken(user));
        }