中文字幕精品亚洲无线码二区,国产黄a三级三级三级看三级,亚洲七七久久桃花影院,丰满少妇被猛烈进入,国产小视频在线观看网站

WebApi系列(lie)~QQ互聯的引入(ru)(QConnectSDK)

回到目錄

感謝與改進

首先要感(gan)謝(xie)張善友老兄為(wei)大家封裝的這個DLL,它將QQ官(guan)(guan)方的相關API都集成到了這個里(li)(li)面(mian),這對于開發人(ren)員來說,是(shi)個福音,有(you)人(ren)會說,為(wei)什么QQ官(guan)(guan)方沒有(you)提供.net版的SDK呢,在這里(li)(li),我想說,可能是(shi)騰訊公(gong)司(si)沒有(you)人(ren)會.net吧,哈哈!

玩笑話,在使用善友兄的QConnectSDK時,也遇到了一些問題,如session持久化問題,有人會說,session可以持久化所有對象,當然,這句話在某種情況下是正確的,但當你的session持久化方式改變后,如,使用sqlserver來存儲信息時(可能是為了跨站點進行信息共享吧,呵呵)你的session就不允許使用無法序列化的對象或 MarshalByRef 對象.這是個很嚴重的問題,在我今天介紹的架構里,解決了它,主要思想是使用sessionID和cache來代替session來存儲某些對象的.

插件DLL

Newtonsoft.Json.dll

QConnectSDK.dll

RestSharp.dll

DLL下載

注意,它們之前是相互依(yi)賴的,所以,要考慮到版本的兼容性

代碼相關

C#代碼

     /// <summary>
        /// QQ登陸頁面
        /// </summary>
        [HttpGet]
        public ActionResult QQLogin(string returnUrl)
        {
            if (!string.IsNullOrWhiteSpace(returnUrl))
            {
                System.Web.HttpRuntime.Cache.Insert(Session.SessionID + "RETURNURL", returnUrl);
            }
            var context = new QzoneContext();
            string state = Guid.NewGuid().ToString().Replace("-", "");
            System.Web.HttpRuntime.Cache.Insert(Session.SessionID + "requeststate", state);//一個請求(qiu)狀態碼,寫入session,在redirectUri時進行(xing)比較
            string scope = "get_user_info,add_share,list_album,upload_pic,check_page_fans,
                add_t,add_pic_t,del_t,get_repost_list,get_info,get_other_info,
                get_fanslist,get_idolist,add_idol,del_idol,add_one_blog,add_topic,get_tenpay_addr
"; var authenticationUrl = context.GetAuthorizationUrl(state, scope);  return new RedirectResult(authenticationUrl); } /// <summary> /// 回(hui)調頁(ye)面 /// </summary> public ActionResult QQConnect() { if (Request.Params["code"] != null) { QOpenClient qzone = null; string url = Url.Action("index", "home"); var verifier = Request.Params["code"]; var state = Request.Params["state"];  System.Web.HttpRuntime.Cache.Insert(Session.SessionID + "verifier", verifier); string requestState = System.Web.HttpRuntime.Cache.Get(Session.SessionID + "requeststate").ToString(); if (state == requestState) { qzone = new QOpenClient(verifier, state); var currentUser = qzone.GetCurrentUser(); if (System.Web.HttpRuntime.Cache.Get(Session.SessionID + "QzoneOauth") == null) { System.Web.HttpRuntime.Cache.Insert(Session.SessionID + "QzoneOauth", qzone); } if (!string.IsNullOrWhiteSpace((System.Web.HttpRuntime.Cache.Get(Session.SessionID + "RETURNURL") ?? string.Empty).ToString()))  { url = System.Web.HttpRuntime.Cache.Get(Session.SessionID + "RETURNURL").ToString(); } ViewBag.friendlyName = currentUser.Nickname;  ViewBag.img = currentUser.Figureurl;  } } return View(); }

HTML & JS代碼

<h2><a id="logins">qq</a></h2>
<h1>@Request.QueryString["friendlyName"]</h1>
<img  src="@Request.QueryString["img"]"/>

<script type="text/ecmascript">
    $(function () {
        $("#logins").live("click", function () {
            window.open('/Home/QQLogin', 'newwindow', 'height=400,width=400,top=400,left=400,toolbar=no,menubar=no,scrollbars=no, resizable=no,location=no, status=no');
        });
    });
</script>

程序截圖

改進的地方

這個(ge)程(cheng)序事(shi)(shi)實上(shang)也(ye)(ye)是(shi)有問題的(de)(de)(de)(de),因應當有多臺WEB服務器作負載均衡時,它的(de)(de)(de)(de)session_ID也(ye)(ye)是(shi)不(bu)(bu)同(tong)的(de)(de)(de)(de),這時,就(jiu)會出現問題了,所以(yi),最好還是(shi)使用(yong)session來(lai)做這事(shi)(shi),我(wo)試著把復雜(za)對象的(de)(de)(de)(de)存儲去掉了,QQ登(deng)陸也(ye)(ye)是(shi)可(ke)以(yi)的(de)(de)(de)(de),不(bu)(bu)知道善(shan)友用(yong)這個(ge)持久化干什(shen)么用(yong)的(de)(de)(de)(de).

修改后的代碼如下

/// <summary>
        /// QQ登(deng)陸(lu)頁面
        /// </summary>
        [HttpGet]
        public ActionResult QQLogin(string returnUrl)
        {
            if (!string.IsNullOrWhiteSpace(returnUrl))
            {
                Session["RETURNURL"] = returnUrl;
            }
            var context = new QzoneContext();
            string state = Guid.NewGuid().ToString().Replace("-", "");
            Session["requeststate"] = state;//一(yi)個(ge)請求狀態(tai)碼,寫入session,在(zai)redirectUri時進行比較
            string scope = "get_user_info,add_share,list_album,upload_pic,check_page_fans,add_t,
add_pic_t,del_t,get_repost_list,get_info,get_other_info,get_fanslist,
 get_idolist,add_idol,del_idol,add_one_blog,add_topic,get_tenpay_addr
"; var authenticationUrl = context.GetAuthorizationUrl(state, scope); return new RedirectResult(authenticationUrl);  } /// <summary> /// 回(hui)調頁(ye)面 /// </summary> public ActionResult QQConnect() { if (Request.Params["code"] != null) { QOpenClient qzone = null; string url = Url.Action("index", "home"); var verifier = Request.Params["code"]; var state = Request.Params["state"]; Session["verifier"] = verifier; string requestState = Session["requeststate"].ToString(); if (state == requestState) { qzone = new QOpenClient(verifier, state);  var currentUser = qzone.GetCurrentUser(); // Session["QzoneOauth"] = qzone; //不支(zhi)持session持久化sqlserver方法 if (!string.IsNullOrWhiteSpace((Session["RETURNURL"] ?? string.Empty).ToString())) {  url = Session["RETURNURL"].ToString(); }   ViewBag.friendlyName = currentUser.Nickname; ViewBag.img = currentUser.Figureurl; } }  return View(); }

回到目錄

posted @ 2014-07-15 16:35  張占嶺  閱讀(5163)  評論(4)    收藏  舉報