示例#1
0
        public async Task <IActionResult> Register([FromBody] RegistrationRequest request)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                _logger.LogInformation($"Registering new user {request.Username}");

                bool check = await _authService.CheckUserAsync(request);

                if (!check)
                {
                    _logger.LogWarning($"Email or username are already in use");
                    return(Conflict());
                }

                var newUser = new ApplicationUser()
                {
                    Email    = request.Email,
                    UserName = request.Username
                };

                var res = await _userManager.CreateAsync(newUser, request.Password);

                var ress = await _userManager.FindByNameAsync(newUser.UserName);

                if (!res.Succeeded)
                {
                    _logger.LogWarning($"Failed to create user.");
                    return(StatusCode(500));
                }

                var token = await _authService.GetToken(ress);

                _logger.LogInformation($"User {newUser.UserName} registered.");

                return(Ok(token));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                return(StatusCode(500));
            }
        }