示例#1
0
        public async Task <IActionResult> Register([FromBody] RegisterModel model)
        {
            if (!ModelState.IsValid)
            {
                return(ValidationProblem(ModelState));
            }

            var user = new ApplicationUser {
                Email = model.Email, UserName = model.Email
            };
            var result = await _userManager.CreateAsync(user, model.Password);

            if (result.Succeeded)
            {
                await _userManager.AddToRoleAsync(user, RoleHelper.DefaultRole);

                await _service.InsertAsync(UserDto.Default(user.Id, model.UserName));

                var token = CreateToken(user, new List <string> {
                    RoleHelper.DefaultRole
                });

                return(Ok(new
                {
                    access_token = token,
                    username = user.Email
                }));
            }
            else
            {
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            return(ValidationProblem(ModelState));
        }