专业网站建设品牌,十四年专业建站经验,服务6000+客户--广州京杭网络
免费热线:400-683-0016      微信咨询  |  联系我们

C# 获取Header中的token值

当前位置:网站建设 > 技术支持
资料来源:网络整理       时间: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;
            }
        }
版权说明:
本网站凡注明“广州京杭 原创”的皆为本站原创文章,如需转载请注明出处!
本网转载皆注明出处,遵循行业规范,如发现作品内容版权或其它问题的,请与我们联系处理!
欢迎扫描右侧微信二维码与我们联系。
·上一条:简单一码付:将支付宝和微信的收款二维码合并为一个二维码 | ·下一条:将微信和支付宝支付的个二维码合二为一

Copyright © 广州京杭网络科技有限公司 2005-2024 版权所有    粤ICP备16019765号 

广州京杭网络科技有限公司 版权所有