微信开发者验证

上代码

 1 using System;
 2 using System.Collections.Generic;
 3 using System.IO;
 4 using System.Linq;
 5 using System.Web;
 6 using System.Web.UI;
 7 using System.Web.UI.WebControls;
 8 using System.Web.Security;
 9 
10 public partial class wx_Default : System.Web.UI.Page
11 {
12     protected void Page_Load(object sender, EventArgs e)
13     {
14         HttpContext context = HttpContext.Current;
15         string _signature = Request.QueryString["signature"];
16         string _timestamp = Request.QueryString["timestamp"];
17         string _nonce = Request.QueryString["nonce"];
18         string _echostr = Request.QueryString["echostr"];
19         string token = "linkideas";
20 
21         string[] stringArray = { token, _timestamp, _nonce };
22         Array.Sort(stringArray);
23         string mergeString = string.Concat(stringArray);
24 
25         mergeString = FormsAuthentication.HashPasswordForStoringInConfigFile(mergeString, "SHA1").ToLower();
26 
27         if (_signature == mergeString)
28         {
29             Response.Clear();
30             Response.ContentType = "text/plain";
31             Response.Write(_echostr);
32             Response.End();
33         }
34     }
35 }

下边重写,易于理解,作用同上

 1 using System;
 2 using System.Collections.Generic;
 3 using System.IO;
 4 using System.Linq;
 5 using System.Web;
 6 using System.Web.UI;
 7 using System.Web.UI.WebControls;
 8 using System.Web.Security;
 9 
10 public partial class wx_Default : System.Web.UI.Page
11 {
12     protected void Page_Load(object sender, EventArgs e)
13     {
14         valid();
15     }
16 
17     HttpContext context = HttpContext.Current;
18     public void valid()
19     {
20         var echostr = context.Request["echoStr"].ToString();
21         if (checkSignature() && !string.IsNullOrEmpty(echostr))
22         {
23             Response.Clear();
24             Response.ContentType = "text/plain";
25             Response.Write(echostr);
26             Response.End();
27         }
28     }
29     public bool checkSignature()
30     {
31         var signature = context.Request["signature"].ToString();
32         var timestamp = context.Request["timestamp"].ToString();
33         var nonce = context.Request["nonce"].ToString();
34         var token = "linkideas";
35 
36         string[] ArrTmp = { token, timestamp, nonce };
37         Array.Sort(ArrTmp);//字典排序
38         string mergeString = string.Concat(ArrTmp);//作用相同 string mergeString = string.Join("", ArrTmp);
39 
40         mergeString = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(mergeString, "SHA1");
41         mergeString = mergeString.ToLower();
42         if (mergeString == signature)
43         {
44             return true;
45         }
46         else
47         {
48             return false;
49         }
50     }
51 }

 

posted @ 2013-11-26 16:02  duanyong  阅读(304)  评论(0编辑  收藏  举报