VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > C#编程 >
  • C#教程之c#微信公众号开发一----基本设置,服务器配置token验证,获取timestamp/nonce/signa

本站最新发布   C#从入门到精通
试听地址  
https://www.xin3721.com/eschool/CSharpxin3721/

参考微信官方文档

https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Access_Overview.html

开发→基本配置

公众号开发信息

 

 注:1.记录好开发者密码,会在程序中验证过程中使用到。

2.通过appid和appsecret调用access_token时,至有在ip白名单的ip才能成功调用。

 

 

服务器配置

 

若此处开启服务器配置,设置的自动回复和自定义菜单将全部失效。必须在程序中重写相关方法。

 

 

点击修改配置,token为随意填写的参数

 

 

我是用的是一般处理程序编写的微信接口token验证,参数参考官方文档。代码如下:

 

开发者通过检验signature对请求进行校验。若确认此次GET请求来自微信服务器,请原样返回echostr参数内容,则接入生效,成为开发者成功,否则接入失败。加密/校验流程如下:

1)将token、timestamp、nonce三个参数进行字典序排序

2)将三个参数字符串拼接成一个字符串进行sha1加密

3)开发者获得加密后的字符串可与signature对比,标识该请求来源于微信。

 

复制代码
 1 public void ProcessRequest(HttpContext context){
 2     //验证token
 3     string postString = string.Empty;
 4     string token ="aabbcc";   //验证token,随意填写  
 5     if(string.IsNullEmpty(token)){
 6         return ;
 7     }
 8     string echoString = HttpContext.Current.Request.QueryString["echoStr"];
 9     string signature = HttpContext.Current.Request.QueryString["sianature"];
10     string timestamp = HttpContext.Current.Request.QueryString["timestamp"];
11     string nonce = HttpContext.Current.Request.QueryString["nonce"];
12     if(CheckSignature(token,signature,timestamp,nonce)){
13         if(!string.IsNullOrEmpty(echiString)){
14           HttpContext.Current.Response.Write(echoString);
15           HttpContext.Current.Response.End();
16        } 
17     }
18 }
复制代码

 

复制代码
 1          /// <summary>
 2         /// 验证微信签名
 3         /// </summary>
 4         /// <param name="token">token</param>
 5         /// <param name="signature">签名</param>
 6         /// <param name="timestamp">时间戳</param>
 7         /// <param name="nonce">随机数</param>
 8         /// <returns></returns>
 9         public static bool CheckSignature(string token,
10  string signature, string timestamp, string nonce)
11         {
12             string[] ArrTmp = { token, timestamp, nonce };
13             //字典排序
14             Array.Sort(ArrTmp);
15             //拼接
16             string tmpStr = string.Join("", ArrTmp);
17             //sha1验证
18             tmpStr = FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1");
19             //tmpStr = Membership.CreateUser(tmpStr, "SHA1");
20             tmpStr = tmpStr.ToLower();
21 
22             if (tmpStr == signature)
23             {
24                 return true;
25             }
26             else
27             {
28                 return false;
29             }
30         }
复制代码

 

 将编写的代码路径,填写到url里,前面填写的“aabbcc”,此时token里填写的也必须为“aabbcc”。

token必须保持一致,若不一致会弹出提示。

 

 

 

二、获取timestamp/nonce/signature

timestamp时间戳

public static string timestamp(){
   TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
   return Convert.ToInt64(ts.TotalSeconds).ToString();
}

nonce随机数

public static string getNoncestr(){
   Random random = new Random();
   return Md5Util.GetMD5(random.Next(1000).ToString(),”GBK”).ToLower().Replace(“s”,”S”);
}

signature随机数

复制代码
public static string Signature(string token, string timestamp, string nonce){
   string[] ArrTmp = { token, timestamp, nonce };
   //字典排序
   Array.Sort(ArrTmp);
   //拼接
   string tmpStr = string.Join("", ArrTmp);
   //sha1验证
   tmpStr = FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1");
   tmpStr = tmpStr.ToLower();
   return tmpStr;
}
复制代码

 

若一蓑烟雨 任平生 倒是也无晴 也无风
相关教程