這篇文章主要介紹了微信公眾號開發(fā)網(wǎng)頁中如何獲取當(dāng)前用戶Openid,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

具體實現(xiàn)
首先,我們定一個獲取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為空時,先獲取Code
string GetCodeUrls = GetCodeUrl(url);
System.Web.HttpContext.Current.Response.Redirect(GetCodeUrls);//先跳轉(zhuǎn)到微信的服務(wù)器,取得code后會跳回來這頁面的
}
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)微信平臺接口的時候,會報鏈接不正確錯誤
上文中GetCodeUrl方法如下
#region 重新獲取Code的跳轉(zhuǎn)鏈接(沒有用戶授權(quán)的,只能獲取基本信息)
/// <summary>重新獲取Code,以后面實現(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進行編碼
url = System.Web.HttpUtility.UrlEncode(url);
CodeUrl = string.Format("https://open.weixin.qq.com/connect/oauth3/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 = "/tupian/20230522/access_token
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)用憑證超時時間,單位(秒)
/// </summary>
public string expires_in { get; set; }
/// <summary>
/// 用戶刷新access_token
/// </summary>
public string refresh_token { get; set; }
/// <summary>
/// 用戶標(biāo)識,請注意,在未關(guān)注公眾號時,用戶訪問公眾號的網(wǎng)頁,也會產(chǎn)生一個用戶和公眾號的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("我會被輸入在日志文件中")最后呢,拿到當(dāng)前用戶Openid,就可以從數(shù)據(jù)庫再次獲取到該用戶的其他基本信息。從而可以更好的輔助你完成你項目中其他的業(yè)務(wù)模塊。
感謝你能夠認真閱讀完這篇文章,希望小編分享的“微信公眾號開發(fā)網(wǎng)頁中如何獲取當(dāng)前用戶Openid”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,,關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!
當(dāng)前標(biāo)題:微信公眾號開發(fā)網(wǎng)頁中如何獲取當(dāng)前用戶Openid-創(chuàng)新互聯(lián)
文章位置:http://chinadenli.net/article16/cejcgg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計公司、外貿(mào)網(wǎng)站建設(shè)、手機網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作、App開發(fā)、外貿(mào)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容