<iframe data-id="codeleading.com_750x200_responsive_5_DFP" frameborder="0" height="1" marginheight="0" marginwidth="0" scrolling="no" width="1"></iframe> 因公司需要,开通了微信公众号。
在开发对接中摸索了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; } } }
其实没那么麻烦,直接最简单的:
/// <summary> /// 微信公众号开发配置基本配置时候填的token校验 /// </summary> /// <returns></returns> public IActionResult TokenValid() { ///这里做的假,正常的是需要一堆审核的 ///https://codeleading.com/article/81633002885/ string echostr = GDCUtility.GetParamNoSpace("echostr"); return Content(echostr); }
直接返回验证传递过来的 echostr .
标签:nonce,string,C#,微信,Current,token,HttpContext,tmpStr From: https://www.cnblogs.com/huaan011/p/18008186