免费观看又色又爽又黄的小说免费_美女福利视频国产片_亚洲欧美精品_美国一级大黄大色毛片

微信公眾號開發網頁中如何獲取當前用戶Openid-創新互聯

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

成都創新互聯公司是一家集網站建設,鶴峰企業網站建設,鶴峰品牌網站建設,網站定制,鶴峰網站建設報價,網絡營銷,網絡優化,鶴峰網站推廣為一體的創新建站企業,幫助傳統企業提升企業形象加強企業競爭力。可充分滿足這一群體相比中小企業更為豐富、高端、多元的互聯網需求。同時我們時刻保持專業、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學習、思考、沉淀、凈化自己,讓我們為更多的企業打造出實用型網站。

具體實現

首先,我們定一個獲取openid的方法 ReGetOpenId

public static void ReGetOpenId()
        {
            string url = System.Web.HttpContext.Current.Request.Url.AbsoluteUri;//獲取當前url
            if (System.Web.HttpContext.Current.Session["openid"] == "" || System.Web.HttpContext.Current.Session["openid"] == null)
            {
                //先要判斷是否是獲取code后跳轉過來的
                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);//先跳轉到微信的服務器,取得code后會跳回來這頁面的

                }
                else
                {
                    //Code非空,已經獲取了code后跳回來啦,現在重新獲取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好是帶域名的,花生殼的域名是行不通的,再調微信平臺接口的時候,會報鏈接不正確錯誤

上文中GetCodeUrl方法如下

#region 重新獲取Code的跳轉鏈接(沒有用戶授權的,只能獲取基本信息)
        /// <summary>重新獲取Code,以后面實現帶著Code重新跳回目標頁面(沒有用戶授權的,只能獲取基本信息(openid))</summary>
        /// <param name="url">目標頁面</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>根據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"]  中,所以獲取也是非常簡單

在需要獲取的地方執行

#region 獲取當前用戶Openid
                ReGetOpenId();
                log.log("走完獲取openid的方法之后,當前Session的值是:" + System.Web.HttpContext.Current.Session["openid"]);
                #endregion

注:上文中 OAuth_Token 類如下:

public class OAuth_Token
    {
        /// <summary>
        /// 網頁授權接口調用憑證,注意:此access_token與基礎支持的access_token不同
        /// </summary>
        public string access_token { get; set; }
        /// <summary>
        /// access_token接口調用憑證超時時間,單位(秒)
        /// </summary>
        public string expires_in { get; set; }
        /// <summary>
        /// 用戶刷新access_token
        /// </summary>
        public string refresh_token { get; set; }
        /// <summary>
        /// 用戶標識,請注意,在未關注公眾號時,用戶訪問公眾號的網頁,也會產生一個用戶和公眾號的OpenID
        /// </summary>
        public string openid { get; set; }
        /// <summary>
        /// 用戶授權作用域
        /// </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();
            }
        }
    }

調用呢,很簡單,調用方法如下:

   Log log = new Log(AppDomain.CurrentDomain.BaseDirectory + @"/log/Log.txt");
        log.log("我會被輸入在日志文件中")

最后呢,拿到當前用戶Openid,就可以從數據庫再次獲取到該用戶的其他基本信息。從而可以更好的輔助你完成你項目中其他的業務模塊。

感謝你能夠認真閱讀完這篇文章,希望小編分享的“微信公眾號開發網頁中如何獲取當前用戶Openid”這篇文章對大家有幫助,同時也希望大家多多支持創新互聯網站建設公司,,關注創新互聯行業資訊頻道,更多相關知識等著你來學習!

當前標題:微信公眾號開發網頁中如何獲取當前用戶Openid-創新互聯
文章位置:http://m.newbst.com/article16/cejcgg.html

成都網站建設公司_創新互聯,為您提供網站設計公司外貿網站建設手機網站建設企業網站制作App開發外貿建站

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

成都網站建設公司