zoukankan      html  css  js  c++  java
  • webapi JWT 认证

    第一步 使用ng安装JWT组件

    第二步 编写登录和生成token代码

                byte[] key = Encoding.UTF8.GetBytes("123456789aaaaaaa");
                IJwtAlgorithm algorithm = new HMACSHA256Algorithm();//加密方式
                IJsonSerializer serializer = new JsonNetSerializer();//序列化Json
                IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder();//base64加解密
    
                IJwtEncoder encoder = new JwtEncoder(algorithm, serializer, urlEncoder);//JWT编码    
    
                var payload = new Dictionary<string, object>() {
                    { "sub","121212"},
                    { "name","胜多负少多送点"},
                    { "uid","587345"},
                    { "exp",DateTime.Now.AddDays(1)},
                };
    
                var token = encoder.Encode(payload, key);//生成令牌
                return token;

    第三部编写  基于 AuthorizeAttribute 的请求筛选

      public class ApiAuthorizeAttribute : AuthorizeAttribute
        {
            protected override bool IsAuthorized(HttpActionContext actionContext)
            {
    
                var authHeader = from h in actionContext.Request.Headers where h.Key == "token" select h.Value.FirstOrDefault();
                byte[] key = Encoding.UTF8.GetBytes("123456789aaaaaaa");
                IJwtAlgorithm algorithm = new HMACSHA256Algorithm();//加密方式
                IJsonSerializer serializer = new JsonNetSerializer();//序列化Json
                IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder();//base64加解密
                IDateTimeProvider provider1 = new UtcDateTimeProvider();
                IJwtValidator validator1 = new JwtValidator(serializer, provider1);
                JwtDecoder jwtDecoder = new JwtDecoder(serializer, validator1, urlEncoder);
                //解密,在这里可以做互斥登录、或者做token 有效性验证
                var jwt = jwtDecoder.Decode(authHeader.Single());
                return false;
                //  return base.IsAuthorized(actionContext);
            }
    
            protected override void HandleUnauthorizedRequest(HttpActionContext filterContext)
            {
                base.HandleUnauthorizedRequest(filterContext);
    
                var response = filterContext.Response = filterContext.Response ?? new HttpResponseMessage();
                response.StatusCode = HttpStatusCode.Forbidden;
                var content = new
                {
                    success = false,
                    errs = new[] { "您暂无权限" }
                };
                response.Content = new StringContent(Json.Encode(content), Encoding.UTF8, "application/json");
            }
        }

    使用的时候只需要在  Controller 或者 action 上加上特性 [ApiAuthorize] 不需要验证的增加特性 [AllowAnonymous]

  • 相关阅读:
    Android NDK学习(1) 简介
    wmsys.wm_concat结果长度限制的问题
    onInterceptTouchEvent和onTouchEvent调用时序
    滑动到底部或顶部响应的ScrollView实现
    Android ViewPager使用详解
    android include标签的使用,在RelativeLayout中使用include标签需注意!!!!!
    Eclipse中如何在指定工程中搜索指定的字符串
    android:windowSoftInputMode属性详解
    cocos2d-x中关于touch事件的响应
    《从零开始学Swift》学习笔记(Day 6)——哎呀常量和变量都该什么时候用啊?
  • 原文地址:https://www.cnblogs.com/sxmny/p/10757337.html
Copyright © 2011-2022 走看看