微信开发(一) asp.net接入
想要微信开发,首先要有个服务器,但是自己没有。这时候可以用花生壳,将内网映射到公网上,这样就可以在公网访问自己的网站了。具体见:https://www.nhooo.com/article/83783.htm
然后要写一个接入代码,而微信上只有php是示例。这里附上asp.net的示例。
首先创建一个Default.aspx。在Page_Load里进行检验:(MyLog是日志类,可以忽略) 关于checkSignature()就和所查到的差不多了。这里贴一下
MyLog.DebugInfo("requestdefault.aspx"); StringechoStr=Request.QueryString["echostr"]; MyLog.DebugInfo("echoStr:"+echoStr); if(this.checkSignature()) { if(!string.IsNullOrEmpty(echoStr)){ MyLog.DebugInfo("echostr:"+echoStr); Response.Write(echoStr); Response.End(); } }
最最主要的是那句Response.End(),不加这一句怎么样都接不进去(希望有大神告知)。关于checkSignature()就和所查到的差不多了。这里贴一下
privateboolcheckSignature() { stringsignature=Request["signature"]; stringtimestamp=Request["timestamp"]; stringnonce=Request["nonce"]; MyLog.DebugInfo(String.Format("signature:{0},timestamp:{1},nonce:{2}",signature,timestamp,nonce)); stringtoken=TOKEN; string[]tmpArr=newstring[]{token,timestamp,nonce}; Array.Sort(tmpArr); stringtmpStr=string.Join("",tmpArr); //sha1加密 System.Security.Cryptography.SHA1sha1=newSystem.Security.Cryptography.SHA1CryptoServiceProvider(); byte[]secArr=sha1.ComputeHash(System.Text.Encoding.Default.GetBytes(tmpStr)); tmpStr=BitConverter.ToString(secArr).Replace("-","").ToLower(); MyLog.DebugInfo(String.Format("afterparse:{0}",tmpStr)); if(tmpStr==signature) { MyLog.DebugInfo("true"); returntrue; } else { returnfalse; } }
这里主要是因为那个Response.End()的问题,导致我搞了许久,特此记录一下,希望帮助能帮助到的人。
还有一点可能是因为微信服务器的原因Token验证失败,多点2次即可,别像我这样只点一次啊!!!