架构师_程序员_码农网

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

搜索
查看: 209385|回复: 102

[WebAPI] webapi通用签名的验证(二)

  [复制链接]
发表于 2017-6-26 15:49:16 | 显示全部楼层 |阅读模式
对Web API签名的封装,验证时间戳随机数密钥等
http://www.itsvse.com/thread-3274-1-1.html
(出处: 架构师)
上面是以前写的,有点bug,下面是新版的通用验证方案

  1. #region -   版   权   信   息  -
  2.         //======================================================
  3.         //
  4.         //      创 建 人:小渣渣
  5.         //      创建时间:2017/06/26 15:39:31
  6.         //      邮    箱:help@itsvse.com
  7.         //      个人网站:http://www.itsvse.com
  8.         //      功    能:
  9.         //      修改纪录:
  10.         //
  11.         //======================================================
  12.         #endregion

  13.         public class AuthFilterAttribute : ActionFilterAttribute
  14.         {
  15.                 private const string Origin = "Origin";
  16.                 private const string AccessControlAllowOrigin = "Access-Control-Allow-Origin";
  17.                 private const string originHeaderdefault = "*";
  18.                 /// <summary>
  19.                 /// 签名
  20.                 /// </summary>
  21.                 private static string[] sign = { "nonce", "timestamp", "signature" };

  22.                 /// <summary>
  23.                 /// 走完api之后
  24.                 /// </summary>
  25.                 /// <param name="actionExecutedContext"></param>
  26.                 public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
  27.                 {
  28.                         actionExecutedContext.Response.Headers.Add(AccessControlAllowOrigin, originHeaderdefault);
  29.                 }

  30.                 /// <summary>
  31.         /// 进api之前
  32.         /// </summary>
  33.         /// <param name="actionContext"></param>
  34.                 public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
  35.                 {

  36.                         try
  37.                         {
  38.                                 HttpContextBase context = (HttpContextBase)actionContext.Request.Properties["MS_HttpContext"];
  39.                                 #region 判断签名必要的参数
  40.                                 foreach (var item in sign)
  41.                                 {
  42.                                         if (!context.Request.QueryString.AllKeys.Any(x => x.Equals(item, StringComparison.OrdinalIgnoreCase)))
  43.                                         {
  44.                                                 //不包含
  45.                                                 ReturnModel model = new ReturnModel()
  46.                                                 {
  47.                                                         result = false,
  48.                                                         code = 15,
  49.                                                         message = "Missing " + item
  50.                                                 };
  51.                                                 actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.OK, model);
  52.                                                 return;
  53.                                         }
  54.                                 }
  55.                                 #endregion
  56.                                 #region 判断时间戳
  57.                                 long time = long.Parse(context.Request.QueryString["timestamp"]);
  58.                                 bool result = Tools.TimeHelp.IsTime(time, Convert.ToDouble(ConfigurationManager.AppSettings["TimestampInterval"]));
  59.                                 if (!result)
  60.                                 {
  61.                                         //时间戳不正确
  62.                                         ReturnModel model = new ReturnModel()
  63.                                         {
  64.                                                 result = false,
  65.                                                 code = 15,
  66.                                                 message = "Time is not right!"
  67.                                         };
  68.                                         actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.OK, model);
  69.                                         return;
  70.                                 }
  71.                                 #endregion
  72.                                 #region 签名
  73.                                 SortedDictionary<string, string> dic_sign = new SortedDictionary<string, string>();
  74.                                 foreach (var item in context.Request.QueryString.AllKeys)
  75.                                 {
  76.                                         if (!item.Equals("signature", StringComparison.OrdinalIgnoreCase))
  77.                                         {
  78.                                                 dic_sign.Add(item, context.Request.QueryString[item]);
  79.                                         }
  80.                                 }
  81.                                 bool resultsign = Tools.SignHelper.Sign(ConfigurationManager.AppSettings["secretkey"], context.Request.QueryString["signature"], dic_sign);
  82.                                 if (!resultsign)
  83.                                 {
  84.                                         //签名不正确
  85.                                         ReturnModel model = new ReturnModel()
  86.                                         {
  87.                                                 result = false,
  88.                                                 code = 15,
  89.                                                 message = "The signature is not correct!"
  90.                                         };
  91.                                         actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.OK, model);
  92.                                         return;
  93.                                 }
  94.                                 #endregion
  95.                         }
  96.                         catch (Exception ex)
  97.                         {
  98.                                 ReturnModel model = new ReturnModel()
  99.                                 {
  100.                                         result = false,
  101.                                         code = 15,
  102.                                         message = "Exception " + ex
  103.                                 };
  104.                                 actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.OK, model);
  105.                         }
  106.                 }
  107.         }
复制代码

返回的model模型:

  1. /// <summary>
  2.         /// 统一的返回模型
  3.         /// </summary>
  4.         [Serializable]
  5.         [DataContract]
  6.         public class ReturnModel
  7.         {
  8.                 [DataMember]
  9.                 public bool result { get; set; }

  10.                 [DataMember]
  11.                 public int code { get; set; }

  12.                 [DataMember]
  13.                 public string message { get; set; }

  14.                 [DataMember]
  15.                 public object data { get; set; }

  16.         }
复制代码



在请求的接口,要求客户端必须携带时间戳、随机数、签名在url参数里面

签名判断方法如下:

游客,如果您要查看本帖隐藏内容请回复






上一篇:webapi ModelState.IsValid模型验证
下一篇:C# task.waitall会卡死界面
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
发表于 2018-2-26 17:33:35 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
 楼主| 发表于 2017-6-26 16:18:58 | 显示全部楼层
接口地址:http://ip:port/api/XXX/XXX

请求方式:POST

签名方式:url参数必须带nonce、timestamp、signature

signature的值:url参数a-Z排序(排除signature)拼接value值,最后加上secretkey,然后md5
secretkey为123456


成功返回:
{
    "result": true,
    "message": "ok",
    "code": 200
}

错误返回:
{
    "result": false,
    "message": "XXX属性是必需的。",
    "code": 200
}

码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
发表于 2018-7-26 13:39:35 | 显示全部楼层
GetDateTimeFrom1970Ticks 里面有点问题:return dtStart.AddMilliseconds(curSeconds); 原来是return dtStart.Addseconds(curSeconds);经查证,curSeconds应该是毫秒级的,所以改成加毫秒就对了。
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
发表于 2017-6-27 11:15:33 | 显示全部楼层
我要看隐藏内容~!!!!
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
发表于 2017-7-12 16:56:42 | 显示全部楼层
初学者,学习学习安全验证
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
发表于 2017-7-16 23:52:03 | 显示全部楼层

我要看隐藏的代码
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
发表于 2017-7-17 16:28:45 | 显示全部楼层
想看签名方法
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
发表于 2017-7-24 10:38:57 | 显示全部楼层
阿斯顿发阿发发送到
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
发表于 2017-8-11 23:17:20 | 显示全部楼层
不错不错,学习了
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
发表于 2017-8-19 08:02:20 | 显示全部楼层
膜拜中,最近在搞webapi 接口安全认证
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
发表于 2017-9-1 13:13:21 | 显示全部楼层
回复看看!
- 本文出自架构师,原文地址:https://www.itsvse.com/thread-3653-1-1.html
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

免责声明:
码农网所发布的一切软件、编程资料或者文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To:help@itsvse.com

QQ|手机版|小黑屋|架构师 ( 鲁ICP备14021824号-2 )|网站地图

GMT+8, 2025-1-17 02:45

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表