码迷,mamicode.com
首页 > Windows程序 > 详细

.net core webapi使用nginx实现集群和负载均衡

时间:2018-12-22 15:00:48      阅读:853      评论:0      收藏:0      [点我收藏+]

标签:get   copy   str   技术分享   代码   第一步   dht   secret   none   

第一步:先编写webapi接口:

  技术分享图片

  接口介绍:

    1、接口采用appkey和appsecret

    2、访问的话,在报文头加上,appkey和sign。

      1、sign由请求地址(例如:http://www.xxx.com/api/user/xx/,那么地址是/api/user/xx/)+appsecret+queryString或者报文体使用SHA1散列算法得出。

  Filter代码:

技术分享图片
 1 string appKey = "fjdsakljg";
 2         string appSecret = "dfhdjsklahfkdsbgkfdhtu";
 3 
 4         public void OnAuthorization(AuthorizationFilterContext context)
 5         {
 6             string method = context.HttpContext.Request.Method.ToLower();
 7             string path = context.HttpContext.Request.Path.Value;
 8             string thSign = "";
 9             StringValues appkeys;
10             StringValues signs;
11             if (!context.HttpContext.Request.Headers.TryGetValue("appKey", out appkeys))
12             {
13                 context.Result = new JsonResult(new APIResult<int> { Code = 1, Message = "appkey不存在" });
14                 return;
15             }
16             if (!context.HttpContext.Request.Headers.TryGetValue("sign", out signs))
17             {
18                 context.Result = new JsonResult(new APIResult<int> { Code = 1, Message = "sign不存在" });
19                 return;
20             }
21             string sign = signs.First();
22             string key = appkeys.First();
23             if (key != appKey)
24             {
25                 context.Result = new JsonResult(new APIResult<int> { Code = 1, Message = "appkey错误" });
26                 return;
27             }
28             if (method == "get" || method == "delete")
29             {
30                 IQueryCollection reqUrls = context.HttpContext.Request.Query;
31                 StringBuilder sb = new StringBuilder();
32                 var querys = reqUrls.OrderBy(k => k.Key).Select(e => e.Key + "=" + e.Value);
33                 string query = string.Join("&", querys);
34                 thSign = CaclSHA(path + appSecret + query);
35             }
36             else
37             {
38                 IQueryCollection reqUrls = context.HttpContext.Request.Query;
39                 StringBuilder sb = new StringBuilder();
40                 var querys = reqUrls.OrderBy(k => k.Key).Select(e => e.Key + "=" + e.Value);
41                 string query = string.Join("&", querys);
42                 //读取报文体
43                 var requestBodyStream = new MemoryStream();
44                 context.HttpContext.Request.Body.CopyTo(requestBodyStream);
45                 requestBodyStream.Seek(0, SeekOrigin.Begin);
46                 string body = new StreamReader(requestBodyStream, Encoding.UTF8).ReadToEnd();
47                 requestBodyStream.Seek(0, SeekOrigin.Begin);
48                 context.HttpContext.Request.Body = requestBodyStream;
49                 thSign = CaclSHA(path+appSecret+body);
50             }
51             if (!sign.Equals(thSign,StringComparison.InvariantCultureIgnoreCase))
52             {
53                 context.Result = new JsonResult(new APIResult<int> { Code = 1, Message = "sign错误" });
54                 return;
55             }
56         }
View Code

2、编写SDK包:

  技术分享图片

  技术分享图片

3、剩下就是部署了,我暂且拿虚拟机来测试

  在ubuntu 16.04装上nginx

  然后修改nginx的配置:

    技术分享图片

然后启动。访问就行了:

技术分享图片

 

.net core webapi使用nginx实现集群和负载均衡

标签:get   copy   str   技术分享   代码   第一步   dht   secret   none   

原文地址:https://www.cnblogs.com/norain/p/10160871.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!