移动端的项目之前一直没有接触过,最近做了一个公众号的项目。做过公众号的程序猿都清楚,开发公众号需要访问后台接口服务器。首先通过公众号接口测试号登录微信公众平台。
URL地址是外网接口服务器地址。Token是自定义的一个字符串,验证接口需要用到的参数。本人是做C# 开发,所以通过IIS配置了一个站点服务器。
void context_startRequest(object sender, EventArgs e){HttpApplication app = sender as HttpApplication;HttpContext context = app.Context;System.Web.HttpRequest Request = context.Request;string signature = Request["signature"];string timestamp = Request["timestamp"];string nonce = Request["nonce"];string echostr = Request["echostr"];string token = "fdfdfgjh785558943";if (Request.HttpMethod == "GET"){//get method - 仅在微信后台填写URL验证时触发if (CheckSignature(token, signature, timestamp, nonce)){context.Response.Write(echostr); //返回随机字符串则表示验证通过}else{context.Response.Write("failed:" + signature);}context.Response.End();}}private 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 = FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1");tmpStr = tmpStr.ToLower();if (tmpStr == signature){return true;}else{return false;}}
上面这段代码是我通过添加HttpModel文件,在context_startRequest事件中进行的公众号服务器验证处理。希望对初学公众号开发的朋友有所帮助。