最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

微信開(kāi)放平臺(tái)之網(wǎng)站授權(quán)微信登錄功能

 更新時(shí)間:2015年09月24日 11:22:29   作者:小 鵬  
本文通過(guò).net實(shí)現(xiàn)的微信開(kāi)放平臺(tái)之網(wǎng)站授權(quán)微信登錄功能,需要的小伙伴一起看看吧

1 微信開(kāi)放平臺(tái):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頁(yè)面顯示

4. 通過(guò)官方提供的文檔,我們可以看出一共分4個(gè)步驟

第一步:請(qǐng)求CODE

第二步:通過(guò)code獲取access_token

第三步:通過(guò)access_token調(diào)用接口

第4步:獲取用戶個(gè)人信息(UnionID機(jī)制)

api:核心代碼

public class weixin_helper
 {
  public weixin_helper()
  {
  }
  /// <summary>
  /// 根據(jù)AppID和AppSecret獲得access token(默認(rèn)過(guò)期時(shí)間為2小時(shí))
  /// </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 + "";
   //發(fā)送并接受返回值
   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>
  /// 取得臨時(shí)的Access Token(默認(rèn)過(guò)期時(shí)間為2小時(shí))
  /// </summary>
  /// <param name="code">臨時(shí)Authorization Code</param>
  /// <param name="state">防止CSRF攻擊,成功授權(quán)后回調(diào)時(shí)會(huì)原樣帶回</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";
   //發(fā)送并接受返回值
   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>
  /// 根據(jù)access_token判斷access_token是否過(guò)期
  /// </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;
   //發(fā)送并接受返回值
   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已過(guò)期則根據(jù)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;
   //發(fā)送并接受返回值
   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">臨時(shí)的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);
   //發(fā)送并接受返回值 
   string send_url = "https://api.weixin.qq.com/sns/userinfo?access_token="+access_token+"&openid="+open_id;
   //發(fā)送并接受返回值
   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("出錯(cuò)了,您尚未配置微信相關(guān)的API信息!");
   }
   string state = Guid.NewGuid().ToString().Replace("-", "");
   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";
   //開(kāi)始發(fā)送
   return Redirect(send_url); //跳轉(zhuǎn)到微信自己 指定的關(guān)聯(lián)登陸頁(yè)面
  }
  /// <summary>
  /// 微信登錄返回action
  /// </summary>
  public ActionResult WeChatReturnUrl(string state, string code)
  {
   //取得返回參數(shù)
   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))//若返回參數(shù)中未包含code或者state沒(méi)有通過(guò)驗(yàn)證則提示出錯(cuò)
   {
    return Content("出錯(cuò)啦,state未初始化!");
   }
   //第一步:通過(guò)code來(lái)獲取Access Token以及openid
   Dictionary<string, object> dic1 = weixin_helper.get_access_token(code, state);
   if (dic1 == null || !dic1.ContainsKey("access_token"))
   {
    return Content("錯(cuò)誤代碼:,無(wú)法獲取Access Token,請(qǐng)檢查App Key是否正確!");
   }
   if (dic1 == null || !dic1.ContainsKey("openid"))
   {
    if (dic1.ContainsKey("errmsg"))
    {
     return Content("errcode:" + dic1["errcode"] + ",errmsg:" + dic1["errmsg"]);
    }
    else
    {
     return Content("出錯(cuò)啦,無(wú)法獲取用戶授權(quán)Openid!");
    }
   }
   access_token = dic1["access_token"].ToString();//獲取access_token
   expires_in = dic1["expires_in"].ToString();//獲取過(guò)期時(shí)間
   refresh_token = dic1["refresh_token"].ToString();//獲取用于重新刷新access_token的憑證
   openid = dic1["openid"].ToString();//用戶唯一標(biāo)示openid
   //儲(chǔ)存獲取數(shù)據(jù)用到的信息
   Session["oauth_name"] = "webchat";
   Session["oauth_access_token"] = access_token;
   Session["oauth_openid"] = openid;
   Session["oauth_refresh_token"] = refresh_token;
   #region todo 將獲取到的用戶信息保存到數(shù)據(jù)庫(kù)中
   #endregion
   //第二步:通過(guò)Access Token以及openid來(lái)獲取用戶的基本信息
   //Dictionary<string, object> dic2 = weixin_helper.get_user_info(access_token,openid);
   //第三步:跳轉(zhuǎn)到指定頁(yè)面
   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\":\"出錯(cuò)啦,Access Token已過(guò)期或不存在!\"}";
   }
   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)) //調(diào)用access_token前需判斷是否過(guò)期
   {
    Dictionary<string, object> dic1 = weixin_helper.get_refresh_token(oauth_refresh_token);//如果已過(guò)期則重新?lián)Q取新的access_token
    if (dic1 == null || !dic1.ContainsKey("access_token"))
    {
     return "{\"openid\":\"0\", \"msg\":\"出錯(cuò)啦,無(wú)法獲取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\":\"出錯(cuò)啦,無(wú)法獲取授權(quán)用戶信息!\"}";
   }
   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\":\"出錯(cuò)啦,無(wú)法獲取授權(quán)用戶信息!\"}";
   }
  }
  #endregion

相關(guān)文章

  • C#實(shí)現(xiàn)桶排序算法的示例代碼

    C#實(shí)現(xiàn)桶排序算法的示例代碼

    桶排序是一種快速且高效的排序算法,通過(guò)將數(shù)據(jù)分配到有序桶中并分別排序,適合均勻分布數(shù)據(jù),它的時(shí)間復(fù)雜度為O(n),但不適合數(shù)據(jù)分布極不均勻或數(shù)據(jù)范圍很大的情況,桶排序算法簡(jiǎn)單、易實(shí)現(xiàn),可調(diào)整桶的大小和數(shù)量以適應(yīng)不同數(shù)據(jù),感興趣的可以了解一下
    2024-10-10
  • 詳解C#如何使用讀寫鎖控制多線程寫入

    詳解C#如何使用讀寫鎖控制多線程寫入

    這篇文章主要為大家詳細(xì)介紹了C#如何使用讀寫鎖控制多線程寫入,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-01-01
  • Unity實(shí)現(xiàn)微信聊天框界面

    Unity實(shí)現(xiàn)微信聊天框界面

    這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)微信聊天框界面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • 輕松學(xué)習(xí)C#的String類

    輕松學(xué)習(xí)C#的String類

    輕松學(xué)習(xí)C#的String類,小編也是第一次接觸C#的String類,感興趣的小伙伴們可以參考一下,大家一起學(xué)習(xí)
    2015-11-11
  • C# 大數(shù)據(jù)導(dǎo)出word的假死報(bào)錯(cuò)的處理方法

    C# 大數(shù)據(jù)導(dǎo)出word的假死報(bào)錯(cuò)的處理方法

    C# 大數(shù)據(jù)導(dǎo)出word的假死報(bào)錯(cuò)的處理方法,需要的朋友可以參考一下
    2013-03-03
  • 基于WPF實(shí)現(xiàn)帶蒙版的MessageBox消息提示框

    基于WPF實(shí)現(xiàn)帶蒙版的MessageBox消息提示框

    這篇文章主要介紹了如何利用WPF實(shí)現(xiàn)帶蒙版的MessageBox消息提示框,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定幫助,需要的可以參考一下
    2022-08-08
  • WinForm實(shí)現(xiàn)頁(yè)面按鈕定時(shí)隱藏功能

    WinForm實(shí)現(xiàn)頁(yè)面按鈕定時(shí)隱藏功能

    這篇文章主要介紹了WinForm實(shí)現(xiàn)頁(yè)面按鈕定時(shí)隱藏功能,結(jié)合實(shí)例形式分析了WinForm基于定時(shí)器的頁(yè)面控件屬性動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2017-05-05
  • c# for循環(huán)中創(chuàng)建線程執(zhí)行問(wèn)題

    c# for循環(huán)中創(chuàng)建線程執(zhí)行問(wèn)題

    這篇文章主要介紹了有關(guān)c# for循環(huán)中創(chuàng)建線程執(zhí)行問(wèn)題,下面文章將將以舉例的方式展開(kāi)for循環(huán)中創(chuàng)建線程執(zhí)行問(wèn)題的內(nèi)容,需要的朋友可以參考一下,希望對(duì)你有所幫助
    2021-11-11
  • c#中合并excel表格的方法示例

    c#中合并excel表格的方法示例

    本篇文章主要介紹了c#中合并excel表格的方法,就是將excel表格結(jié)構(gòu)一樣的合并到一起,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。
    2016-10-10
  • C# 圖片剪切與縮小的實(shí)例

    C# 圖片剪切與縮小的實(shí)例

    C# 剪切圖片與縮小的實(shí)例,需要的朋友可以參考一下
    2013-04-04

最新評(píng)論

来安县| 阜平县| 错那县| 屯门区| 襄垣县| 广安市| 临邑县| 荔波县| 石家庄市| 保靖县| 山阳县| 浦东新区| 正阳县| 渝中区| 永清县| 宕昌县| 额济纳旗| 冀州市| 鄂托克旗| 杭锦后旗| 康乐县| 曲麻莱县| 错那县| 滨州市| 称多县| 宁津县| 阿图什市| 华亭县| 弥勒县| 田东县| 渑池县| 鲜城| 沅陵县| 新乐市| 东乡县| 乐清市| 舟曲县| 二连浩特市| 峨眉山市| 元江| 长沙市|