Md5 校验

md5在线加密工具网站:http://md5.chahuo.com/

1.Controller 继承自 API 基类,从而调用 Md5 校验。

/// 
    /// 人员上传位置定位
    /// 
    [EnableCors(origins: "*", headers: "*", methods: "*")]
    [RoutePrefix("api/App/PersonMap")]
    public class PersonMapController : BaseApiController
    {

2.Api 基类使用自定义注解 [CustomActionFilter], 注解中包含Md5校验方法。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Http.Cors;

namespace YC.WebAPI.Controllers.BMS
{
    /// 
    /// API基类
    /// 
    [CustomActionFilter]
    [EnableCors(origins: "*", headers: "*", methods: "*")]
    public class BaseApiController : ApiController
    {

    }
}

3.[CustomActionFilter] 注解中包含Md5校验:
MD5信息摘要算法,一种被广泛使用的密码散列函数,可以产生出一个128位(16字节)的散列值(hash value),用于确保信息传输完整一致。
Md5 是将时间戳进行特定转换为一个字符串,

using HY.CommDataOpr;
using Newtonsoft.Json;
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;

namespace YC.WebAPI.Controllers.BMS
{
    /// 
    /// 自定义方法属性,登录判断
    /// 
    public class CustomActionFilterAttribute : ActionFilterAttribute
    {
        /// 
        /// 重写方法执行前
        /// 
        /// 
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            var request = actionContext.Request;
            string accToken = "";
            string s = "";
            var tips = new TipsModel<string>();
            if (!(request.Headers.Contains("timeMd5")))
            {
                tips.SetError("timeMd5丢失!");
                tips.ReturnCode = "200";
                actionContext.Response = new HttpResponseMessage(HttpStatusCode.NonAuthoritativeInformation) { Content = new StringContent(JsonConvert.SerializeObject(tips)) };
                return;
            }
            if (!request.Headers.Contains("timestamp"))
            {
                tips.SetError("timestamp丢失!");
                tips.ReturnCode = "200";
                actionContext.Response = new HttpResponseMessage(HttpStatusCode.NonAuthoritativeInformation) { Content = new StringContent(JsonConvert.SerializeObject(tips)) };
                return;
            }
            accToken = request.Headers.GetValues("timeMd5").FirstOrDefault();
            //可以规定一个字符串作为密钥
            string timestamp = "240's CSDN" + request.Headers.GetValues("timestamp").FirstOrDefault();
            s = GetMD5Str(timestamp);
            if (accToken != s)
            {
                tips.SetError("Md5校验错误!");
                tips.ReturnCode = "200";
                actionContext.Response = new HttpResponseMessage(HttpStatusCode.NonAuthoritativeInformation) { Content = new StringContent(JsonConvert.SerializeObject(tips)) };
                return;
            }
        }

        /// 
        /// MD5字符串
        /// 
        /// 参数数组
        /// MD5字符串
        public string GetMD5Str(params string[] paras)
        {
            string str = "";
            for (int i = 0; i < paras.Length; i++)
            {
                str += paras[i];
            }
            byte[] buffer = MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(str));
            string md5Str = string.Empty;
            for (int i = 0; i < buffer.Length; i++)
            {
                md5Str = md5Str + buffer[i].ToString("X2");
            }
            return md5Str;
        }
    }
}

4.运行 WebApi 后调用接口如下:

Md5 校验_第1张图片

你可能感兴趣的:(C#,c#,开发语言)