因公司需要,开通了微信公众号。
在开发对接中摸索了2天,写下此记,备忘。
服务器地址(URL):https://www.findtechgroup.net/Handler1.ashx
因http80端口已被其他业务占用,只能用https(443) 协议,需路由映射服务器的443端口。
IIS 中需要添加SSL证书,这个证书在阿里云中免费申请,
如下为c#的 token 认证代码
在VS 项目中创建一个 一般处理程序( 下面代码是网上Copy的,出处忘记了)
/// <summary>
/// Handler1 的摘要说明
/// </summary>
public class Handler1 : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string token = "find123456789";
if (string.IsNullOrEmpty(token))
{
return;
}
string echoString = 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))
{
if (!string.IsNullOrEmpty(echoString))
{
HttpContext.Current.Response.Write(echoString);
HttpContext.Current.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);
//sha1验证
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;
}
}
}
623

被折叠的 条评论
为什么被折叠?



