配置公衆號token

第一步:點左邊【開發】——【基本配置】

 

第二步:修改配置:

第三步:C#我用的是一般處理程序(代碼轉自網上,改動了裏面的一個地方)

轉載URL:https://q.cnblogs.com/q/106980/

<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;

public class Handler : IHttpHandler {

    public void ProcessRequest(HttpContext context)
    {
        string WeiXinToken = "1234";//要和你微信公衆平臺設置的保持一致

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

        if (CheckSignature(WeiXinToken, signature, timestamp, nonce))
        {
            if (!string.IsNullOrEmpty(echoString))
            {
                context.Response.Write(echoString);
                context.Response.End();
            }
        }
    }

    /// <summary>
    /// 驗證微信簽名
    /// </summary>
    public static bool CheckSignature(string token, string signature, string timestamp, string nonce)
    {
        string[] ArrTmp = { token, timestamp, nonce };

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

        tmpStr = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1");
        //tmpStr = Membership.CreateUser(tmpStr, "SHA1");
        tmpStr = tmpStr.ToLower();

        if (tmpStr == signature)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public bool IsReusable
    {
        get {
            return false;
        }
    }

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章