资料来源:网络整理
时间:2023/2/14 0:57:43 共计:3615 浏览
public CurrentUser currentUser
{
get
{
CurrentUser result = new CurrentUser();
//jwt 解密token
IJsonSerializer serializer = new JsonNetSerializer();
IDateTimeProvider provider = new UtcDateTimeProvider();
IJwtValidator validator = new JwtValidator(serializer, provider);
IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder();
IJwtDecoder decoder = new JwtDecoder(serializer, validator, urlEncoder);
string authHeader = this.Request.Headers["Authorization"];//Header中的token
// Add JWT Protection
if (authHeader != null && authHeader.StartsWith("Bearer"))
{
string token = authHeader.Substring("Bearer ".Length).Trim();
var requestService = HttpContext.RequestServices;
var conf = requestService.GetService(typeof(IConfiguration)) as IConfiguration;
var secretKey = conf["AAA:BBB"];//密钥信息
string resultstr = decoder.Decode(token, secretKey, verify: true);//token为之前生成的字符串
result = JsonConvert.DeserializeObject<CurrentUser>(resultstr);//反序列化 将jwt中的信息解压出来
}
else
{
//Handle what happens if that isn't the case
throw new Exception("The authorization header is either empty or isn't Basic.");
}
return result;
}
}
版权说明:
本网站凡注明“广州京杭 原创”的皆为本站原创文章,如需转载请注明出处!
本网转载皆注明出处,遵循行业规范,如发现作品内容版权或其它问题的,请与我们联系处理!
欢迎扫描右侧微信二维码与我们联系。