ApiController实现自定义身份认证
标签:rem pac ram air lte log mes author void
1 ///
2 /// 身份认证
3 ///
4 public class AuthAttribute : AuthorizationFilterAttribute
5 {
6 ///
7 /// 重写认证过程
8 ///
9 ///
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 }
ApiController实现自定义身份认证
标签:rem pac ram air lte log mes author void
原文地址:http://www.cnblogs.com/tian2008/p/7766935.html
评论