首先大家可以看到微信会发送一个get请求到我们自己的服务器上,所以我们首先需要写一个接口,接口需要接收者四个参数。
public string CheckToken(string signature, string timestamp, string nonce, string echostr)
{
ArrayList AL = new ArrayList();
AL.Add(sToken); //自己在基本配置上面的token
AL.Add(timestamp); //微信请求给的时间戳
AL.Add(sNonce); //微信给的随机数
AL.Sort(new DictionarySort());
string raw = "";
for (int i = 0; i < AL.Count; ++i)
raw += AL[i];
SHA1 sha;
ASCIIEncoding enc;
string hash = "";
try
{
//使用sha1进行加密
sha = new SHA1CryptoServiceProvider();
enc = new ASCIIEncoding();
byte[] dataToHash = enc.GetBytes(raw);
byte[] dataHashed = sha.ComputeHash(dataToHash);
hash = BitConverter.ToString(dataHashed).Replace("-", "");
hash= hash.ToLower();
}
catch (Exception)
{
throw new Exception("加密错误!");
}
}
然后加密后,我们可以得到上方代码的hash,这个 hash 变量其实就是微信给我们的 signature(签名)。然后我们需要进行对比是否与微信发送过来的一致,
if (hash == signature)
return echostr;
return echostr;
如果一致的话就返回微信给我们的那串随机字符串(echostr)。
这样子就ok了!!!
注意加密的token一定要和自己在基本配置的一致!!!
然后如果实在是不想这么麻烦,那么就降低要求!!!直接将微信给的 echostr(随机字符串),
给他扔回去,也是会成功的!!!但是还是建议自己验证一下。
然后你的url就直接写你接口的地址就ok了,
然后如果你需要申请测试号的话那个url填写也是一样的!!!