服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - ASP.NET教程 - 微信开发(一) asp.net接入

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

2020-01-07 14:01lvzhongjian ASP.NET教程

我们要进行微信公众平台的开发,第一步当然是要有公众号了。什么?不知道什么是微信公众号,看来你还要先回炉炼炼了,呵呵。通俗的说,我们微信平台就好像是一个大社会,里面有个体人,也有各种组织机构。

        想要微信开发,首先要有个服务器,但是自己没有。这时候可以用花生壳,将内网映射到公网上,这样就可以在公网访问自己的网站了。

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
MyLog.DebugInfo("request default.aspx");
String echoStr = 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()就和所查到的差不多了。这里贴一下

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

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

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

延伸 · 阅读

精彩推荐