zoukankan      html  css  js  c++  java
  • ApiController实现自定义身份认证

     1 /// <summary>
     2     /// 身份认证
     3     /// </summary>
     4     public class AuthAttribute : AuthorizationFilterAttribute
     5     {
     6         /// <summary>
     7         /// 重写认证过程
     8         /// </summary>
     9         /// <param name="actionContext"></param>
    10         public override void OnAuthorization(HttpActionContext actionContext)
    11         {
    12             HttpResponseMessage message = new HttpResponseMessage();
    13             message.StatusCode = System.Net.HttpStatusCode.OK;
    14 
    15             var TokenQuery = actionContext.Request.GetQueryNameValuePairs().ToList().Where(m => m.Key == "api_key").FirstOrDefault();
    16             if (string.IsNullOrEmpty(TokenQuery.Value))
    17             {
    18                 string json = JsonConvert.SerializeObject(new ReturnModel() { RetCode = 200, Data = { }, Msg = "token认证失败" });
    19                 StringContent Content = new StringContent(json, Encoding.GetEncoding("UTF-8"), "application/json");
    20                 message.Content = Content;
    21 
    22                 actionContext.Response = message;
    23                 return;
    24             }
    25             //数据库查询方法
    26             //...
    27         }
    28     }
  • 相关阅读:
    矩阵乘法
    年关了,抛一个模拟ip刷票的php程序
    mysql处理大数据合并的另一种方法
    php之aop实践
    轻量级php框架phpk v1.0发布
    eclipse
    super关键字的使用
    方法的重写
    方法的重载
    java 构造器 (构造方法)
  • 原文地址:https://www.cnblogs.com/tian2008/p/7766935.html
Copyright © 2011-2022 走看看