公众平台用户提交信息后,微信服务器将发送GET请求到填写的URL上,并且带上四个参数:
开发者通过检验signature对请求进行校验(下面有校验方式)。若确认此次GET请求来自微信服务器,请原样返回echostr参数内容,则接入生效,否则接入失败。
signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
加密/校验流程:
- 1. 将token、timestamp、nonce三个参数进行字典序排序
- 2. 将三个参数字符串拼接成一个字符串进行sha1加密
- 3. 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
/// <summary>
/// 验证签名
/// </summary>
/// <param name="signature"></param>
/// <param name="timestamp"></param>
/// <param name="nonce"></param>
/// <returns></returns>
public static bool CheckSignature(String signature, String timestamp, String nonce)
{
String[] arr = new String[] { token, timestamp, nonce };
// 将token、timestamp、nonce三个参数进行字典序排序
Array.Sort<String>(arr);
StringBuilder content = new StringBuilder();
for ( int i = 0; i < arr.Length; i++)
{
content.Append(arr[i]);
}
String tmpStr = SHA1_Encrypt(content.ToString());
// 将sha1加密后的字符串可与signature对比,标识该请求来源于微信
return tmpStr != null ? tmpStr.Equals(signature) : false ;
}
/// <summary>
/// 使用缺省密钥给字符串加密
/// </summary>
/// <param name="Source_String"></param>
/// <returns></returns>
public static string SHA1_Encrypt( string Source_String)
{
byte [] StrRes = Encoding.Default.GetBytes(Source_String);
HashAlgorithm iSHA = new SHA1CryptoServiceProvider();
StrRes = iSHA.ComputeHash(StrRes);
StringBuilder EnText = new StringBuilder();
foreach ( byte iByte in StrRes)
{
EnText.AppendFormat( "{0:x2}" , iByte);
}
return EnText.ToString();
}
|
接入后是消息推送当普通微信用户向公众账号发消息时,微信服务器将POST该消息到填写的URL上。
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
protected void Page_Load( object sender, EventArgs e)
{
if (Request.HttpMethod.ToUpper() == "GET" )
{
// 微信加密签名
string signature = Request.QueryString[ "signature" ];
// 时间戳
string timestamp = Request.QueryString[ "timestamp" ];
// 随机数
string nonce = Request.QueryString[ "nonce" ];
// 随机字符串
string echostr = Request.QueryString[ "echostr" ];
if (WeixinServer.CheckSignature(signature, timestamp, nonce))
{
Response.Write(echostr);
}
}
else if (Request.HttpMethod.ToUpper() == "POST" )
{
StreamReader stream = new StreamReader(Request.InputStream);
string xml = stream.ReadToEnd();
processRequest(xml);
}
}
/// <summary>
/// 处理微信发来的请求
/// </summary>
/// <param name="xml"></param>
public void processRequest(String xml)
{
try
{
// xml请求解析
Hashtable requestHT = WeixinServer.ParseXml(xml);
// 发送方帐号(open_id)
string fromUserName = ( string )requestHT[ "FromUserName" ];
// 公众帐号
string toUserName = ( string )requestHT[ "ToUserName" ];
// 消息类型
string msgType = ( string )requestHT[ "MsgType" ];
//文字消息
if (msgType == ReqMsgType.Text)
{
// Response.Write(str);
string content = ( string )requestHT[ "Content" ];
if (content== "1" )
{
// Response.Write(str);
Response.Write(GetNewsMessage(toUserName, fromUserName));
return ;
}
if (content == "2" )
{
Response.Write(GetUserBlogMessage(toUserName, fromUserName));
return ;
}
if (content == "3" )
{
Response.Write(GetGroupMessage(toUserName, fromUserName));
return ;
}
if (content == "4" )
{
Response.Write(GetWinePartyMessage(toUserName, fromUserName));
return ;
}
Response.Write(GetMainMenuMessage(toUserName, fromUserName, "你好,我是vinehoo," ));
}
else if (msgType == ReqMsgType.Event)
{
// 事件类型
String eventType = ( string )requestHT[ "Event" ];
// 订阅
if (eventType==ReqEventType.Subscribe)
{
Response.Write(GetMainMenuMessage(toUserName, fromUserName, "谢谢您的关注!," ));
}
// 取消订阅
else if (eventType==ReqEventType.Unsubscribe)
{
// TODO 取消订阅后用户再收不到公众号发送的消息,因此不需要回复消息
}
// 自定义菜单点击事件
else if (eventType==ReqEventType.CLICK)
{
// TODO 自定义菜单权没有开放,暂不处理该类消息
}
}
else if (msgType == ReqMsgType.Location)
{
}
}
catch (Exception e)
{
}
}<pre name= "code" class = "csharp" > protected void Page_Load( object sender, EventArgs e)
{
if (Request.HttpMethod.ToUpper() == "GET" )
{
// 微信加密签名
string signature = Request.QueryString[ "signature" ];
// 时间戳
string timestamp = Request.QueryString[ "timestamp" ];
// 随机数
string nonce = Request.QueryString[ "nonce" ];
// 随机字符串
string echostr = Request.QueryString[ "echostr" ];
if (WeixinServer.CheckSignature(signature, timestamp, nonce))
{
Response.Write(echostr);
}
}
else if (Request.HttpMethod.ToUpper() == "POST" )
{
StreamReader stream = new StreamReader(Request.InputStream);
string xml = stream.ReadToEnd();
processRequest(xml);
}
}
/// <summary>
/// 处理微信发来的请求
/// </summary>
/// <param name="xml"></param>
public void processRequest(String xml)
{
try
{
// xml请求解析
Hashtable requestHT = WeixinServer.ParseXml(xml);
// 发送方帐号(open_id)
string fromUserName = ( string )requestHT[ "FromUserName" ];
// 公众帐号
string toUserName = ( string )requestHT[ "ToUserName" ];
// 消息类型
string msgType = ( string )requestHT[ "MsgType" ];
//文字消息
if (msgType == ReqMsgType.Text)
{
// Response.Write(str);
string content = ( string )requestHT[ "Content" ];
if (content== "1" )
{
// Response.Write(str);
Response.Write(GetNewsMessage(toUserName, fromUserName));
return ;
}
if (content == "2" )
{
Response.Write(GetUserBlogMessage(toUserName, fromUserName));
return ;
}
if (content == "3" )
{
Response.Write(GetGroupMessage(toUserName, fromUserName));
return ;
}
if (content == "4" )
{
Response.Write(GetWinePartyMessage(toUserName, fromUserName));
return ;
}
Response.Write(GetMainMenuMessage(toUserName, fromUserName, "你好,我是vinehoo," ));
}
else if (msgType == ReqMsgType.Event)
{
// 事件类型
String eventType = ( string )requestHT[ "Event" ];
// 订阅
if (eventType==ReqEventType.Subscribe)
{
Response.Write(GetMainMenuMessage(toUserName, fromUserName, "谢谢您的关注!," ));
}
// 取消订阅
else if (eventType==ReqEventType.Unsubscribe)
{
// TODO 取消订阅后用户再收不到公众号发送的消息,因此不需要回复消息
}
// 自定义菜单点击事件
else if (eventType==ReqEventType.CLICK)
{
// TODO 自定义菜单权没有开放,暂不处理该类消息
}
}
else if (msgType == ReqMsgType.Location)
{
}
}
catch (Exception e)
{
}
}</pre><br>
<pre></pre>
<br>
<br>
|
以上就是关于ASP.NET微信开发接口指南的相关内容介绍,希望对大家的学习有所帮助。