From 9c1cce3505f334de6dffcae3cb1f7367b103ea8b Mon Sep 17 00:00:00 2001 From: "DESKTOP-M4V1LH5\\Jin" Date: Mon, 3 Dec 2018 16:16:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/api/ArticleController.cs | 8 +- COMCMS.Web/Controllers/api/IndexController.cs | 12 ++- .../Controllers/api/PaymentController.cs | 2 +- .../Controllers/api/ProductController.cs | 4 +- COMCMS.Web/Controllers/api/UserController.cs | 4 +- COMCMS.Web/Filter/CheckFilter.cs | 74 +++++++++++++------ 6 files changed, 73 insertions(+), 31 deletions(-) diff --git a/COMCMS.Web/Controllers/api/ArticleController.cs b/COMCMS.Web/Controllers/api/ArticleController.cs index 3f4c115..262077c 100644 --- a/COMCMS.Web/Controllers/api/ArticleController.cs +++ b/COMCMS.Web/Controllers/api/ArticleController.cs @@ -30,7 +30,7 @@ namespace COMCMS.Web.Controllers.api /// [HttpGet] [CheckFilter] - public object GetArticleList(int kid, int pagesize = 10, int page = 1, string random = "", string timeStamp = "", string signature = "") + public object GetArticleList(int kid, int pagesize = 10, int page = 1) { var where = Article._.IsHide != 1; if (kid > 0) @@ -83,7 +83,7 @@ namespace COMCMS.Web.Controllers.api /// [HttpGet] [CheckFilter] - public object GetArticleListWidthSub(int kid, int pagesize = 10, int page = 1, string key = "", string random = "", string timeStamp = "", string signature = "") + public object GetArticleListWidthSub(int kid, int pagesize = 10, int page = 1, string key = "") { var where = Article._.IsHide != 1; @@ -154,7 +154,7 @@ namespace COMCMS.Web.Controllers.api /// [HttpGet] [CheckFilter] - public object GetArticleDetail(int id, string random = "", string timeStamp = "", string signature = "") + public object GetArticleDetail(int id) { //获取商品 Article entity = Article.FindById(id); @@ -173,7 +173,7 @@ namespace COMCMS.Web.Controllers.api #region 获取文章栏目详情,并带下一级栏目 [HttpGet] [CheckFilter] - public object GetArticleCategory(int id, string random = "", string timeStamp = "", string signature = "") + public object GetArticleCategory(int id) { ArticleCategory entity = ArticleCategory.FindById(id); if (entity == null) diff --git a/COMCMS.Web/Controllers/api/IndexController.cs b/COMCMS.Web/Controllers/api/IndexController.cs index 2b65044..903de8e 100644 --- a/COMCMS.Web/Controllers/api/IndexController.cs +++ b/COMCMS.Web/Controllers/api/IndexController.cs @@ -20,6 +20,7 @@ namespace COMCMS.Web.Controllers.api { // GET: api/ [HttpGet] + [CheckFilter] public object Get() { Config cfg = Config.GetSystemConfig(); @@ -37,10 +38,19 @@ namespace COMCMS.Web.Controllers.api /// [HttpGet] [CheckFilter] - public ReJson CheckSign(string id, string random = "", string timeStamp = "", string signature = "") + public ReJson CheckSign(string id) { return new ReJson("验签成功!"); } + + //POST验证 + [HttpPost] + [CheckFilter] + public ReJson ChekSignWidhPost(int id) + { + return new ReJson("验证通过!"); + } + #endregion //#region 测试出错 diff --git a/COMCMS.Web/Controllers/api/PaymentController.cs b/COMCMS.Web/Controllers/api/PaymentController.cs index e0c2408..38fa5cd 100644 --- a/COMCMS.Web/Controllers/api/PaymentController.cs +++ b/COMCMS.Web/Controllers/api/PaymentController.cs @@ -22,7 +22,7 @@ namespace COMCMS.Web.Controllers.api #region 微信小程序订单支付 [HttpGet] [CheckFilter] - public ReJson DoPayOrder(string ordernum, string random = "", string timeStamp = "", string signature = "") + public ReJson DoPayOrder(string ordernum) { //获取订单 Order entity = Order.Find(Order._.OrderNum == ordernum); diff --git a/COMCMS.Web/Controllers/api/ProductController.cs b/COMCMS.Web/Controllers/api/ProductController.cs index 322343f..e2df8fe 100644 --- a/COMCMS.Web/Controllers/api/ProductController.cs +++ b/COMCMS.Web/Controllers/api/ProductController.cs @@ -34,7 +34,7 @@ namespace COMCMS.Web.Controllers.api /// [HttpGet] [CheckFilter] - public object GetCategories(int level, int pid = 0, string random = "", string timeStamp = "", string signature = "") + public object GetCategories(int level, int pid = 0) { var list = Category.GetListTree(pid, level, false, false); @@ -74,7 +74,7 @@ namespace COMCMS.Web.Controllers.api /// [HttpGet] [CheckFilter] - public object GetProductList(int kid, int page, int pageSize = 10, string random = "", string timeStamp = "", string signature = "") + public object GetProductList(int kid, int page, int pageSize = 10) { var where = Product._.IsHide == 0; if (kid > 0) diff --git a/COMCMS.Web/Controllers/api/UserController.cs b/COMCMS.Web/Controllers/api/UserController.cs index 9f4482c..af06165 100644 --- a/COMCMS.Web/Controllers/api/UserController.cs +++ b/COMCMS.Web/Controllers/api/UserController.cs @@ -31,7 +31,7 @@ namespace COMCMS.Web.Controllers.api /// [HttpGet] [CheckFilter] - public ReJson UserLogin(string code, string random = "", string timeStamp = "", string signature = "") + public ReJson UserLogin(string code) { if (string.IsNullOrEmpty(code)) { @@ -141,7 +141,7 @@ namespace COMCMS.Web.Controllers.api /// /// [HttpGet] - public ReJson SyncWeixinInfo(string sessionkey, string userInfo, int inviteid = 0, string random = "", string timeStamp = "", string signature = "") + public ReJson SyncWeixinInfo(string sessionkey, string userInfo, int inviteid = 0) { if (string.IsNullOrEmpty(sessionkey)) { diff --git a/COMCMS.Web/Filter/CheckFilter.cs b/COMCMS.Web/Filter/CheckFilter.cs index 4996375..738175d 100644 --- a/COMCMS.Web/Filter/CheckFilter.cs +++ b/COMCMS.Web/Filter/CheckFilter.cs @@ -9,8 +9,6 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; - - namespace COMCMS.Web.Filter { public class CheckFilterAttribute : ActionFilterAttribute @@ -33,7 +31,13 @@ namespace COMCMS.Web.Filter } } - throw new Exception(error); + ReJson re = new ReJson(40000, error); + context.Result = new ContentResult() + { + Content = re.ToJson(), + ContentType = "application/json" + }; + //throw new Exception(error); } var notVali = ValiSignature(context.HttpContext); @@ -49,35 +53,60 @@ namespace COMCMS.Web.Filter } } + #region 详细验证 private ReJson ValiSignature(HttpContext context) { var Request = context.Request; - - var queryStrings = Request.Query; - List keys = new List(); - //string[] keys = HttpContext.Current.Request.QueryString.AllKeys; - if (queryStrings == null) - { - throw new Exception("queryStrings Is Null"); - } - foreach (var item in queryStrings.Keys) - { - keys.Add(item); - } SortedDictionary pars = new SortedDictionary(); string signature = ""; - foreach (var k in keys) + string method = Request.Method.ToUpper(); + if (method == "GET") { - if (k != "signature") + var queryStrings = Request.Query; + List keys = new List(); + //string[] keys = HttpContext.Current.Request.QueryString.AllKeys; + if (queryStrings == null) { - string v = Request.Query[k]; - pars.Add(k, v); + return new ReJson(40000, "参数错误!"); } - else + foreach (var item in queryStrings.Keys) { - signature = Request.Query[k]; + keys.Add(item); + } + foreach (var k in keys) + { + if (k != "signature") + { + string v = Request.Query[k]; + pars.Add(k, v); + } + else + { + signature = Request.Query[k]; + } } } + else if(method == "POST") + { + var stream = context.Request.Form; + foreach (var item in stream) + { + if (item.Key != "signature") + { + pars.Add(item.Key, item.Value); + } + else + { + signature = item.Value; + } + } + } + else + { + return new ReJson(50000, "不支持的HTTP方法!"); + } + + //没有签名返回错误 if (string.IsNullOrEmpty(signature)) return new ReJson(40004, "signature 为空!"); @@ -98,5 +127,8 @@ namespace COMCMS.Web.Filter return null; } + #endregion + + } }