.Net实现微信公众平台开发接口(一) 之 “微信开发配置”
我们只要通过微信官方认证,成为开发者,才能实现微信提供的各种接口,否则即使调用了接口,微信也不会实现推送,功能也无法通过开发模式真正得到实现,所以需要正确配置微信信息,通过微信官方认证,成为开发者才可以进行下一步的接口开放。
一、客户端需要配置的信息
客户端配置微信信息的时候大概需要参数有:
| 序号 | 参数 | 说明 |
| 1 | URL | 验证开发者身份的url |
| 2 | token | 用于校验 |
| 3 | wechat_name | 微信号 |
| 4 | appid | 调用微信接口的凭证 |
| 5 | appsecret | 调用微信接口的凭证密钥 |
| 6 | wechat_originalid | 微信原始id |
| 7 | wechat_type | 微信类型(服务号,订阅号,企业号) |
| 8 | wechat_key | 为了安全起见,在url后面加的标示参数 |
这些信息都要保存到客户端中,所以我们创建数据库表字段的时候按照上面的参数即可,如下
CREATE TABLE [dbo].[w_wechat](
[wechat_id] [int] IDENTITY(1,1) NOT NULL,
[wechat_name] [varchar](250) NULL,
[wechat_key] [varchar](250) NULL,
[wechat_url] [varchar](250) NULL,
[wechat_token] [varchar](250) NULL,
[wechat_appid] [varchar](250) NULL,
[wechat_appsecret] [varchar](250) NULL,
[wechat_originalid] [varchar](250) NULL,
[wechat_addtime] [datetime] NULL,
[wechat_isdel] [int] NULL,
[wechat_stutas] [int] NULL,
[user_id] [int] NULL,
[wechat_type] [int] NULL,
CONSTRAINT [PK_w_wechat] PRIMARY KEY CLUSTERED
(
[wechat_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
至于如何保存,这里就不在多说,通过三层和mvc都可以,这里只要页面的代码贴出来大家看看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Web.UI.WebControls;
using weixin.Model;
using weixin.DAL; namespace weixinWeb.web.admin.wechat
{
public partial class bindapi : weixinWeb.web.admin.cs.adminbase
{
w_wechat_dal wechatdal = new w_wechat_dal(); protected w_wechat_model wechatmodel = new w_wechat_model();
protected DataTable dtweixin = new DataTable(); protected void Page_Load(object sender, EventArgs e)
{
dtweixin = wechatdal.GetList(" user_id =" + user_id).Tables[];
//打开页面就获取并保存url和token信息
if (dtweixin.Rows.Count <= )
{
string weixin_key = Guid.NewGuid().ToString();
string weixin_url = "http://" + Request.Url.Authority + "/web/wechat/api/wechatapi.aspx?key=" + weixin_key;//url
wechatmodel.wechat_token = GetRandom();//token用来验证每次的接口访问
wechatmodel.wechat_url = weixin_url;
wechatmodel.wechat_key = weixin_key;
wechatmodel.wechat_addtime = DateTime.Now;
wechatmodel.user_id = int.Parse(user_id);
wechatdal.Add(wechatmodel);
}
else
{
wechatmodel = wechatdal.GetModel(int.Parse(dtweixin.Rows[]["wechat_id"].ToString()));
}
switch (Request.Form["action"])
{
case "bindapi":
api();
break;
}
}
/// <summary>
/// 修改和保存微信配置信息
/// </summary>
private void api()
{
dtweixin = wechatdal.GetList(" user_id =" + user_id).Tables[];
if (dtweixin.Rows.Count > )
{
wechatmodel = wechatdal.GetModel(int.Parse(dtweixin.Rows[]["WeChat_ID"].ToString()));
wechatmodel.wechat_name = Request.Form["weixin_name"].Trim().ToString();//微信名称
wechatmodel.wechat_appid = Request.Form["appid"].Trim().ToString();//凭证
wechatmodel.wechat_appsecret = Request.Form["appsecret"].Trim().ToString();//凭证钥匙
wechatmodel.wechat_originalid = Request.Form["originalid"].Trim().ToString();//微信原始id
wechatmodel.wechat_type = int.Parse(Request.Form["is_show"].Trim().ToString());//公众号类型(服务号,订阅号)
wechatdal.Update(wechatmodel);
Response.Write("{\"errno\":\"0\",\"tip\":\"设置成功!\",\"url\":\"bindapi.aspx\",\"error\":\"\"}");
}
Response.End();
}
#region 获取10位随即数(字符串类型)
/// <summary>
/// 获取10位随即数(字符串类型)
/// </summary>
/// <returns></returns>
private string GetRandom()
{
Random ran = new Random();
int RandomValue = ran.Next() + ;
return RandomValue.ToString();
}
#endregion 获取10位随即数(字符串类型) }
}
其中这里要说的是
string weixin_key = Guid.NewGuid().ToString();
string weixin_url = "http://" + Request.Url.Authority + "/web/wechat/api/wechatapi.aspx?key=" + weixin_key;//url
wechatmodel.wechat_token = GetRandom();//token用来验证每次的接口访问
url的地址wechatapi.aspx主要是用来验证微信信息的,下面会重点说说这个页面
token是随机生成的一个说,主要在官网输入的和客户端一致就可以,token可以随意获取。
二、微信url和token验证基本原理和流程
微信是如何验证url和token的呢,假如填写的url和token信息为
URL: http://demo.xxx.com/web/wechat/api/wechatapi.aspx?key=d26bd9ae-5a4f-45d6-bb91-c434e3a7087a
ToKen:
首先、开发者提交信息后,微信服务器将发送GET请求到填写的URL上,GET请求携带四个参数:
| 参数 | 描述 |
|---|---|
| signature | 微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。 |
| timestamp | 时间戳 |
| nonce | 随机数 |
| echostr | 随机字符串 |
其次、客户端接受到这四个参数后,需要进行验证处理:
加密/校验流程如下:
1. 将token、timestamp、nonce三个参数进行字典序排序
2. 将三个参数字符串拼接成一个字符串进行sha1加密
3. 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信
也就是通过客户端的token,和微信服务器发送过来的timestamp,nonce进行字典排序,组成字符串并通过sha1加密,然后和微信服务器发送过来的signature进行比较,如果一样,则验证通过,并返回接收的echostr, 验证通过后,正确返回echostr,则表示接入成功,成为开发者,否则需要检查配置信息。
三、客户端验证处理
首先,开发端接收微信服务器发送的参数,并按照验证流程进行验证signature是否一致,代码如下
wechatapi.aspx
/// <summary>
/// 验证微信签名
/// </summary>
/// <returns></returns>
/// * 将token、timestamp、nonce三个参数进行字典序排序
/// * 将三个参数字符串拼接成一个字符串进行sha1加密
/// * 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信。
private bool CheckSignature()
{
string WeChat_Token = "";
string WeChat_Key = Request.QueryString["key"]; DataTable dtWeChat = wechatdal.GetList("wechat_key='" + WeChat_Key + "'").Tables[]; if (dtWeChat.Rows.Count > )
{
WeChat_Token = dtWeChat.Rows[]["wechat_token"].ToString();
}
//从微信服务器接收传递过来的数据
string signature = Request.QueryString["signature"]; //微信加密签名
string timestamp = Request.QueryString["timestamp"];//时间戳
string nonce = Request.QueryString["nonce"];//随机数
string[] ArrTmp = { WeChat_Token, timestamp, nonce };
Array.Sort(ArrTmp); //字典排序
string tmpStr = string.Join("", ArrTmp);//将三个字符串组成一个字符串
tmpStr = FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1");//进行sha1加密
tmpStr = tmpStr.ToLower();
//加过密的字符串与微信发送的signature进行比较,一样则通过微信验证,否则失败。
if (tmpStr == signature)
{
return true;
}
else
{
return false;
}
}
#endregion 验证微信API接口
若验证通过,正确返回echostr
/// <summary>
/// 验证微信API接口
/// </summary>
private void CheckWeChat()
{
string echoStr = Request.QueryString["echoStr"]; if (CheckSignature())
{
if (!string.IsNullOrEmpty(echoStr))
{
Response.Write(echoStr);
Response.End();
}
}
}
最后,微信访问页面的时候就要对其进行处理验证,所以事件要放到页面加载的地方
protected void Page_Load(object sender, EventArgs e)
{ //微信通过get请求验证api接口
CheckWeChat(); }
这样就基本完成了微信公众平台的配置和验证,成为开发者,下一步就是获取access_token,实现各个接口了。
.Net实现微信公众平台开发接口(一) 之 “微信开发配置”的更多相关文章
- 微信公众平台消息接口开发-封装weixin.class.php
原文:微信公众平台消息接口开发-封装weixin.class.php 一.封装weixin.class.php 由于微信公众平台的通信使用的是特定格式的XML数据,每次接受和回复都要去做一大堆的数据处 ...
- 微信公众平台消息接口开发(26)从Hello2BizUser文本到subscribe事件
微信公众平台 微信公众平台开发模式 消息接口 企业微信公众平台 Hello2BizUser subscribe 订阅事件 作者:方倍工作室 原文:http://www.cnblogs.com/txw1 ...
- 微信公众平台消息接口开发(12)消息接口Bug
微信公众平台开发模式 微信公众平台消息接口 微信公众平台API 微信开发模式 Bug 方倍工作室 原文:http://www.cnblogs.com/txw1958/archive/2013/03/1 ...
- 微信公众平台消息接口开发(24)图片识别之人脸识别API
微信公众平台开发模式 微信 公众平台 消息接口 开发模式 企业微信公众平台 图片识别 人脸识别 API 作者:方倍工作室 原文:http://www.cnblogs.com/txw1958/archi ...
- 微信公众平台消息接口开发(2)你的服务器没有正确响应Token验证的解决方法
你的服务器没有正确响应Token验证,请阅读消息接口使用指南 微信 微信公众平台开发模式 平台 消息 接口 启用 URL Token作者:http://txw1958.cnblogs.com/ 本系统 ...
- 微信公众平台消息接口PHP版开发教程
原文:微信公众平台消息接口PHP版开发教程 一.写好接口程序 在你的服务器上上传好一个接口程序文件,如http://www.yourdomain.com/weixin.php 内容如下: &l ...
- 微信公众平台消息接口开发-封装weixin.class.php(转)
一.封装weixin.class.php 由于微信公众平台的通信使用的是特定格式的XML数据,每次接受和回复都要去做一大堆的数据处理. 我们就考虑在这个基础上做一次封装,weixin.class.ph ...
- 微信公众平台消息接口PHP版
使用前提条件:拥有一个公网上的HTTP服务器主机空间,具有创建目录.上传文件等权限.推荐新浪的SAE.http://sae.sina.com.cn/ 首先请注册微信公众平台的账号,注册地址:http: ...
- 微信公众平台通用接口API指南
微信公众平台 通用接口 消息接口 开发模式 作者:方倍工作室原文:http://www.doucube.com/index.php?m=Article&a=show&id=5 微信公众 ...
- PHP玩转微信公众平台自定义接口
从微信公众平台开通自定义回复后,就一直在关注微信接口这一块,很想用自定义回复这块做个站长工具的查询,例如PR查询,备案查询等,输入网址信息,就能自动获取PR,获取备案信息,应该是一个不错的想法.不过以 ...
随机推荐
- 2019.03.04 bzoj5308: [Zjoi2018]胖(二分答案+st表)
传送门 想题5分钟调题两小时系列 其实还是我tcl 读完题之后自然会知道一个关键点能够更新的点是一段连续的区间,于是我们对于每个点能到的左右区间二分答案,用ststst表维护一下查询即可. 代码: # ...
- ABP框架系列之二十九:(Hangfire-Integration-延迟集成)
Introduction Hangfire is a compherensive background job manager. You can integrate ASP.NET Boilerpla ...
- 走进JDK(十)------HashMap
有人说HashMap是jdk中最难的类,重要性不用多说了,敲过代码的应该都懂,那么一起啃下这个硬骨头吧!一.哈希表在了解HashMap之前,先看看啥是哈希表,首先回顾下数组以及链表数组:采用一段连续的 ...
- Request processing failed; nested exception is java.lang.IllegalStateException: getOutputStream() has already been called for this response
问题分析: 在ServletRequest servletRequest中已经存在一个项目名称,此时,又用项目名称访问 http://localhost:8080/rent/pdf/preview r ...
- 4k项目--PHY通道绑定的两种模式
1.通道绑定有两种模式: • PMA bonding• PMA and PCS bonding GT通道是不支持通道绑定的 2.PMA绑定 PMA绑定减少了PMA之间的通道之间的Skew.并且在PMA ...
- Codeforces Round #486 (Div. 3) F. Rain and Umbrellas
Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...
- select和其元素options
普通的select形式为: <select> <option>选中元素1</option> <option>选中元素2</option> & ...
- [Solution] JZOJ3470 最短路
[Solution] JZOJ3470 最短路 题面 Description 给定一个n个点m条边的有向图,有k个标记点,要求从规定的起点按任意顺序经过所有标记点到达规定的终点,问最短的距离是多少. ...
- _ZNote_Qt_定时器的总结
Qt中实现定时器有两种方法. 一种是使用QObject类定时器;一种是使用QTimer类定时器.(定时器的精度依赖于操作系统和硬件,大多数平台支持20ms) 1,QObject类定时器. 通过QObj ...
- python持久化
将对象转为二进制字节流(pickle) import pickle my_list = [1,2,3] pickle_file = open('my_list.pkl', 'wb') #注意二进制写入 ...