当前位置: 移动技术网 > IT编程>开发语言>c# > c#微信公众号开发----基本设置,服务器配置token验证

c#微信公众号开发----基本设置,服务器配置token验证

2019年11月14日  | 移动技术网IT编程  | 我要评论

c#微信公众号开发----基本设置

参考微信官方文档

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必须保持一致,若不一致会弹出提示。

 

 

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网