c# - How can I refresh my jwtsecuritytoken in my project?
问题描述
I create a .net core api which will send a jwtsecuritytoken to client. Connected user to use a functionality of the application have to have a token for each functionality, this token have an expiration date of 5 minutes for exemple and the token have to be refresh after his expiration (if there is no error).
I start to code something but I don't know how to do the refresh of my token ?
[Route("api/[controller]")]
[ApiController]
public class TokenController : ControllerBase
{
private string GenerateToken(string username)
{
SymmetricSecurityKey key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("ijustwanttotestsomething"));
Claim[] claims = new Claim[]
{
new Claim(ClaimTypes.Name , username)
};
JwtSecurityToken jwt = new JwtSecurityToken(
claims: claims,
notBefore: DateTime.UtcNow,
expires: DateTime.UtcNow.AddMinutes(5),
signingCredentials: new SigningCredentials(key, SecurityAlgorithms.HmacSha256)
);
return new JwtSecurityTokenHandler().WriteToken(jwt);
}
[HttpPost]
public ActionResult<string> Create(string username)
{
return GenerateToken(username);
}
[HttpGet]
public ActionResult<JwtSecurityToken> TokenExpired (string token)
{
var stream = token ;
var handler = new JwtSecurityTokenHandler();
var jsonToken = handler.ReadToken(stream);
JwtSecurityToken tokenS = handler.ReadToken(stream) as JwtSecurityToken;
DateTime dateTimeToken = DateTime.UtcNow;
if (dateTimeToken > tokenS.ValidTo)
return BadRequest("EXPIRED");
return Ok(tokenS);
}
[HttpGet("[Action]")]
public ActionResult<JwtSecurityToken> RefreshToken (string token)
{
// CODE SOMETHING
}
}
解决方案
推荐阅读
- scala - Flink:java.lang.UnsupportedOperationException:无法覆盖 KeyedStream 的分区
- python - 如何比较字符串中的字符
- python - 在python 3中使用matplotlib散点函数时如何更改点子集的绘图标记
- sql - 使用 CASE WHEN 语句和 IN (SELECT...FROM) 子查询
- python - python - 如何检查Union [...]中的任何参数在python中是否为None?
- c# - 为什么 .NET Core 3 应用程序在使用 Webview 时会崩溃?
- powerbi - Power Query - 查找拆分和删除
- jquery - 使用正则表达式选择器附加回调
- android - 向下滚动时无法隐藏默认操作栏
- c# - 如何使用读取文件的数据