微信验证Token

public ContentResult Token()
        {
            string token = "wxtest";// "wxtest";//输入你上面自己编写的Token

            //取到Token接收到的值
            string echoString = Request.QueryString["echoStr"];
            string signature = Request.QueryString["signature"];
            string timestamp = Request.QueryString["timestamp"];
            string nonce = Request.QueryString["nonce"];
            if (CheckSignatureT(token, signature, timestamp, nonce)) //判断验证是否正确
            {
                if (!string.IsNullOrEmpty(echoString)) //正确返回微信服务器
                {
                    return Content(echoString);
                }
            }
            return Content("");
        }

        public static bool CheckSignatureT(string token, string signature, string timestamp, string nonce)
        {
            string[] ArrTmp = { token, timestamp, nonce };
            //字典排序
            Array.Sort(ArrTmp);
            //拼接
            string tmpStr = string.Join("", ArrTmp);
            //sha1验证
            tmpStr = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1");
            //tmpStr = Membership.CreateUser(tmpStr, "SHA1");
            tmpStr = tmpStr.ToLower();
            if (tmpStr == signature)  //如果计算后得到的数值与传过来的数值相等
            {
                return true;   //返回正确
            }
            else
            {
                return false; //不相等  返回错误
            }
        }


猜你喜欢

转载自blog.51cto.com/3002711/2671622