C#中使用JWT实现WabApi的权限控制

C#中使用JWT实现WabApi的权限控制

2023年6月20日发(作者:)

C#中使⽤JWT实现WabApi的权限控制⾸先需要在你的项⽬中引⼊JWT,你只需要在nuget中安装JWT,JWT依赖于.NET Framwork 4.6以上版本,所以你的webapi项⽬也需要.NET Framwork4.6以上版本,我这⾥做法如下: 1 ///

2 /// 验证是否含有token 3 /// 4 public class ApiAuthorizeAttribute : AuthorizeAttribute 5 { 6 private UserIBLL userBll = new UserBLL(); 7 /// 8 /// 验证是否授权 9 /// 10 /// 11 /// 12 protected override bool IsAuthorized(HttpActionContext actionContext)13 {14 //获取header头信息15 IEnumerable tokenInfo;16 Values("Token", out tokenInfo);17 IEnumerable id;18 Values("id", out id);19 if (tokenInfo != null)20 {21 string token = ()[0];22 var userid = ()[0];23 if (!OrEmpty(token))24 {25 try26 {27 UserEntity userEntity = ityByUserId(()[0]);28 if (userEntity == null)29 {30 return false;31 }32 IJsonSerializer serializer = new JsonNetSerializer();33 IDateTimeProvider provider = new UtcDateTimeProvider();34 IJwtValidator validator = new JwtValidator(serializer, provider);35 IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder();36 IJwtDecoder decoder = new JwtDecoder(serializer, validator, urlEncoder);37 var json = (token, , verify: true);38 if (OrEmpty(json))39 {40 return false;41 }42 var newModel = ct();43 if (newModel.F_UserId != userEntity.F_UserId || != )44 {45 return false;46 }47 return true;48 }49 catch (Exception)50 {51 return false;52 }53 }54 }55 return false;56 }57 }

发布者:admin,转转请注明出处:http://www.yc00.com/xiaochengxu/1687249526a13.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信