1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > 微信公众号接口服务器认证配置

微信公众号接口服务器认证配置

时间:2019-04-08 08:05:37

相关推荐

微信公众号接口服务器认证配置

移动端的项目之前一直没有接触过,最近做了一个公众号的项目。做过公众号的程序猿都清楚,开发公众号需要访问后台接口服务器。首先通过公众号接口测试号登录微信公众平台。

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事件中进行的公众号服务器验证处理。希望对初学公众号开发的朋友有所帮助。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。