最近研究了下服务号的服务器配置和企业号的回调模式。真正实现完后,觉得很简单,但一开始还是走了点弯路,所以写了个web程序,只用改下配置文件里的参数就可以直接用了。下面介绍下详细的用法以及实现步骤。
本文原文地址:用c#开发微信(1)服务号的服务器配置和企业号的回调模式 - url接入 (源码下载)
http://yunpan.cn/cjeTSAKwUVmv9 访问密码 7ab3
<appSettings>
<!--微信的Token-->
<add key="WeixinToken" value="dd"/>
<add key="AppId" value="wxdbddd2bc"/>
<add key="AppSecret" value="82f7ddd88e196"/>
<!--企业号配置信息-->
<add key="CorpToken" value="fddd"/>
<add key="CorpId" value="wx1156d982ddda8"/>
<add key="EncodingAESKey" value="aNvJOkGYddyGwf5Rg"/>
</appSettings>
企业号:
服务号:
服务号:
完整源码:
/// <summary>
/// 处理微信服务器验证消息
/// </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 - 仅在微信后台填写URL验证时触发
if (CheckSignature(signature, timestamp, nonce, token))
{
WriteContent(echostr); //返回随机字符串则表示验证通过
}
else
{
WriteContent("failed:" + signature + "," + GetSignature(timestamp, nonce, token) + "。" +
"如果你在浏览器中看到这句话,说明此地址可以被作为微信公众账号后台的Url,请注意保持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
企业号:
完整源码:
public void ProcessRequest(HttpContext context)
{
string postString = string.Empty;
if (HttpContext.Current.Request.HttpMethod.ToUpper() == "GET")
{
Auth();
}
}
/// <summary>
/// 成为开发者的第一步,验证并相应服务器的数据
/// </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"];//企业号的 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
新闻热点
疑难解答