微信開放平台之網站受權微信登錄功效。本站提示廣大學習愛好者:(微信開放平台之網站受權微信登錄功效)文章只能為提供參考,不一定能成為您想要的結果。以下是微信開放平台之網站受權微信登錄功效正文
1 微信開放平台:https://open.weixin.qq.com/
2 微信官方教程:https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419316505&token=&lang=zh_CN
3.pc頁面顯示
4. 經由過程官方供給的文檔,我們可以看出一共分4個步調
第一步:要求CODE
第二步:經由過程code獲得access_token
第三步:經由過程access_token挪用接口
第4步:獲得用戶小我信息(UnionID機制)
api:焦點代碼
public class weixin_helper { public weixin_helper() { } /// <summary> /// 依據AppID和AppSecret取得access token(默許過時時光為2小時) /// </summary> /// <returns>Dictionary</returns> public static Dictionary<string, object> get_access_token() { //取得設置裝備擺設信息 oauth_config config = oauth_helper.get_config(2); string send_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + config.oauth_app_id + "&secret=" + config.oauth_app_key + ""; //發送並接收前往值 string result = Utils.HttpGet(send_url); if (result.Contains("errmsg")) { return null; } try { Dictionary<string, object> dic = JsonConvert.DeserializeObject<Dictionary<string, object>>(result); return dic; } catch { return null; } } /// <summary> /// 獲得暫時的Access Token(默許過時時光為2小時) /// </summary> /// <param name="code">暫時Authorization Code</param> /// <param name="state">避免CSRF進擊,勝利受權後回調時會原樣帶回</param> /// <returns>Dictionary</returns> public static Dictionary<string, object> get_access_token(string code, string state) { //取得設置裝備擺設信息 oauth_config config = oauth_helper.get_config(2); string send_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + config.oauth_app_id + "&secret=" + config.oauth_app_key + "&code="+code+"&grant_type=authorization_code"; //發送並接收前往值 string result = Utils.HttpGet(send_url); if (result.Contains("errmsg")) { return null; } try { Dictionary<string, object> dic = JsonConvert.DeserializeObject<Dictionary<string, object>>(result); return dic; } catch { return null; } } /// <summary> /// 依據access_token斷定access_token能否過時 /// </summary> /// <param name="access_token"></param> /// <returns>true表現未掉效</returns> public static bool check_access_token(string access_token) { //取得設置裝備擺設信息 oauth_config config = oauth_helper.get_config(2); string send_url = "https://api.weixin.qq.com/sns/auth?access_token=" + access_token + "&openid=" + config.oauth_app_id; //發送並接收前往值 string result = Utils.HttpGet(send_url); try { Dictionary<string, object> dic = JsonConvert.DeserializeObject<Dictionary<string, object>>(result); if (dic.ContainsKey("errmsg")) { if (dic["errmsg"].ToString()=="ok") { return true; } else { return false; } } return false; } catch { return false; } } /// <summary> /// 若fresh_token已過時則依據refresh_token獲得新的refresh_token /// </summary> /// <param name="refresh_token">refresh_token</param> /// <returns>Dictionary</returns> public static Dictionary<string, object> get_refresh_token(string refresh_token) { //取得設置裝備擺設信息 oauth_config config = oauth_helper.get_config(2); string send_url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=" + config.oauth_app_id + "&grant_type=refresh_token&refresh_token=" + refresh_token; //發送並接收前往值 string result = Utils.HttpGet(send_url); if (result.Contains("errmsg")) { return null; } try { Dictionary<string, object> dic = JsonConvert.DeserializeObject<Dictionary<string, object>>(result); return dic; } catch { return null; } } /// <summary> /// 獲得登錄用戶本身的根本材料 /// </summary> /// <param name="access_token">暫時的Access Token</param> /// <param name="open_id">用戶openid</param> /// <returns>Dictionary</returns> public static Dictionary<string, object> get_user_info(string access_token, string open_id) { //取得設置裝備擺設信息 oauth_config config = oauth_helper.get_config(2); //發送並接收前往值 string send_url = "https://api.weixin.qq.com/sns/userinfo?access_token="+access_token+"&openid="+open_id; //發送並接收前往值 string result = Utils.HttpGet(send_url); if (result.Contains("errmsg")) { return null; } //反序列化JSON Dictionary<string, object> dic = JsonHelper.DataRowFromJSON(result); return dic; } }
掌握器的焦點代碼:
#region 微信登錄 /// <summary> /// 微信登錄 /// </summary> public ActionResult WeChat() { //取得設置裝備擺設信息 oauth_config config = oauth_helper.get_config(2); //主鍵id if (config == null) { return Content("失足了,您還沒有設置裝備擺設微信相干的API信息!"); } string state = Guid.NewGuid().ToString().WordStr("-", ""); Session["oauth_state"] = state; string send_url = "https://open.weixin.qq.com/connect/qrconnect?appid=" + config.oauth_app_id + "&redirect_uri=" + Utils.UrlEncode(config.return_uri.ToLower()) + "&response_type=code&scope=snsapi_login&state=" + state + "#wechat_redirect"; //開端發送 return Redirect(send_url); //跳轉到微信本身 指定的聯系關系上岸頁面 } /// <summary> /// 微信登錄前往action /// </summary> public ActionResult WeChatReturnUrl(string state, string code) { //獲得前往參數 string access_token = string.Empty; string expires_in = string.Empty; string client_id = string.Empty; string openid = string.Empty; string refresh_token = string.Empty; if (Session["oauth_state"] == null || Session["oauth_state"].ToString() == "" || state != Session["oauth_state"].ToString() || string.IsNullOrEmpty(code))//若前往參數中未包括code或許state沒有經由過程驗證則提醒失足 { return Content("失足啦,state未初始化!"); } //第一步:經由過程code來獲得Access Token和openid Dictionary<string, object> dic1 = weixin_helper.get_access_token(code, state); if (dic1 == null || !dic1.ContainsKey("access_token")) { return Content("毛病代碼:,沒法獲得Access Token,請檢討App Key能否准確!"); } if (dic1 == null || !dic1.ContainsKey("openid")) { if (dic1.ContainsKey("errmsg")) { return Content("errcode:" + dic1["errcode"] + ",errmsg:" + dic1["errmsg"]); } else { return Content("失足啦,沒法獲得用戶受權Openid!"); } } access_token = dic1["access_token"].ToString();//獲得access_token expires_in = dic1["expires_in"].ToString();//獲得過時時光 refresh_token = dic1["refresh_token"].ToString();//獲得用於從新刷新access_token的憑證 openid = dic1["openid"].ToString();//用戶獨一標示openid //貯存獲得數據用到的信息 Session["oauth_name"] = "webchat"; Session["oauth_access_token"] = access_token; Session["oauth_openid"] = openid; Session["oauth_refresh_token"] = refresh_token; #region todo 將獲得到的用戶信息保留到數據庫中 #endregion //第二步:經由過程Access Token和openid來獲得用戶的根本信息 //Dictionary<string, object> dic2 = weixin_helper.get_user_info(access_token,openid); //第三步:跳轉到指定頁面 return Content(WeChatResultJson()); } /// <summary> /// 微信登錄前往action, 處置用戶信息 /// </summary> public string WeChatResultJson() { string oauth_access_token = string.Empty; string oauth_openid = string.Empty; string oauth_name = string.Empty; string oauth_refresh_token = string.Empty; if (Session["oauth_name"] == null || Session["oauth_access_token"] == null || Session["oauth_openid"] == null) { return "{\"ret\":\"1\", \"msg\":\"失足啦,Access Token已過時或不存在!\"}"; } oauth_name = Session["oauth_name"].ToString(); oauth_access_token = Session["oauth_access_token"].ToString(); oauth_openid = Session["oauth_openid"].ToString(); oauth_refresh_token = Session["oauth_refresh_token"].ToString(); if (!weixin_helper.check_access_token(oauth_access_token)) //挪用access_token前需斷定能否過時 { Dictionary<string, object> dic1 = weixin_helper.get_refresh_token(oauth_refresh_token);//假如已過時則從新換取新的access_token if (dic1 == null || !dic1.ContainsKey("access_token")) { return "{\"openid\":\"0\", \"msg\":\"失足啦,沒法獲得access_token!\"}"; } oauth_access_token = dic1["access_token"].ToString(); } Dictionary<string, object> dic = weixin_helper.get_user_info(oauth_access_token, oauth_openid); if (dic == null) { return "{\"openid\":\"0\", \"msg\":\"失足啦,沒法獲得受權用戶信息!\"}"; } try { StringBuilder str = new StringBuilder(); str.Append("{"); str.Append("\"openid\": \"" + dic["openid"].ToString() + "\", "); str.Append("\"nickname\": \"" + dic["nickname"].ToString() + "\", "); str.Append("\"sex\": \"" + dic["sex"].ToString() + "\", "); str.Append("\"province\": \"" + dic["province"].ToString() + "\", "); str.Append("\"city\": \"" + dic["city"].ToString() + "\", "); str.Append("\"country\": \"" + dic["country"].ToString() + "\", "); str.Append("\"headimgurl\": \"" + dic["headimgurl"].ToString() + "\", "); str.Append("\"privilege\": \"" + dic["privilege"].ToString() + "\", "); str.Append("\"unionid\": \"" + dic["unionid"].ToString() + "\""); str.Append("\"oauth_name\": \"" + oauth_name + "\""); str.Append("\"oauth_access_token\": \"" + oauth_access_token + "\""); str.Append("\"oauth_openid\": \"" + oauth_openid + "\""); str.Append("}"); return str.ToString(); } catch { return "{\"ret\":\"0\", \"msg\":\"失足啦,沒法獲得受權用戶信息!\"}"; } } #endregion