C# 获取Header中的token值
2024-10-01 08:54:44
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;
}
}
最新文章
- React-Native 组件开发方法
- 【转】logback 常用配置详解(序)logback 简介
- python随便笔记。。。
- 原生js arguments 用法
- Delphi是座宝山,有待挖掘
- Java知识点:琐碎知识点(2)
- VB程序逆向反汇编常见的函数
- flex toolTip样式设置
- Windows Live Writer 2014版绿色版制作及主题获取
- 第五章 服务容错保护:Spring Cloud Hystrix
- mybatis中两种取值方式?谈谈Spring框架理解?
- 第三十八篇-logcat的使用
- 01背包-dp
- 根据txt中的文件名将文件复制到目标文件夹中
- NOIP 2000 乘积最大
- 【java多线程】线程状态分析
- js中实现 复制到剪切板 功能
- 关于进行pdf的每页广告去除、转换word等方案。
- Snail—UI学习之得到某组件的方法
- PLT redirection through shared object injection into a running process
热门文章
- Ubuntu下解决MySQL自启动,chkconfig list 全部off 情况
- C++ 设置光标问题
- C#Winform的DEV下拉下拉控件介绍
- [leetcode]244. Shortest Word Distance II最短单词距离(允许连环call)
- APIView流程——请求方式分发
- php之$_SESSION的理解
- spring入门——applicationContext与BeanFactory的区别
- surf the internet scientifically
- Some Websites To Learning Laravel
- python 根据字符串内数字排序