咱們只要經過微信官方認證,成爲開發者,才能實現微信提供的各類接口,不然即便調用了接口,微信也不會實現推送,功能也沒法經過開發模式真正獲得實現,因此須要正確配置微信信息,經過微信官方認證,成爲開發者才能夠進行下一步的接口開放。web
1、客戶端須要配置的信息數據庫
客戶端配置微信信息的時候大概須要參數有:api
序號 | 參數 | 說明 |
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[0]; //打開頁面就獲取並保存url和token信息 if (dtweixin.Rows.Count <= 0) { 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[0]["wechat_id"].ToString())); } switch (Request.Form["action"]) { case "bindapi": api(); break; } } /// <summary> /// 修改和保存微信配置信息 /// </summary> private void api() { dtweixin = wechatdal.GetList(" user_id =" + user_id).Tables[0]; if (dtweixin.Rows.Count > 0) { wechatmodel = wechatdal.GetModel(int.Parse(dtweixin.Rows[0]["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(999999999) + 1000000000; 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主要是用來驗證微信信息的,下面會重點說說這個頁面mvc
token是隨機生成的一個說,主要在官網輸入的和客戶端一致就能夠,token能夠隨意獲取。app
2、微信url和token驗證基本原理和流程微信公衆平臺
微信是如何驗證url和token的呢,假如填寫的url和token信息爲dom
URL: http://demo.xxx.com/web/wechat/api/wechatapi.aspx?key=d26bd9ae-5a4f-45d6-bb91-c434e3a7087a ToKen:1369750827
首先、開發者提交信息後,微信服務器將發送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,則表示接入成功,成爲開發者,不然須要檢查配置信息。
3、客戶端驗證處理
首先,開發端接收微信服務器發送的參數,並按照驗證流程進行驗證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[0]; if (dtWeChat.Rows.Count > 0) { WeChat_Token = dtWeChat.Rows[0]["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,實現各個接口了。