public async Task <IActionResult> GetConfirmEmailAsync(string username, string code)
        {
            try
            {
                if (string.IsNullOrEmpty(username) || code == null)
                {
                    _logger.LogError(LoggingEvents.GetItemNotFound, $"Null arguments passed to ConfirmEmailAsync: username = {username}; code = {code}.");
                    return(BadRequest("An error occurred"));
                }

                var user = await _userManager.FindByNameAsync(username);

                if (user == null)
                {
                    _logger.LogError(LoggingEvents.GetItemNotFound, $"User with username '{username}' not found");
                    return(NotFound("User not found"));
                }

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

                if (!result.Succeeded)
                {
                    ModelStateErrorsExtensions.AddIdentityErrors(ModelState, result);
                    _logger.LogError(LoggingEvents.UpdateItemNotFound, "Invalid model state:" + ModelStateErrorsExtensions.GetModelStateErrorMessages(ModelState));
                    return(BadRequest("An error occurred"));
                }

                return(Redirect("/confirmed-email"));
            }
            catch (Exception ex)
            {
                _logger.LogError(LoggingEvents.UpdateItemNotFound, ex, "An error occurred in email confirmation.");
                return(BadRequest("An error occurred"));
            }
        }
        public async Task <IActionResult> PostResetPasswordAsync(ResetPasswordViewModel model)
        {
            try
            {
                var user = await _userManager.FindByEmailAsync(model.Email);

                if (user == null)
                {
                    return(Ok()); // Don't reveal that the user does not exist
                }
                var result = await _userManager.ResetPasswordAsync(user, model.Code, model.Password);

                if (result.Succeeded)
                {
                    return(Ok());
                }

                ModelStateErrorsExtensions.AddIdentityErrors(ModelState, result);
                _logger.LogError(LoggingEvents.UpdateItemNotFound, "Invalid model state:" + ModelStateErrorsExtensions.GetModelStateErrorMessages(ModelState));
                return(BadRequest("An error occurred"));
            }
            catch (Exception ex)
            {
                _logger.LogError(LoggingEvents.UpdateItemNotFound, ex, "An error occurred in forgot password.");
                return(BadRequest("An error occurred"));
            }
        }
        public async Task <IActionResult> PostRegisterAsync([FromBody] RegisterViewModel model)
        {
            try
            {
                //if (!ModelState.IsValid)
                //{
                //    _logger.LogError(LoggingEvents.UpdateItemNotFound, "Invalid model state:" + ModelStateErrorsExtensions.GetModelStateErrorMessages(ModelState));
                //    return BadRequest(ModelState);
                //}

                var newUser = new ApplicationUser
                {
                    UserName = model.UserName,
                    Email    = model.Email,
                    DefaultLocationLatitude  = 54.972237,
                    DefaultLocationLongitude = -2.4608560000000352,
                    Avatar           = "https://img.icons8.com/color/96/000000/user.png", // "https://birderstorage.blob.core.windows.net/profile/default.png",
                    RegistrationDate = _systemClock.GetNow
                };

                var result = await _userManager.CreateAsync(newUser, model.Password);

                if (result.Succeeded)
                {
                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(newUser);

                    var url          = _urlService.GetConfirmEmailUrl(newUser.UserName, code);
                    var templateData = new { username = newUser.UserName, url = url };
                    await _emailSender.SendTemplateEmail("d-882e4b133cae40268364c8a929e55ea9", newUser.Email, templateData);

                    return(Ok()); //ToDo: Is this adequate?  Created reponse?
                }

                ModelStateErrorsExtensions.AddIdentityErrors(ModelState, result);
                _logger.LogError(LoggingEvents.UpdateItemNotFound, "Invalid model state:" + ModelStateErrorsExtensions.GetModelStateErrorMessages(ModelState));
                return(BadRequest(ModelState));
            }
            catch (Exception ex)
            {
                _logger.LogError(LoggingEvents.UpdateItemNotFound, ex, "An error occurred in new user registration.");
                return(BadRequest("An error occurred"));
            }
        }