예제 #1
0
        public async Task <IActionResult> Register([FromBody] UserRegistrationRequest user)
        {
            if (ModelState.IsValid)
            {
                var existingUser = await _userManager.FindByEmailAsync(user.Email);

                if (existingUser != null)
                {
                    return(BadRequest(new RegistrationResponse()
                    {
                        Result = false,
                        Errors = new List <string>()
                        {
                            "Email already exist"
                        }
                    }));
                }

                var newUser = new IdentityUser()
                {
                    Email = user.Email, UserName = user.Email
                };
                var isCreated = await _userManager.CreateAsync(newUser, user.Password);

                if (isCreated.Succeeded)
                {
                    var jwtToken = _jwtGenerator.GenerateJwtToken(newUser);
                    return(Ok(new RegistrationResponse()
                    {
                        Result = true,
                        Token = jwtToken
                    }));
                }

                return(new JsonResult(new RegistrationResponse()
                {
                    Result = false,
                    Errors = isCreated.Errors.Select(x => x.Description).ToList()
                })
                {
                    StatusCode = 500
                });
            }

            return(BadRequest(new RegistrationResponse()
            {
                Result = false,
                Errors = new List <string>()
                {
                    "Invalid payload"
                }
            }));
        }
예제 #2
0
        public async Task <IActionResult> SignInAsync([FromBody] LoginDto login)
        {
            try
            {
                if (login is null || string.IsNullOrWhiteSpace(login.Password))
                {
                    return(this.SetError($"The parameter [{nameof(login.Password)}] is required !!", "NullParameter", StatusCodes.Status400BadRequest));
                }

                var signInResult = await this.signInManager.PasswordSignInAsync(login.Username, login.Password, false, true);

                if (!signInResult.Succeeded)
                {
                    return(this.SetError($"Login Failed for the User [{login.Username}].", "InvalidCredentials", StatusCodes.Status400BadRequest));
                }

                var user = await this.userManager.FindByNameAsync(login.Username);

                var result = JwtGenerator.GenerateJwtToken(user, this.jwtOptions);

                return(this.StatusCode(StatusCodes.Status200OK, result));
            }
            catch (Exception ex)
            {
                return(this.SetError(ex.Message));
            }
        }
예제 #3
0
 public async Task <IActionResult> TokenPost(string email, string password, string grant_type)
 {
     if (await IsValidUsernameAndPassword(email, password))
     {
         return(new ObjectResult(await _jwtGenerator.GenerateJwtToken(email)));
     }
     else
     {
         return(BadRequest());
     }
 }
        public async Task <IActionResult> Login([FromBody] LoginDto model)
        {
            var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, false, false);

            if (result.Succeeded)
            {
                var appUser = _userManager.Users.SingleOrDefault(r => r.Email == model.Email);
                return(Ok(new User {
                    AppUser = appUser, Token = JwtGenerator.GenerateJwtToken(model.Email, appUser, _configuration)
                }));
            }

            return(NotFound());
        }