Ejemplo n.º 1
0
        public async Task <BaseResponse> RefreshTokenAsync(RefreshTokenResource refreshTokenResource)
        {
            try
            {
                var token = _tokenHandler.TakeRefreshToken(refreshTokenResource.token);
                if (token == null)
                {
                    return(GetErrorResponse("Invalid refresh token"));
                }
                if (token.IsExpired())
                {
                    return(GetErrorResponse("Expired refresh token"));
                }
                User user = await _userRepository.GetUserByValue("Id", refreshTokenResource.userId);

                if (user == null)
                {
                    return(GetErrorResponse("Invalid refresh token"));
                }
                var accesstoken = _tokenHandler.CreateAccessToken(user);

                return(new BaseResponse(true, null, accesstoken));
            }
            catch (Exception ex)
            {
                return(GetErrorResponse(ex.Message));
            }
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> RefreshTokenAsync([FromBody] RefreshTokenResource refreshTokenResource)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var user = await _userManager.FindByEmailAsync(refreshTokenResource.Email);

            if (user == null)
            {
                return(BadRequest(new { message = "Invalid refresh token." }));
            }
            var roles = await _userManager.GetRolesAsync(user);

            var response = _authenticationService.RefreshToken(refreshTokenResource.Token, user, roles);

            if (!response.Success)
            {
                return(BadRequest(response.Message));
            }

            var tokenResource = _mapper.Map <AccessToken, AccessTokenResource>(response.Token);

            return(Ok(tokenResource));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> RefreshTokenAsync([FromBody] RefreshTokenResource refreshTokenResource)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var response = await _iidentityService.RefreshTokenAsync(refreshTokenResource);

            return(Ok(response));
        }
        public async Task <IActionResult> RefreshToken([FromBody] RefreshTokenResource model)
        {
            var authResponse = await _userManager.RefreshTokenAsync(model.Token, model.RefreshToken);

            if (!authResponse.Success)
            {
                return(BadRequest(authResponse.Message));
            }

            return(Ok(authResponse));
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> RefreshTokenAsync([FromBody] RefreshTokenResource refreshTokenResource)
        {
            var response = await _authenticationService.RefreshTokenAsync(refreshTokenResource.Token, refreshTokenResource.UserEmail);

            if (!response.Success)
            {
                return(BadRequest(response.Message));
            }

            var tokenResource = _mapper.Map <AccessToken, AccessTokenResource>(response.Token);

            return(Ok(tokenResource));
        }
Ejemplo n.º 6
0
        public async Task <IActionResult> RefreshTokenAsync([FromBody] RefreshTokenResource refreshTokenResource)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var response = await _authenticationService.RefreshTokenAsync(refreshTokenResource.Token, refreshTokenResource.UserEmail);

            if (!response.Success)
            {
                return(BadRequest(response.Message));
            }


            return(Ok(response));
        }
Ejemplo n.º 7
0
        public async Task <IActionResult> RefreshTokenAsync([FromBody] RefreshTokenResource resource)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState.GetErrorMessages()));
            }

            var result = await _authenticationService.RefreshTokenAsync(resource.Token, resource.Username);

            if (!result.Success)
            {
                return(BadRequest(result.Message));
            }

            var tokenResource = _mapper.Map <AccessToken, AccessTokenResource>(result.AccessToken);

            return(Ok(tokenResource));
        }
        public async Task <IActionResult> RefreshTokenAsync([FromBody] RefreshTokenResource refreshTokenResource)
        {
            if (refreshTokenResource == null || !ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var response = await _authenticationService.RefreshTokenAsync(refreshTokenResource.Token, refreshTokenResource.UserEmail).ConfigureAwait(false);

            if (!response.Success)
            {
                return(BadRequest(response.Message));
            }

            var tokenResource = _mapper.Map <AccessToken, AccessTokenResource>(response.Token);

            return(Ok(tokenResource));
        }
Ejemplo n.º 9
0
        public IActionResult Refresh([FromBody] RefreshTokenResource resource)
        {
            UserModels    sv       = new UserModels();
            IActionResult response = null;

            var identity = (ClaimsIdentity)User.Identity;
            IEnumerable <Claim> claims = identity.Claims;
            var userLogin = claims.FirstOrDefault(c => c.Type == ClaimTypes.Email).Value;

            User user = sv.GetUserbyUserName(userLogin);

            if (user != null && user.Token == resource.Token)
            {
                var role        = sv.GetRolebyId(user.Role);
                var expiresDate = DateTime.Now.AddHours(12);
                var now         = DateTime.Now;
                var userInfo    = sv.GetUserInforByEmail(user.Username);

                if (!string.IsNullOrEmpty(user.Token) && user.Expire != null && user.Expire.Value > DateTime.Now)
                {
                    var tokenResource = new TokenResource
                    {
                        Token  = user.Token,
                        Expiry = user.Expire.Value
                    };
                    response = Json(tokenResource);
                }
                else
                {
                    var tokenResource = BuildToken(userInfo, user, role, now, expiresDate);
                    response = Json(tokenResource);
                }
            }

            return(response);
        }
Ejemplo n.º 10
0
        public async Task <IActionResult> RefreshToken([FromBody] RefreshTokenResource resource)
        {
            if (ModelState.IsValid)
            {
                var refreshToken = await this.refreshTokenRepository.GetRefreshTokenAsync(new RefreshToken { AccessToken = resource.AccessToken, Token = resource.RefreshToken });

                if (refreshToken == null)
                {
                    return(Unauthorized());
                }
                if (!await this.signInManager.CanSignInAsync(refreshToken.User))
                {
                    return(Unauthorized());
                }

                var newRefreshToken = new RefreshToken
                {
                    UserId      = refreshToken.User.Id,
                    AccessToken = await GenerateAccessTokenAsync(refreshToken.User),
                    Token       = Guid.NewGuid().ToString(),
                    CreatedAt   = DateTime.Now
                };
                this.refreshTokenRepository.Add(newRefreshToken);
                await this.unitOfWork.CompleteAsync();

                var response = new TokenResource()
                {
                    AccessToken  = newRefreshToken.AccessToken,
                    RefreshToken = newRefreshToken.Token
                };

                return(new OkObjectResult(response));
            }

            return(new BadRequestObjectResult(new BadRequestResource(ModelState)));
        }