一、前提條件,要有一台伺服器
請填寫接口配置資訊,此資訊需要你有自己的伺服器資源,填寫的
URL
需要正确響應微信發送的
Token
驗證,請閱讀消息接口使用指南。

二、C#代碼示例
但是上面連結中的代碼是
PHP
示例代碼,是以我整理了一份
C#
版的,親測能用。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WXtoken
{
public partial class index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//驗證token
string poststring = string.Empty;
string token = "aabbcc"; //驗證token,随意填寫
if (string.IsNullOrEmpty(token))
{
return;
}
string echostr = 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, nonce))
{
HttpContext.Current.Response.Write(echostr);
HttpContext.Current.Response.End();
}
}
#region 微信接口對接驗證代碼
public bool checkSignature(string token, string signature, string timestamp, string nonce, string echostr)
{
List<string> list = new List<string>();
list.Add(token);
list.Add(timestamp);
list.Add(nonce);
list.Sort();
string res = string.Join("", list.ToArray());
Byte[] dataToHash = Encoding.ASCII.GetBytes(res);
byte[] hashvalue = ((HashAlgorithm)CryptoConfig.CreateFromName("SHA1")).ComputeHash(dataToHash);
StringBuilder sb = new StringBuilder();
foreach (byte b in hashvalue)
{
sb.Append(b.ToString("x2"));
}
if (signature == sb.ToString())
return true;
else
return false;
}
#endregion
}
}
三、配置失敗原因整理:
- 端口錯誤,必須是80或者443端口
- 參數錯誤,建議從官方文檔複制參數到項目中(我就是有兩個字母寫反了,尴尬)
- 位址或者Token錯誤
四、文章下載下傳:
點選下載下傳MarkDown壓縮包