微信公众平台填写服务器配置提示“token验证失败”

时间:2022-04-02 04:48:57
配置URL:http://域名/wxapi.ashx
配置Token:123456
在mvc项目中添加一般处理程序代码如下:

public class wxapi : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Write("欢迎关注!");
            string postString = string.Empty;
            if (HttpContext.Current.Request.HttpMethod.ToUpper() == "POST")
            {
                using (Stream stream = HttpContext.Current.Request.InputStream)
                {
                    Byte[] postBytes = new Byte[stream.Length];
                    stream.Read(postBytes, 0, (Int32)stream.Length);
                    postString = Encoding.UTF8.GetString(postBytes);
                }

                if (!string.IsNullOrEmpty(postString))
                {
                    //Execute(postString);
                }
            }
            else
            {
                Auth(); //微信接入的测试
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// 成为开发者的第一步,验证并相应服务器的数据
        /// </summary>
        private void Auth()
        {
            //string token = ConfigurationManager.AppSettings["WeixinToken"];//从配置文件获取Token
            string token = "123456";
            if (string.IsNullOrEmpty(token))
            {
                //LogTextHelper.Error(string.Format("WeixinToken 配置项没有配置!"));
            }

            string echoString = HttpContext.Current.Request.QueryString["echoStr"];
            string signature = HttpContext.Current.Request.QueryString["signature"];
            string timestamp = HttpContext.Current.Request.QueryString["timestamp"];
            string nonce = HttpContext.Current.Request.QueryString["nonce"];

            if (CheckSignature(token, signature, timestamp, nonce))
            {
                if (!string.IsNullOrEmpty(echoString))
                {
                    HttpContext.Current.Response.Write(echoString);
                    HttpContext.Current.Response.End();
                }
            }
        }


        /// <summary>
        /// 验证微信签名
        /// </summary>
        public bool CheckSignature(string token, string signature, string timestamp, string nonce)
        {
            string[] ArrTmp = { token, timestamp, nonce };

            Array.Sort(ArrTmp);
            string tmpStr = string.Join("", ArrTmp);

            tmpStr = FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1");
            tmpStr = tmpStr.ToLower();

            if (tmpStr == signature)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }

6 个解决方案

#1


补充,用的是80端口

#2


可以的啊////

#3


引用 2 楼 u013685845 的回复:
可以的啊////



可是偏偏验证失败。。。。 微信公众平台填写服务器配置提示“token验证失败”

#4


不错!应该可以的

#5


要这样写:

        if (CheckSignature(token, signature, timestamp, nonce))
        {
            context.Response.Write(echoString);
        }
        else
        {
            context.Response.Write("验证失败:" + signature);
        }

#6


推荐看下我的博客专栏java微信公众平台开发:http://blog.csdn.net/column/details/13293.html
希望可以帮助到你 。 

#1


补充,用的是80端口

#2


可以的啊////

#3


引用 2 楼 u013685845 的回复:
可以的啊////



可是偏偏验证失败。。。。 微信公众平台填写服务器配置提示“token验证失败”

#4


不错!应该可以的

#5


要这样写:

        if (CheckSignature(token, signature, timestamp, nonce))
        {
            context.Response.Write(echoString);
        }
        else
        {
            context.Response.Write("验证失败:" + signature);
        }

#6


推荐看下我的博客专栏java微信公众平台开发:http://blog.csdn.net/column/details/13293.html
希望可以帮助到你 。