当前位置:Gxlcms > asp.net > 微信开发(一) asp.net接入

微信开发(一) asp.net接入

时间:2021-07-01 10:21:17 帮助过:46人阅读

 

        想要微信开发,首先要有个服务器,但是自己没有。这时候可以用花生壳,将内网映射到公网上,这样就可以在公网访问自己的网站了。具体见://www.gxlcms.com/article/83783.htm

        然后要写一个接入代码,而微信上只有php是示例。这里附上asp.net的示例。

        首先创建一个Default.aspx。在Page_Load里进行检验:(MyLog是日志类,可以忽略)   关于checkSignature()就和所查到的差不多了。这里贴一下
     

  1. MyLog.DebugInfo("request default.aspx");
  2. String echoStr = Request.QueryString["echostr"];
  3. MyLog.DebugInfo("echoStr:"+echoStr);
  4. if (this.checkSignature())
  5. {
  6. if(!string.IsNullOrEmpty(echoStr)){
  7. MyLog.DebugInfo("echostr:" + echoStr);
  8. Response.Write(echoStr);
  9. Response.End();
  10. }
  11. }

最最主要的是那句Response.End(),不加这一句怎么样都接不进去(希望有大神告知)。 关于checkSignature()就和所查到的差不多了。这里贴一下

  1. private bool checkSignature()
  2. {
  3. string signature = Request["signature"];
  4. string timestamp = Request["timestamp"];
  5. string nonce = Request["nonce"];
  6. MyLog.DebugInfo(String.Format("signature:{0},timestamp:{1},nonce:{2}", signature, timestamp, nonce));
  7. string token = TOKEN;
  8. string[] tmpArr = new string[] { token, timestamp, nonce };
  9. Array.Sort(tmpArr);
  10. string tmpStr = string.Join("", tmpArr);
  11. //sha1加密
  12. System.Security.Cryptography.SHA1 sha1 = new System.Security.Cryptography.SHA1CryptoServiceProvider();
  13. byte[] secArr = sha1.ComputeHash(System.Text.Encoding.Default.GetBytes(tmpStr));
  14. tmpStr = BitConverter.ToString(secArr).Replace("-", "").ToLower();
  15. MyLog.DebugInfo(String.Format("after parse:{0}", tmpStr));
  16. if (tmpStr == signature)
  17. {
  18. MyLog.DebugInfo("true");
  19. return true;
  20. }
  21. else
  22. {
  23. return false;
  24. }
  25. }

        这里主要是因为那个Response.End()的问题,导致我搞了许久,特此记录一下,希望帮助能帮助到的人。

        还有一点可能是因为微信服务器的原因Token验证失败,多点2次即可,别像我这样只点一次啊!!!

人气教程排行