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

來源:互聯網
上載者:User

標籤:lin   rip   nconf   var   處理   bsp   cti   desc   token   

需求:OAuth2實現第三方網站授權並擷取其相關資料來實現登入等功能

暫時支援Facebook ,LinkedIn ,基本大同小異,只是返回時的資料不同,需根據具體傳回型別進行相應處理

 

1.OAuth2認證流程

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

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

1、向使用OAuth2認證的服務方申請應用,擷取應用的client_id(應用唯一標識)和client_secret(應用私密金鑰)

2、使用key/secret向服務方請求使用者授權Token(code也就是authorization_code)

3、使用使用者授權Token換取使用者資訊訪問Token(access_token ),

4、使用access_token(使用者資訊存取權杖)擷取相關資訊

 

2.授權訪問流程

1、向第三方平台申請存取權限得到(client_id和client_secret)

2、填寫Oauth2.0本站返回連結

3、向第三方平台發送授權請求

4、再返回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

第一次發部落格 ,有疑問或者有建議的請留言

 

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.