コード例 #1
0
        public async Task <ActionResult <TokenResponseDTO> > RefreshToken()
        {
            HttpContext.Request.Cookies.TryGetValue("MyKey", out var refreshToken);

            if (string.IsNullOrWhiteSpace(refreshToken))
            {
                return(Unauthorized());
            }

            var hasValidRefreshToken = await _tokenService.CheckForValidRefreshToken(new RefreshToken { Token = refreshToken });

            if (!hasValidRefreshToken)
            {
                return(Unauthorized());
            }

            var token = await _context.RefreshTokens.FirstOrDefaultAsync(t => t.Token == refreshToken);

            var user = await _userManager.FindByIdAsync(token.UserId);

            var accessToken = new TokenResponseDTO
            {
                Token = await _tokenService.GenerateAccessToken(user.Email)
            };

            return(Ok(accessToken));
        }