Exemplo n.º 1
0
        public async Task <bool> SaveRefreshTokenAsync(string userId, CreateRefreshTokenDto newRefreshToken)
        {
            if (string.IsNullOrEmpty(userId))
            {
                throw new ArgumentNullException(nameof(userId));
            }

            if (newRefreshToken == default)
            {
                throw new ArgumentNullException(nameof(newRefreshToken));
            }

            var user = await this._userManager.FindByIdAsync(userId);

            if (user == default)
            {
                throw new ArgumentException("User not found.");
            }

            var refreshToken = this._mapper.Map <RefreshToken>(newRefreshToken);

            user.RefreshTokens.Add(refreshToken);

            return(this._context.SaveChanges() > 0);
        }
Exemplo n.º 2
0
        public async Task <ActionResult> RefreshToken(CreateRefreshTokenDto refreshTokenDto)
        {
            var result = await _authenticationService.CreateRefreshTokenAsync(refreshTokenDto);

            if (result.Successeded && result.Data.RefreshToken != null)
            {
                return(Ok(result));
            }
            else
            {
                return(BadRequest(result));
            }
        }
Exemplo n.º 3
0
        public async Task <IDataResult <AccessTokenDto> > CreateRefreshTokenAsync(CreateRefreshTokenDto refreshTokenDto)
        {
            var response = new DataResult <AccessTokenDto>();

            try
            {
                var user = await _userService.GetByRefreshToken(refreshTokenDto.RefreshToken);

                if (user.Data != null && user.Successeded)
                {
                    if (user.Data.RefreshTokenEndDate > DateTime.Now)
                    {
                        var newToken = _token.CreateAccessToken(_mapper.Map <Users>(user.Data));
                        var result   = await _userService.SaveRefreshToken(user.Data.Id, newToken.RefreshToken);

                        response.Successeded = result.Successeded;
                        response.Data        = newToken;
                        response.Message     = "Token üretimi başarılı";
                    }
                    else
                    {
                        response.Successeded = false;
                        response.Data        = null;
                        response.Message     = "Refresh Token Süresi Dolmuş";
                    }
                }
                else
                {
                    response.Successeded = false;
                    response.Data        = null;
                    response.Message     = "Refresh Token'a ait kullanıcı bulunamadı";
                }
            }
            catch (Exception exception)
            {
                response.Successeded = false;
                response.Data        = null;
                response.Message     = exception.Message;
            }
            return(response);
        }
Exemplo n.º 4
0
        public async Task <IDataResult <UserDto> > RemoveRefreshTokenAsync(CreateRefreshTokenDto refreshTokenDto)
        {
            var response = new DataResult <UserDto>();

            try
            {
                var user = await _userService.GetByRefreshToken(refreshTokenDto.RefreshToken);

                if (user.Data != null && user.Successeded)
                {
                    var result = await _userService.RemoveRefreshToken(user.Data.RefreshToken);

                    if (result.Successeded)
                    {
                        response.Successeded = true;
                        response.Data        = result.Data;
                    }
                    else
                    {
                        response.Successeded = false;
                        response.Data        = null;
                    }
                }
                else
                {
                    response.Successeded = false;
                    response.Data        = null;
                    response.Message     = "Refresh Token bulunamadı";
                }
            }
            catch (Exception exception)
            {
                response.Successeded = false;
                response.Data        = null;
                response.Message     = exception.Message;
            }
            return(response);
        }