Esempio n. 1
0
        public async Task <JwtAuthentificationResponse> Login(LoginRequest requestModel)
        {
            var user = await FindUser(requestModel.Email);

            if (user is null)
            {
                throw new ApplicationCustomException(ExceptionConstants.WrongEmailOrPassword);
            }
            var result = await _signInManager.PasswordSignInAsync(user, requestModel.Password, requestModel.IsRememberMe, false);

            if (!result.Succeeded)
            {
                throw new ApplicationCustomException(ExceptionConstants.WrongEmailOrPassword);
            }
            IEnumerable <string> userRoles = await _userManager.GetRolesAsync(user);

            JwtAuthentificationResponse response = _jwtHelper.GenerateToken(user, userRoles);

            return(response);
        }
Esempio n. 2
0
        public async Task <JwtAuthentificationResponse> RefreshToken(RefreshTokenRequest model)
        {
            JwtSecurityToken refreshToken = new JwtSecurityTokenHandler().ReadJwtToken(model.RefreshToken);

            if (refreshToken.ValidFrom >= DateTime.UtcNow || refreshToken.ValidTo <= DateTime.UtcNow)
            {
                throw new UnauthorizeCustomException(ExceptionConstants.UnauthorizeAccess);
            }
            string          userId = refreshToken.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier)?.Value;
            ApplicationUser user   = await _userManager.FindByIdAsync(userId);

            if (user is null)
            {
                throw new UnauthorizeCustomException(ExceptionConstants.UnauthorizeAccess);
            }
            IEnumerable <string> userRoles = await _userManager.GetRolesAsync(user);

            JwtAuthentificationResponse response = _jwtHelper.GenerateToken(user, userRoles);

            return(response);
        }
Esempio n. 3
0
        public async Task <ActionResult> RefreshToken([FromBody] RefreshTokenRequest request)
        {
            JwtAuthentificationResponse response = await _userService.RefreshToken(request);

            return(Ok(response));
        }
Esempio n. 4
0
        public async Task <ActionResult> Login([FromBody] LoginRequest request)
        {
            JwtAuthentificationResponse response = await _userService.Login(request);

            return(Ok(response));
        }