前言
這篇主要是承接上篇的網(wǎng)頁授權(quán)獲取用戶基本信息的后文,也是對第一種靜默授權(quán)之后,用戶點(diǎn)擊公眾號內(nèi)鏈接時(shí),如何再次取得當(dāng)前用戶的OpenId的大致講解和一些注意事項(xiàng)。
?
看過上一篇的小伙伴都知道,我們在用戶關(guān)注的時(shí)候就已經(jīng)將該用戶的基本信息存入數(shù)據(jù)庫中,那么如果該用戶過了很久才點(diǎn)擊公眾號內(nèi)的網(wǎng)頁鏈接,那么我們該如何再次獲取這個(gè)唯一標(biāo)識呢?
重新獲取openid
具體實(shí)現(xiàn)
首先,我們定一個(gè)獲取openid的方法 ReGetOpenId
public static void ReGetOpenId() { string url = System.Web.HttpContext.Current.Request.Url.AbsoluteUri;//獲取當(dāng)前url if (System.Web.HttpContext.Current.Session["openid"] == "" || System.Web.HttpContext.Current.Session["openid"] == null) { //先要判斷是否是獲取code后跳轉(zhuǎn)過來的 if (System.Web.HttpContext.Current.Request.QueryString["code"] == "" || System.Web.HttpContext.Current.Request.QueryString["code"] == null) { //Code為空時(shí),先獲取Code string GetCodeUrls = GetCodeUrl(url); System.Web.HttpContext.Current.Response.Redirect(GetCodeUrls);//先跳轉(zhuǎn)到微信的服務(wù)器,取得code后會(huì)跳回來這頁面的 } else { //Code非空,已經(jīng)獲取了code后跳回來啦,現(xiàn)在重新獲取openid Log log = new Log(AppDomain.CurrentDomain.BaseDirectory + @"/log/Log.txt"); string openid = ""; openid = GetOauthAccessOpenId(System.Web.HttpContext.Current.Request.QueryString["Code"]);//重新取得用戶的openid System.Web.HttpContext.Current.Session["openid"] = openid; } } }
注:url最好是帶域名的,花生殼的域名是行不通的,再調(diào)微信平臺接口的時(shí)候,會(huì)報(bào)鏈接不正確錯(cuò)誤
上文中GetCodeUrl方法如下
#region 重新獲取Code的跳轉(zhuǎn)鏈接(沒有用戶授權(quán)的,只能獲取基本信息) /// <summary>重新獲取Code,以后面實(shí)現(xiàn)帶著Code重新跳回目標(biāo)頁面(沒有用戶授權(quán)的,只能獲取基本信息(openid))</summary> /// <param name="url">目標(biāo)頁面</param> /// <returns></returns> public static string GetCodeUrl(string url) { string CodeUrl = ""; //對url進(jìn)行編碼 url = System.Web.HttpUtility.UrlEncode(url); CodeUrl = string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + Appid + "&redirect_uri=" + url + "?action=viewtest&response_type=code&scope=snsapi_base&state=1#wechat_redirect"); return CodeUrl; } #endregion
上文中 GetOauthAccessOpenId方法如下
#region 以Code換取用戶的openid、access_token /// <summary>根據(jù)Code獲取用戶的openid、access_token</summary> public static string GetOauthAccessOpenId(string code) { Log log = new Log(AppDomain.CurrentDomain.BaseDirectory + @"/log/Log.txt"); string Openid = ""; string url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + Appid + "&secret=" + Secret + "&code=" + code + "&grant_type=authorization_code"; string gethtml = MyHttpHelper.HttpGet(url); log.log("拿到的url是:" + url); log.log("獲取到的gethtml是" + gethtml); OAuth_Token ac = new OAuth_Token(); ac = JsonHelper.ToObject<OAuth_Token>(gethtml); log.log("能否從html里拿到openid=" + ac.openid); Openid = ac.openid; return Openid; } #endregion
通過以上方法即可拿到用戶的Openid,如上文所示,用戶id保存在System.Web.HttpContext.Current.Session["openid"] 中,所以獲取也是非常簡單
在需要獲取的地方執(zhí)行
#region 獲取當(dāng)前用戶Openid ReGetOpenId(); log.log("走完獲取openid的方法之后,當(dāng)前Session的值是:" + System.Web.HttpContext.Current.Session["openid"]); #endregion
注:上文中 OAuth_Token 類如下:
public class OAuth_Token { /// <summary> /// 網(wǎng)頁授權(quán)接口調(diào)用憑證,注意:此access_token與基礎(chǔ)支持的access_token不同 /// </summary> public string access_token { get; set; } /// <summary> /// access_token接口調(diào)用憑證超時(shí)時(shí)間,單位(秒) /// </summary> public string expires_in { get; set; } /// <summary> /// 用戶刷新access_token /// </summary> public string refresh_token { get; set; } /// <summary> /// 用戶唯一標(biāo)識,請注意,在未關(guān)注公眾號時(shí),用戶訪問公眾號的網(wǎng)頁,也會(huì)產(chǎn)生一個(gè)用戶和公眾號唯一的OpenID /// </summary> public string openid { get; set; } /// <summary> /// 用戶授權(quán)作用域 /// </summary> public string scope { get; set; } }
日志文件
用到的簡單日志類也順便提供放上來:
/// <summary> /// 日志類 /// </summary> public class Log { private string logFile; private StreamWriter writer; private FileStream fileStream = null; public Log(string fileName) { logFile = fileName; CreateDirectory(logFile); } public void log(string info) { try { System.IO.FileInfo fileInfo = new System.IO.FileInfo(logFile); if (!fileInfo.Exists) { fileStream = fileInfo.Create(); writer = new StreamWriter(fileStream); } else { fileStream = fileInfo.Open(FileMode.Append, FileAccess.Write); writer = new StreamWriter(fileStream); } writer.WriteLine(DateTime.Now + ": " + info); } finally { if (writer != null) { writer.Close(); writer.Dispose(); fileStream.Close(); fileStream.Dispose(); } } } public void CreateDirectory(string infoPath) { DirectoryInfo directoryInfo = Directory.GetParent(infoPath); if (!directoryInfo.Exists) { directoryInfo.Create(); } } }
調(diào)用呢,很簡單,調(diào)用方法如下:
Log log = new Log(AppDomain.CurrentDomain.BaseDirectory + @"/log/Log.txt"); log.log("我會(huì)被輸入在日志文件中")
最后呢,拿到當(dāng)前用戶Openid,就可以從數(shù)據(jù)庫再次獲取到該用戶的其他基本信息。從而可以更好的輔助你完成你項(xiàng)目中其他的業(yè)務(wù)模塊。

熱AI工具

Undress AI Tool
免費(fèi)脫衣服圖片

Undresser.AI Undress
人工智能驅(qū)動(dòng)的應(yīng)用程序,用于創(chuàng)建逼真的裸體照片

AI Clothes Remover
用于從照片中去除衣服的在線人工智能工具。

Clothoff.io
AI脫衣機(jī)

Video Face Swap
使用我們完全免費(fèi)的人工智能換臉工具輕松在任何視頻中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費(fèi)的代碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
功能強(qiáng)大的PHP集成開發(fā)環(huán)境

Dreamweaver CS6
視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版
神級代碼編輯軟件(SublimeText3)