码迷,mamicode.com
首页 > 微信 > 详细

基于C#的微信公众平台开发系列1

时间:2015-03-06 23:22:40      阅读:282      评论:0      收藏:0      [点我收藏+]

标签:

1.首先服务器地址及Token验证:

技术分享

Token验证请求地址wx.ashx代码:

 1 using System;
 2 using System.Web;
 3 
 4 public class wx : IHttpHandler {
 5     
 6     public void ProcessRequest (HttpContext context) 
 7     {
 8         context.Response.ContentType = "text/plain";
 9         //
10         if (context.Request.HttpMethod.ToLower().Equals("get"))
11         {
12             ValidateUrl();
13         }
14         else {
15             context.Response.Write("wgx:post");
16         }
17     }
18    #region 服务器地址校验
19     /// <summary>
20     /// 服务器地址校验
21     /// </summary>
22     public void ValidateUrl()
23     {
24         /* signature    微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
25             timestamp    时间戳
26             nonce    随机数
27             echostr    随机字符串
28         */
29         //接收请求过来的参数
30         HttpContext context = HttpContext.Current;
31         string signature = context.Request["signature"];
32         string timestamp = context.Request["timestamp"];
33         string nonce = context.Request["nonce"];
34 
35         string echostr = context.Request["echostr"];
36         
37         string token = "weixinduang";
38         string[] temp1 = { token, timestamp, nonce };
39         //1.字典序排序
40         Array.Sort(temp1);
41         //2.将3个参数字符串拼接成一个字符串后进行sha1加密
42         string str = string.Join("", temp1);
43         string strResult = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "SHA1");
44         //3.开发者获得加密后的字符串可与signature对比,标识该请求来源于微信
45         if (strResult.ToLower().Equals(signature))
46         {
47             context.Response.Write(echostr);
48         }
49     } 
50     #endregion
51         
52     public bool IsReusable {
53         get {
54             return false;
55         }
56     }
57 }

如果验证通过会提示成功,否则失败。

基于C#的微信公众平台开发系列1

标签:

原文地址:http://www.cnblogs.com/wgx0428/p/4319335.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!