最近研究了下服務(wù)號(hào)的服務(wù)器配置和企業(yè)號(hào)的回調(diào)模式。真正實(shí)現(xiàn)完后,覺(jué)得很簡(jiǎn)單,但一開(kāi)始還是走了點(diǎn)彎路,所以寫(xiě)了個(gè)web程序,只用改下配置文件里的參數(shù)就可以直接用了。下面介紹下詳細(xì)的用法以及實(shí)現(xiàn)步驟。
本文原文地址:用c#開(kāi)發(fā)微信(1)服務(wù)號(hào)的服務(wù)器配置和企業(yè)號(hào)的回調(diào)模式 - url接入 (源碼下載)
http://yunpan.cn/cjeTSAKwUVmv9 訪問(wèn)密碼 7ab3
<appSettings>
<!--微信的Token-->
<add key="WeixinToken" value="dd"/>
<add key="AppId" value="wxdbddd2bc"/>
<add key="AppSecret" value="82f7ddd88e196"/>
<!--企業(yè)號(hào)配置信息-->
<add key="CorpToken" value="fddd"/>
<add key="CorpId" value="wx1156d982ddda8"/>
<add key="EncodingAESKey" value="aNvJOkGYddyGwf5Rg"/>
</appSettings>
企業(yè)號(hào):
服務(wù)號(hào):
服務(wù)號(hào):
完整源碼:
/// <summary>
/// 處理微信服務(wù)器驗(yàn)證消息
/// </summary>
public void Auth()
{
string token = ConfigurationManager.AppSettings[Token].ToString();
string signature = HttpContext.Current.Request.QueryString["signature"];
string timestamp = HttpContext.Current.Request.QueryString["timestamp"];
string nonce = HttpContext.Current.Request.QueryString["nonce"];
string echostr = HttpContext.Current.Request.QueryString["echostr"];
if (HttpContext.Current.Request.HttpMethod.ToUpper() == "GET")
{
//get method - 僅在微信后臺(tái)填寫(xiě)URL驗(yàn)證時(shí)觸發(fā)
if (CheckSignature(signature, timestamp, nonce, token))
{
WriteContent(echostr); //返回隨機(jī)字符串則表示驗(yàn)證通過(guò)
}
else
{
WriteContent("failed:" + signature + "," + GetSignature(timestamp, nonce, token) + "。" +
"如果你在瀏覽器中看到這句話,說(shuō)明此地址可以被作為微信公眾賬號(hào)后臺(tái)的Url,請(qǐng)注意保持Token一致。");
}
HttpContext.Current.Response.End();
}
}
PRivate void WriteContent(string str)
{
HttpContext.Current.Response.Output.Write(str);
}
/// <summary>
/// 檢查簽名是否正確
/// </summary>
/// <param name="signature"></param>
/// <param name="timestamp"></param>
/// <param name="nonce"></param>
/// <param name="token"></param>
/// <returns></returns>
public static bool CheckSignature(string signature, string timestamp, string nonce, string token)
{
return signature == GetSignature(timestamp, nonce, token);
}
/// <summary>
/// 返回正確的簽名
/// </summary>
/// <param name="timestamp"></param>
/// <param name="nonce"></param>
/// <param name="token"></param>
/// <returns></returns>
public static string GetSignature(string timestamp, string nonce, string token)
{
string[] arr = new[] { token, timestamp, nonce }.OrderBy(z => z).ToArray();
string arrString = string.Join("", arr);
System.Security.Cryptography.SHA1 sha1 = System.Security.Cryptography.SHA1.Create();
byte[] sha1Arr = sha1.ComputeHash(Encoding.UTF8.GetBytes(arrString));
StringBuilder enText = new StringBuilder();
foreach (var b in sha1Arr)
{
enText.AppendFormat("{0:x2}", b);
}
return enText.ToString();
}
官方接入文檔: http://mp.weixin.QQ.com/wiki/17/2d4265491f12608cd170a95559800f2d.html
企業(yè)號(hào):
完整源碼:
public void ProcessRequest(HttpContext context)
{
string postString = string.Empty;
if (HttpContext.Current.Request.HttpMethod.ToUpper() == "GET")
{
Auth();
}
}
/// <summary>
/// 成為開(kāi)發(fā)者的第一步,驗(yàn)證并相應(yīng)服務(wù)器的數(shù)據(jù)
/// </summary>
private void Auth()
{
string token = ConfigurationManager.AppSettings["CorpToken"];//從配置文件獲取Token
string encodingAESKey = ConfigurationManager.AppSettings["EncodingAESKey"];//從配置文件獲取EncodingAESKey
string corpId = ConfigurationManager.AppSettings["CorpId"];//從配置文件獲取corpId
string echoString = HttpContext.Current.Request.QueryString["echoStr"];
string signature = HttpContext.Current.Request.QueryString["msg_signature"];//企業(yè)號(hào)的 msg_signature
string timestamp = HttpContext.Current.Request.QueryString["timestamp"];
string nonce = HttpContext.Current.Request.QueryString["nonce"];
string decryptEchoString = "";
if (CheckSignature(token, signature, timestamp, nonce, corpId, encodingAESKey, echoString, ref decryptEchoString))
{
if (!string.IsNullO
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注