微信公众号token的asp.net脚本

老板让我搞一个微信公众号。好吧。前面都很EZ,直到要使用一个token验证服务器的有效性。

看了下文档,大概意思就是微信的服务器用GET请求访问你的服务器。

其中包含了signature,nonce,timestamp,echostr四组参数

你所要做的,是把nonce,timestamp,token这三个参数按字典序排序。其中token为开发者自行定义。

然后将其合并成一个字符串,假定为str。

在求str的sha1加密的值。如果该sha1值与signature值相同,则返回echostr。

一开始用JavaScript,死活通不过,原来你妈海JavaScript是客户端脚本语言,它返回给微信的是整个代码。

后来改用asp.net,一开始也通不过,原来在返回的页面里包含了<html>的标记。微信的要求是只返回那个echostr

改掉后 成功了。下面给出代码

<%
 

    Dim signatrue = Request.QueryString("signature")
    Dim nonce = Request.QueryString("nonce")
    Dim echostr = Request.QueryString("echostr")
    Dim timestamp = Request.QueryString("timestamp")
    Dim token = "weixin"
    Dim wlh(3)
    wlh(0) = nonce
    wlh(1) = token
    wlh(2) = timestamp
    wlh.Sort(wlh)
    Dim all = ""
    Dim i = 0
    For i = 0 To 3
        all = all + wlh(i)
    Next
   
    Dim sha1result = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(all, "SHA1")
    If (signatrue.ToString.ToUpper.Equals(sha1result)) Then
          
        Response.Write(echostr)
    End If  
%>

  

原文地址:https://www.cnblogs.com/elnino/p/5643921.html