小程序通過webview向公眾號授權
1.設置公眾號授權域名
2.小程序的webview的src通過帶上小程序的openid以及其他的一些參數向網頁授權域名下的網站發起請求。
ps:這里有個坑!!!!如果src的url太長,是的!太長!會導致參數發不過去!什么意思呢?好比,我的請求url是https://xxx.xxx/xxx/xxx?openid={{openid}}&xx={{xx}},到達url下的接口接收到的參數openid為空,那個xx也為空。我們之前是把openid,nickname,unionid都發過去,結果全為空,找了很久才發現這個問題。
3.微信公眾號靜默授權
redirect_uri為授權回調地址
4.授權回調

public class PublicNumberCodeController : Controller { // GET: PublicNumberCode public ActionResult Index() { LogHelper.Info("***已經到達PublicNumberCode頁***:" + DateTime.Now); try { //如果已經授權,直接進入頁面 if (!Util.isNotNull(Request["code"])) { LogHelper.Info("已經授權過了"); return View(); } int weixin_id = Convert.ToInt32(Request["weixin_id"]); //公眾號id string fromopenid = Request["openid"]; //openid string appcode = Request["appcode"]; //appcode string code = Request["code"]; string state = Request["state"]; string appid = Request["appid"]; string appsecret = Request["appsecret"]; LogHelper.Info("code值:" + code); string token_jsonstr = WeixinAPi.accesstoken(code, appid, appsecret); //根據 code 換取 token ,得到 openid 與 access_token if (token_jsonstr.IndexOf("openid") > -1 && token_jsonstr.IndexOf("access_token") > -1) { JObject token_json = JObject.Parse(token_jsonstr); //公眾號的openid string openid = token_json["openid"].ToString(); string access_token = token_json["access_token"].ToString(); //插入數據 ***********關聯小程序openid與公眾號openid************** //查詢是否有未發成功的模板消息,一個小時以內的 LogHelper.Info("***准備發送模板消息***:" + DateTime.Now); *************發送待支付模板消息************************** } } catch (Exception ex) { LogHelper.Error(ex.Message, ex); } LogHelper.Info("***完成,到引導頁***:" + DateTime.Now); return View(); }
微信授權方法
/// <summary> /// 第一步:用戶同意授權,獲取code /// </summary> /// <param name="redirect_uri">授權后重定向的回調鏈接地址,請使用urlencode對鏈接進行處理</param> /// <param name="scope">應用授權作用域,snsapi_base (不彈出授權頁面,直接跳轉,只能獲取用戶openid),snsapi_userinfo (彈出授權頁面</param> /// <param name="state">重定向后會帶上state參數,開發者可以填寫a-zA-Z0-9的參數值,最多128字節 </param> /// <returns>null</returns> public static void authorize(string AppId, string redirect_uri, string scope, string state) { HttpResponse res = HttpContext.Current.Response; string url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + AppId + "&redirect_uri=" + HttpUtility.UrlEncode(redirect_uri) + "&response_type=code&scope=" + scope + "&state=" + state + "#wechat_redirect"; //sys.errMessage = url; res.Redirect(url); } /// <summary> /// 第二步:通過code換取網頁授權access_token /// </summary> /// <param name="code">填寫第一步獲取的code參數 </param> /// <returns>json string</returns> public static string accesstoken(string code, string appid, string appsecret) { string url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appid + "&secret=" + appsecret + "&code=" + code + "&grant_type=authorization_code"; return Util.MethodGET(url, "UTF-8"); } /// <summary> /// 第三步:通過access_token換取unionid /// </summary> /// <param name="access_token">填寫第一步獲取的code參數 </param> /// <param name="openid">openid</param> /// <returns>json string</returns> public static string getunionid(string access_token, string openid) { string url = "https://api.weixin.qq.com/sns/userinfo?access_token=" + access_token + "&openid=" + openid + "&lang=zh_CN "; return Util.MethodGET(url, "UTF-8"); }