public async Task <ActionResult> Refresh(RefreshReq req) { var result = await Client.RequestRefreshTokenAsync(new RefreshTokenRequest { Address = "http://localhost:32354/connect/token", ClientId = "Api", ClientSecret = "6CD30DB681204AFA85CEFF8F157FE28E", RefreshToken = req.Token }, CancellationToken.None); if (result.IsError == false && !string.IsNullOrEmpty(result.AccessToken)) { var handler = new JwtSecurityTokenHandler(); var token = handler.ReadToken(result.AccessToken) as JwtSecurityToken; var systemCode = token.Claims.FirstOrDefault(a => a.Type == "SystemCode").Value.ToString(); var UserName = token.Claims.FirstOrDefault(a => a.Type == "Name").Value.ToString(); SaveTokenToRedis($"{systemCode}-{UserName}", result); } return(Json(new JwtToken(result.AccessToken, result.RefreshToken))); }
public IActionResult RefreshToken(RefreshReq refreshReq) { if (!securityDb.checkIfTokenExists(refreshReq.refreshToken)) { return(NotFound()); } var claims = new[] { new Claim(ClaimTypes.Role, "Employee"), }; var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configuration["SecretKey"])); var credentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha512); var token = new JwtSecurityToken ( issuer: "Program", audience: "Ktokolwiek", claims: claims, expires: DateTime.Now.AddMinutes(60), signingCredentials: credentials ); var rToken = Guid.NewGuid(); securityDb.createRefreshToken(rToken); return(Ok(new { token = new JwtSecurityTokenHandler().WriteToken(token), refreshToken = rToken })); }