コード例 #1
0
        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"));
            }
        }
コード例 #2
0
        public async Task <IActionResult> UpdateProfileAsync(ManageProfileViewModel model)
        {
            try
            {
                var user = await _userManager.FindByNameAsync(User.Identity.Name);

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

                var userName = user.UserName;
                if (model.UserName != userName)
                {
                    if (await _userManager.FindByNameAsync(model.UserName) != null)
                    {
                        ModelState.AddModelError("Username", $"Username '{model.UserName}' is already taken.");
                        return(BadRequest(ModelState));
                    }
                    var setUserNameResult = await _userManager.SetUserNameAsync(user, model.UserName);

                    if (!setUserNameResult.Succeeded)
                    {
                        ModelState.AddModelError("Username", $"Unexpected error occurred setting username for user with ID '{user.Id}'.");
                        return(BadRequest(ModelState));
                    }

                    // Save Avatar with new username ========> use user id instead
                    //
                    //var file = await _fileClient.GetFile(StorageContainers.Avatar, userName);
                    //if (file != null)
                    //{
                    //    await _fileClient.SaveFile(StorageContainers.Avatar, model.UserName, file);
                    //    await file.DisposeAsync();
                    //    await _fileClient.DeleteFile(StorageContainers.Avatar, userName);
                    //}

                    //var avatarUrl = await _fileClient.GetFileUrl(StorageContainers.Avatar, model.UserName);

                    //if (string.IsNullOrEmpty(avatarUrl))
                    //{
                    //    avatarUrl = "https://img.icons8.com/color/96/000000/user.png";
                    //}

                    //user.Avatar = avatarUrl;
                }

                var email = user.Email;
                if (model.Email != email)
                {
                    var setEmailResult = await _userManager.SetEmailAsync(user, model.Email);

                    if (!setEmailResult.Succeeded)
                    {
                        ModelState.AddModelError("Email", $"Unexpected error occurred setting email for user with ID '{user.Id}'.");
                        return(BadRequest(ModelState));
                    }
                    else
                    {
                        var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

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

                var update = await _userManager.UpdateAsync(user);

                if (!update.Succeeded)
                {
                    throw new ApplicationException($"Unexpected error occurred setting the location for user with ID '{user.Id}'.");
                }

                return(Ok(model));
            }
            catch (Exception ex)
            {
                _logger.LogError(LoggingEvents.UpdateItemNotFound, ex, "GetUserProfileAsync");
                return(BadRequest("There was an error updating the user"));
            }
        }