• 微信token验证源码分享(c#版)


    在开发时遇到一个问题:

      上线后提交申请微信提示"您的服务器没有正确响应token验证。。。",我查看日志发现根本就没有接收到来自微信的参数。

    后来我又记录了微信请求方式和请求的字符串,想看看微信服务器到底有没有给我的服务器响应请求。结果是有的。并且通过了。

    代码就添加了Request.HttpMethod和Request.QueryString没变,但不晓得怎么回事。

     /// <summary> 按照api说明对signature进行校验,校验成功返回参数echostr </summary>
            /// <returns></returns>
            public string CheckSign()
            {
                var httpMethod = Request.HttpMethod.ToLower();
                string httpString = string.Empty;
                if (httpMethod == "get")
                {
                    httpString = Request.QueryString.ToString();
                }
                else if (httpMethod == "post")
                {
                    httpMethod = Request.Form.ToString();
                }
                else
                {
                    httpMethod = "请求方式不是get和post";
                }
                var strSignature = Request["signature"];
                var strEchostr = Request["echostr"];
                var strToken = "58jiancai";
                var strTimestamp = Request["timestamp"];
                var strNonce = Request["nonce"];
    
                log4net.LogManager.GetLogger("请求方式").Info(httpMethod);
                log4net.LogManager.GetLogger("请求字符串").Info(httpString);
                log4net.LogManager.GetLogger("pram1.strSignature").Info(strSignature);
                log4net.LogManager.GetLogger("pram2.strEchostr").Info(strEchostr);
                log4net.LogManager.GetLogger("pram3.strToken").Info(strToken);
                log4net.LogManager.GetLogger("pram4.strTimestamp").Info(strTimestamp);
                log4net.LogManager.GetLogger("pram5.strNonce").Info(strNonce);
    
                //step1:字典序排序
                string[] array = new[] { strToken, strTimestamp, strNonce };
                Array.Sort(array);
                log4net.LogManager.GetLogger("sort").Info(array[0] + "||" + array[1] + "||" + array[2]);
    
                //step2:sha1加密
                var strResult = FormsAuthentication.HashPasswordForStoringInConfigFile(string.Concat(array), "SHA1").ToLower();
                log4net.LogManager.GetLogger("sha1").Info(strResult);
    
                //step3:加密后的字符串与参数signature值比较
                if (strResult == strSignature.ToLower())
                {
                    log4net.LogManager.GetLogger("result").Info("success");
                    return strEchostr;
                }
                log4net.LogManager.GetLogger("result").Info("fail");
                return string.Empty;
            }
  • 相关阅读:
    KALI LINUX 核心概念讲解,持续更新
    KALI LINUX 工具大全之密码破解 --- BruteSpray ( 暴力喷雾 )
    android studio的 gradle 依赖同步错误解决方法
    安卓逆向的初步研究--从某恋app入手
    nc浏览器的十宗罪
    手机重要文件目录(换新机可能要用到)
    国产手机的谷X服务
    安卓手机设置的那些琐事
    办公中遇见的那些问题
    装系统遇到的那些问题
  • 原文地址:https://www.cnblogs.com/paulhe/p/3875127.html
Copyright © 2020-2023  润新知