.Net Oauth2.0 第三方登陸開發(Facebook ,LinkedIn )

需求:OAuth2實現第三方網站受權並獲取其相關數據來實現登陸等功能git

暫時支持Facebook ,LinkedIn ,基本大同小異,只是返回時的數據不一樣,需根據具體返回類型進行相應處理github

 

1.OAuth2認證流程網站

OAuth2認證協議涉及3方(應用、用戶和服務方),加之流程較爲繁瑣,實現命名不盡相同,url

容易忘記和混淆,簡述認證流程以下spa

一、向使用OAuth2認證的服務方申請應用,獲取應用的client_id(應用惟一標識)和client_secret(應用私鑰)code

二、使用key/secret向服務方請求用戶受權Token(code也就是authorization_code)blog

三、使用用戶受權Token換取用戶信息訪問Token(access_token ),token

四、使用access_token(用戶信息訪問令牌)獲取相關信息ip

 

2.受權訪問流程get

一、向第三方平臺申請訪問權限獲得(client_id和client_secret)

二、填寫Oauth2.0本站返回連接

三、向第三方平臺發送受權請求

四、再返回url中進行業務潮處理

注意:申請的網址須要與實際訪問的url保持一致

 

3. AuthHelper代碼

    public abstract class AuthHelper
    {
        public static AuthToken GetToken(string code, string token_url, string cliend_id, string client_secret, string return_url)
        {
            var strResult = GetTokenStr(code, token_url, cliend_id, client_secret, return_url);
            try
            {
                var res = JsonConvert.DeserializeObject<AuthToken>(strResult);
                return res;
            }
            catch (Exception ex)
            {
                Tool.Log.Write(ex.ToString());
            }
            return default(AuthToken);
        }

        /// <summary>
        /// 向第三方平臺發送獲取token請求
        /// </summary>
        /// <param name="code"></param>
        /// <param name="token_url"></param>
        /// <param name="cliend_id"></param>
        /// <param name="client_secret"></param>
        /// <param name="return_url"></param>
        /// <returns></returns>
        public static string GetTokenStr(string code, string token_url, string cliend_id, string client_secret, string return_url)
        {
            Dictionary<string, string> dicPara = new Dictionary<string, string>();
            dicPara.Add("grant_type", "authorization_code");
            dicPara.Add("code", code);
            dicPara.Add("redirect_uri", return_url);
            dicPara.Add("client_id", cliend_id);
            dicPara.Add("client_secret", client_secret);

            var token = WebApiHelper.PostResponseStr(token_url, dicPara);
            return token;
        }

        /// <summary>
        /// header中發送token
        /// </summary>
        /// <param name="accessToken"></param>
        /// <param name="profile_url"></param>
        /// <returns></returns>
        public static string GetProFileAuth(string accessToken, string profile_url)
        {
            Dictionary<string, string> dicAuth = new Dictionary<string, string>();
            dicAuth.Add("Authorization", "Bearer " + accessToken);
            var profile = WebApiHelper.GetResponseStr(profile_url, null, dicAuth);
            return profile;
        }
        /// <summary>
        /// get方式獲取token
        /// </summary>
        /// <param name="accessToken"></param>
        /// <param name="profile_url"></param>
        /// <returns></returns>
        public static string GetProFileStr(string accessToken, string profile_url)
        {
            Dictionary<string, string> dicQuery = new Dictionary<string, string>();
            dicQuery.Add("access_token", accessToken);
            var profile = WebApiHelper.GetResponseStr(profile_url, dicQuery, null);
            return profile;
        }
    }

4.返回業務處理

   public ActionResult ReturnLinkedin()
        {
            string description = string.Empty;

            string code = RequestString("code");
            string state = RequestString("state");
            string error = RequestString("error");
            string error_description = RequestString("error_description");
            if (code == "" || error != "")
            {
                if (code == "user_cancelled_authorize" || code == "user_cancelled_login ")
                {
                    description = code;
                }
                else
                    description = error != "" ? error_description : "no authentication !";
            }
            else
            {
                var res = Tools.Auth.LinkinHelper.GetToken(code, Tools.Auth.LinkinConfig.ReturnUrl);
                if (res.access_token != "")
                {
                    var entity = Tools.Auth.LinkinHelper.GetProFileStr(res.access_token, Tools.Auth.LinkinConfig.ProfileResourceUrl);
                    description = entity;
            /***具體業務處理
            **/ }
else { description = "access token error"; } } ViewBag.Description = description; return View(); }

Github地址:https://github.com/willianchen/Chml.Oauth

第一次發博客 ,有疑問或者有建議的請留言

相關文章
相關標籤/搜索