現(xiàn)在QQ第三方快捷登錄在網(wǎng)站/App中的應(yīng)用已經(jīng)非常普遍,QQ快捷登錄不僅可以減少用戶登錄、注冊平臺所花費(fèi)的時間,而且不需要牢記平臺賬戶密碼,對于提升用戶體驗(yàn)可謂是錦上添花(注:本人是記不住任何網(wǎng)站賬號密碼的)。
當(dāng)然,有很多站點(diǎn)沒有使用任何第三方登錄功能,這里不做過多評價。
下面看下C#中如何實(shí)現(xiàn)QQ一鍵登錄功能 沒有引用任何第三方封裝的幫助類庫,全部源碼開放,結(jié)尾處有DEMO案例,請自行下載。
第一步:請自行到QQ互聯(lián)(https://connect.qq.com)申請開發(fā)者權(quán)限,創(chuàng)建屬于自己網(wǎng)站的應(yīng)用
對于這一步 這里不做過多敘述,進(jìn)入QQ互聯(lián)按照步驟填寫資料后 等待審核就行。
第二步:這里視作第一步已經(jīng)完成 并取得了APP ID 和 APP Key并已配置好網(wǎng)站回調(diào)地址,廢話不多說 直接上代碼來看。
下面為QQ互聯(lián)交互處理幫助類,請自行換成自己的ID和Key。
using System; using System.Collections.Generic; using System.Text; using System.Collections.Specialized; using System.Configuration; using System.Web; using System.Web.Script.Serialization; using System.Text.RegularExpressions; namespace OAuthQQ.open { /// <summary> /// C#實(shí)現(xiàn)QQ快捷登錄網(wǎng)站DEMO /// 來源:hnxxbl.cn /// </summary> public sealed class QQLogin { private const string AppId = "xxxxx";//QQ互聯(lián)中申請的APPID private const string AppKey = "xxxxxxxx";//QQ互聯(lián)中申請的APP Key private const string RequestAuthorizeUrl = "https://graph.qq.com/oauth2.0/authorize"; private const string RequestAccessTokenUrl = "https://graph.qq.com/oauth2.0/token"; private const string RequestOpenIdUrl = "https://graph.qq.com/oauth2.0/me"; private const string RequestUserInfoUrl = "https://graph.qq.com/user/get_user_info"; public static string getLoginUrl(string state) { return RequestAuthorizeUrl "?response_type=code&client_id=" AppId "&redirect_uri=" getCallBackUrl() "&state=" state; } public static string getCallBackUrl() { return HttpUtility.UrlEncode("http://www.xxxxxx.com/open/open.aspx");//此處鏈接務(wù)必修改為自己網(wǎng)站對應(yīng)鏈接 } private string getTokenUrl(string code) { string ret = Utils.httpGet(RequestAccessTokenUrl "?grant_type=authorization_code&client_id=" AppId "&client_secret=" AppKey "&code=" code "&redirect_uri=" getCallBackUrl()); return HttpUtility.ParseQueryString(ret).Get("access_token"); } private QQOpenIdInfo getOpenId(string accessToken) { string ret = Utils.httpGet(RequestOpenIdUrl "?access_token=" accessToken); if (!ret.StartsWith("callback")) { return null; } JavaScriptSerializer jss = new JavaScriptSerializer(); int start = ret.IndexOf("(") 1; int end = ret.IndexOf(")"); return jss.Deserialize<QQOpenIdInfo>(ret.Substring(start, end - start)); } private QQUserInfo getUserInfo(string access_token, string openId) { string ret = Utils.httpGet(RequestUserInfoUrl "?access_token=" access_token "&oauth_consumer_key=" AppId "&openid=" openId); JavaScriptSerializer jss = new JavaScriptSerializer(); return jss.Deserialize<QQUserInfo>(ret); } public OpenInfo login(string code) { OpenInfo info = new OpenInfo(); try { string accessToken = getTokenUrl(code); QQOpenIdInfo qqOpenIdInfo = getOpenId(accessToken); if (qqOpenIdInfo == null) { return null; } QQUserInfo userInfo = getUserInfo(accessToken, qqOpenIdInfo.OpenId); info.OpenId = qqOpenIdInfo.OpenId; info.NickName = userInfo.NickName; info.Photo = userInfo.Figureurl_1; } catch (Exception e) { info.ErrorMsg = e.ToString(); info.HasError = false; } return info; } } }下面來看下交互處理調(diào)用方法(當(dāng)然還有一些其他輔助類,這里不做過多解釋,請下載源碼附件自行查看)
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace OAuthQQ.open { /// <summary> /// 首頁 運(yùn)行此頁面跳轉(zhuǎn)測試(為保證測試效果,請務(wù)必在配置完整后在公網(wǎng)環(huán)境進(jìn)行) /// 來源:hnxxbl.cn /// </summary> public partial class qq : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string state = Utils.getRandomNumber().ToString(); HttpCookie loginCookie = new HttpCookie("OAuthQQLogin", state); Response.Cookies.Add(loginCookie); Response.Redirect(QQLogin.getLoginUrl(state)); } } }
第三步:進(jìn)行回調(diào)地址頁面邏輯編寫,代碼已奉上 請查閱
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace OAuthQQ.open { /// <summary> /// 此頁面為登錄成功后的回調(diào)地址,此頁面地址需要在QQ互聯(lián)接口基本信息中預(yù)先配置,否則無法使用 /// 來源:hnxxbl.cn /// </summary> public partial class open : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string state = Request.QueryString["state"]; string code = Request.QueryString["code"]; if (code == null) { //參數(shù)不全不處理請求 ,跳轉(zhuǎn)到首頁 Response.Redirect("http://www.xxxxxx.com/"); } OpenInfo info = null;//當(dāng)前登錄用戶信息 HttpCookie cookie = Request.Cookies["OAuthQQLogin"]; //qq登錄 if (cookie.Value == state) { QQLogin qqLogin = new QQLogin(); info = qqLogin.login(code); } if (info.HasError) { //出錯 Response.Write("出錯" Server.UrlEncode(info.ErrorMsg)); } //至此 若info!=null 并且OpenId不為空,則登錄成功 if (info != null && !string.IsNullOrEmpty(info.OpenId)) { //登錄成功,獲取用戶信息 //請?jiān)诖颂幚砟牡卿?注冊業(yè)務(wù)邏輯 string query = "photo=" info.Photo "&nickname=" info.NickName "&openid=" info.OpenId; Response.Write(query); } } } }
第四步:說那么多廢話,其實(shí)以上代碼只需要配置AppID 、APPKey、回調(diào)地址即可。其他登錄、注冊邏輯請自行處理。
完成以上代碼配置后,就可以進(jìn)行線上測試,為保證測試效果,請務(wù)必在配置完整后在公網(wǎng)環(huán)境進(jìn)行。如有疑問 請進(jìn)入技術(shù)群咨詢?nèi)褐鳌?/span>
第五步:第五步已經(jīng)沒啥能說的了,下面來看下源碼目錄結(jié)構(gòu)。
點(diǎn)擊下載 C#實(shí)現(xiàn)QQ快捷登錄注冊案例源碼 回家自行研究吧??次艺f的再多,不如自己去實(shí)戰(zhàn)下來的爽快。
原文鏈接:C#實(shí)現(xiàn)QQ快捷登錄注冊案例