微信入门001(服务器配置)
通过url+token =è返回signature+timestamp+nonce+echostr
timestamp+nonce+ token 排序+sha1 加密=比较signature是否相等
注意:
url: 只能是备案的80 域名
token:32位随机数
timestamp:时间戳
nonce:随机数
echostr:随机字符串
代码如下:一般处理程序
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Net; 5 using System.Web; 6 using System.Security.Authentication; 7 using System.Security.Cryptography; 8 using System.Web.Security; 9 10 namespace WebApplication1 11 { 12 /// <summary> 13 /// wx 的摘要说明 14 /// </summary> 15 public class wx : IHttpHandler 16 { 17 private string token = "hHneE6EtBEHxNamAkTsCvz5pA3mHWxT"; 18 public void ProcessRequest(HttpContext context) 19 { 20 var url = context.Request.RawUrl; 21 22 // /?signature=28b94e012e1d698990faf96460a904e334699c6b&echostr=3153896548757110783×tamp=1457086219&nonce=1453006066 23 //context.Response.ContentType = "text/plain"; 24 //context.Response.Write("Hello World"); 25 26 //三个参数排序 ==>放入数组中 27 var signature = context.Request.QueryString["signature"]; 28 var timestamp = context.Request.QueryString["timestamp"]; 29 var nonce = context.Request.QueryString["nonce"]; 30 var echostr = context.Request.QueryString["echostr"]; 31 var arr = new[] { token, timestamp, nonce }; 32 Array.Sort(arr); 33 var tmepstr = string.Join("", arr); 34 var tempsignature= FormsAuthentication.HashPasswordForStoringInConfigFile(tmepstr, "SHA1").ToLower(); 35 if (signature == tempsignature) 36 { 37 context.Response.Write(echostr); 38 } 39 40 } 41 42 public bool IsReusable 43 { 44 get 45 { 46 return false; 47 } 48 } 49 } 50 }
结果 :
微信调试技巧:记得打开w3wp.exe 附件进程