第三方OAuth授權登錄,QQ、微信(WeChat)、微博、GitHub、碼雲(Gitee)、淘寶(天貓)、微軟(Microsoft )、釘釘、谷歌(Google)、支付寶(AliPay)、StackOverflow

来源:https://www.cnblogs.com/netnr/archive/2019/12/11/12020597.html

Netnr.Login 第三方OAuth授權登錄 支持第三方登錄 三方參考文檔 參考文檔 參考文檔 參考文檔 參考文檔 參考文檔 參考文檔 參考文檔 參考文檔 參考文檔 參考文檔 參考文檔 安裝 (NuGet) 修改配置信息(密鑰、回調等) 提醒:一般所有第三方登錄都有一個 state 參數,用於防 ...


Netnr.Login

第三方OAuth授權登錄

支持第三方登錄

三方參考文檔
參考文檔
參考文檔
參考文檔
參考文檔
參考文檔
參考文檔
參考文檔
參考文檔
參考文檔
參考文檔
參考文檔

安裝 (NuGet)

Install-Package Netnr.Login

修改配置信息(密鑰、回調等)

提醒:一般所有第三方登錄都有一個 state 參數,用於防止CSRF攻擊(防偽),可以利用該參數添加 登錄、註冊 的標註首碼

框架

  • .NETStandard 2.1
  • .NETFramework 4.0

使用

/*
 * 這是測試代碼,只為調通每一個介面,拿到 唯一標識
 * 實際應用中還要處理昵稱、郵箱、頭像等,可參考個站開源項目:https://github.com/netnr/blog
 */

using System;

namespace Netnr.Login.Sample
{
    class Program
    {
        static void Main(string[] args)
        {
            var lc = new LoginClient(LoginBase.LoginType.StackOverflow);

            //拷貝授權鏈接在瀏覽器打開,授權後拿到code,並手動賦值,手動賦值需解碼
            var url = lc.Auth();

            var ar = new LoginBase.AuthorizeResult();
            ar.code = "";
            //此處打斷點,賦值上面拿到的code再繼續
            ar.code = ar.code.ToDecode();

            lc.AuthCallback(ar);
        }

        public class LoginClient
        {
            private LoginBase.LoginType? loginType;

            public LoginClient(LoginBase.LoginType _loginType)
            {
                loginType = _loginType;

                // 配置
                QQConfig.APPID = "XXX";
                QQConfig.APPKey = "XXX";
                //回調地址,與申請填寫的地址保持一致
                QQConfig.Redirect_Uri = "https://rf2.netnr.com/account/authcallback/qq";

                WeChatConfig.AppId = "";
                WeChatConfig.AppSecret = "";
                WeChatConfig.Redirect_Uri = "";

                WeiboConfig.AppKey = "";
                WeiboConfig.AppSecret = "";
                WeiboConfig.Redirect_Uri = "";

                GitHubConfig.ClientID = "";
                GitHubConfig.ClientSecret = "";
                GitHubConfig.Redirect_Uri = "";
                //申請的應用名稱,非常重要
                GitHubConfig.ApplicationName = "netnrf";

                TaoBaoConfig.AppKey = "";
                TaoBaoConfig.AppSecret = "";
                TaoBaoConfig.Redirect_Uri = "";

                MicroSoftConfig.ClientID = "";
                MicroSoftConfig.ClientSecret = "";
                MicroSoftConfig.Redirect_Uri = "";

                DingTalkConfig.appId = "";
                DingTalkConfig.appSecret = "";
                DingTalkConfig.Redirect_Uri = "";

                GiteeConfig.ClientID = "";
                GiteeConfig.ClientSecret = "";
                GiteeConfig.Redirect_Uri = "";

                GoogleConfig.ClientID = "";
                GoogleConfig.ClientSecret = "";
                GoogleConfig.Redirect_Uri = "";

                AliPayConfig.AppId = "";
                AliPayConfig.AppPrivateKey = "";
                AliPayConfig.Redirect_Uri = "";

                StackOverflowConfig.ClientId = "";
                StackOverflowConfig.ClientSecret = "";
                StackOverflowConfig.Key = "";
                StackOverflowConfig.Redirect_Uri = "";
            }

            /// <summary>
            /// 生成請求鏈接
            /// </summary>
            /// <param name="authType">在防偽參數追加信息(可用於登錄、註冊、綁定、解綁區分)</param>
            /// <returns></returns>
            public string Auth(string authType = "")
            {
                var url = string.Empty;

                switch (loginType)
                {
                    case LoginBase.LoginType.QQ:
                        {
                            var reqe = new QQ_Authorization_RequestEntity();
                            if (!string.IsNullOrWhiteSpace(authType))
                            {
                                reqe.state = authType + reqe.state;
                            }
                            url = QQ.AuthorizationHref(reqe);
                        }
                        break;
                    case LoginBase.LoginType.WeiBo:
                        {
                            var reqe = new Weibo_Authorize_RequestEntity();
                            if (!string.IsNullOrWhiteSpace(authType))
                            {
                                reqe.state = authType + reqe.state;
                            }
                            url = Weibo.AuthorizeHref(reqe);
                        }
                        break;
                    case LoginBase.LoginType.GitHub:
                        {
                            var reqe = new GitHub_Authorize_RequestEntity();
                            if (!string.IsNullOrWhiteSpace(authType))
                            {
                                reqe.state = authType + reqe.state;
                            }
                            url = GitHub.AuthorizeHref(reqe);
                        }
                        break;
                    case LoginBase.LoginType.TaoBao:
                        {
                            var reqe = new TaoBao_Authorize_RequestEntity();
                            if (!string.IsNullOrWhiteSpace(authType))
                            {
                                reqe.state = authType + reqe.state;
                            }
                            url = TaoBao.AuthorizeHref(reqe);
                        }
                        break;
                    case LoginBase.LoginType.MicroSoft:
                        {
                            var reqe = new MicroSoft_Authorize_RequestEntity();
                            if (!string.IsNullOrWhiteSpace(authType))
                            {
                                reqe.state = authType + reqe.state;
                            }
                            url = MicroSoft.AuthorizeHref(reqe);
                        }
                        break;
                    case LoginBase.LoginType.WeChat:
                        {
                            var reqe = new WeChat_Authorization_RequestEntity();
                            if (!string.IsNullOrWhiteSpace(authType))
                            {
                                reqe.state = authType + reqe.state;
                            }
                            url = WeChat.AuthorizationHref(reqe);
                        }
                        break;
                    case LoginBase.LoginType.DingTalk:
                        {
                            var reqe = new DingTalk_Authorize_RequestEntity();
                            if (!string.IsNullOrWhiteSpace(authType))
                            {
                                reqe.state = authType + reqe.state;
                            }
                            //掃描模式
                            url = DingTalk.AuthorizeHref_ScanCode(reqe);

                            //密碼模式
                            //url = DingTalk.AuthorizeHref_Password(reqe);
                        }
                        break;
                    case LoginBase.LoginType.Gitee:
                        {
                            var reqe = new Gitee_Authorize_RequestEntity();
                            if (!string.IsNullOrWhiteSpace(authType))
                            {
                                reqe.state = authType + reqe.state;
                            }
                            url = Gitee.AuthorizeHref(reqe);
                        }
                        break;
                    case LoginBase.LoginType.Google:
                        {
                            var reqe = new Google_Authorize_RequestEntity();
                            if (!string.IsNullOrWhiteSpace(authType))
                            {
                                reqe.state = authType + reqe.state;
                            }
                            url = Google.AuthorizeHref(reqe);
                        }
                        break;
                    case LoginBase.LoginType.AliPay:
                        {
                            var reqe = new AliPay_Authorize_RequestEntity();
                            if (!string.IsNullOrWhiteSpace(authType))
                            {
                                reqe.state = authType + reqe.state;
                            }
                            url = AliPay.AuthorizeHref(reqe);
                        }
                        break;
                    case LoginBase.LoginType.StackOverflow:
                        {
                            var reqe = new StackOverflow_Authorize_RequestEntity();
                            if (!string.IsNullOrWhiteSpace(authType))
                            {
                                reqe.state = authType + reqe.state;
                            }
                            url = StackOverflow.AuthorizeHref(reqe);
                        }
                        break;
                }

                return url;
            }

            /// <summary>
            /// 回調方法
            /// </summary>
            /// <param name="authorizeResult">接收授權碼、防偽參數</param>
            public void AuthCallback(LoginBase.AuthorizeResult authorizeResult)
            {
                if (string.IsNullOrWhiteSpace(authorizeResult.code))
                {
                    //打開鏈接沒登錄授權
                }
                else
                {
                    //唯一標示
                    string OpenId = string.Empty;

                    switch (loginType)
                    {
                        case LoginBase.LoginType.QQ:
                            {
                                //獲取 access_token
                                var tokenEntity = QQ.AccessToken(new QQ_AccessToken_RequestEntity()
                                {
                                    code = authorizeResult.code
                                });

                                //獲取 OpendId
                                var openidEntity = QQ.OpenId(new QQ_OpenId_RequestEntity()
                                {
                                    access_token = tokenEntity.access_token
                                });

                                //獲取 UserInfo
                                _ = QQ.OpenId_Get_User_Info(new QQ_OpenAPI_RequestEntity()
                                {
                                    access_token = tokenEntity.access_token,
                                    openid = openidEntity.openid
                                });

                                //身份唯一標識
                                OpenId = openidEntity.openid;
                            }
                            break;
                        case LoginBase.LoginType.WeiBo:
                            {
                                //獲取 access_token
                                var tokenEntity = Weibo.AccessToken(new Weibo_AccessToken_RequestEntity()
                                {
                                    code = authorizeResult.code
                                });

                                //獲取 access_token 的授權信息
                                var tokenInfoEntity = Weibo.GetTokenInfo(new Weibo_GetTokenInfo_RequestEntity()
                                {
                                    access_token = tokenEntity.access_token
                                });

                                //獲取 users/show
                                _ = Weibo.UserShow(new Weibo_UserShow_RequestEntity()
                                {
                                    access_token = tokenEntity.access_token,
                                    uid = Convert.ToInt64(tokenInfoEntity.uid)
                                });

                                OpenId = tokenEntity.access_token;
                            }
                            break;
                        case LoginBase.LoginType.WeChat:
                            {
                                //獲取 access_token
                                var tokenEntity = WeChat.AccessToken(new WeChat_AccessToken_RequestEntity()
                                {
                                    code = authorizeResult.code
                                });

                                //獲取 user
                                _ = WeChat.Get_User_Info(new WeChat_OpenAPI_RequestEntity()
                                {
                                    access_token = tokenEntity.access_token,
                                    openid = tokenEntity.openid
                                });

                                //身份唯一標識
                                OpenId = tokenEntity.openid;
                            }
                            break;
                        case LoginBase.LoginType.GitHub:
                            {
                                //獲取 access_token
                                var tokenEntity = GitHub.AccessToken(new GitHub_AccessToken_RequestEntity()
                                {
                                    code = authorizeResult.code
                                });

                                //獲取 user
                                var userEntity = GitHub.User(new GitHub_User_RequestEntity()
                                {
                                    access_token = tokenEntity.access_token
                                });

                                OpenId = userEntity.id.ToString();
                            }
                            break;
                        case LoginBase.LoginType.TaoBao:
                            {
                                //獲取 access_token
                                var tokenEntity = TaoBao.AccessToken(new TaoBao_AccessToken_RequestEntity()
                                {
                                    code = authorizeResult.code
                                });

                                OpenId = tokenEntity.open_uid;
                            }
                            break;
                        case LoginBase.LoginType.MicroSoft:
                            {
                                //獲取 access_token
                                var tokenEntity = MicroSoft.AccessToken(new MicroSoft_AccessToken_RequestEntity()
                                {
                                    code = authorizeResult.code
                                });

                                //獲取 user
                                var userEntity = MicroSoft.User(new MicroSoft_User_RequestEntity()
                                {
                                    access_token = tokenEntity.access_token
                                });

                                OpenId = userEntity.id.ToString();
                            }
                            break;
                        case LoginBase.LoginType.DingTalk:
                            {
                                //獲取 user
                                var userEntity = DingTalk.User(new DingTalk_User_RequestEntity(), authorizeResult.code);

                                OpenId = userEntity?.openid;
                            }
                            break;
                        case LoginBase.LoginType.Gitee:
                            {
                                //獲取 access_token
                                var tokenEntity = Gitee.AccessToken(new Gitee_AccessToken_RequestEntity()
                                {
                                    code = authorizeResult.code
                                });

                                //獲取 user
                                var userEntity = Gitee.User(new Gitee_User_RequestEntity()
                                {
                                    access_token = tokenEntity.access_token
                                });

                                OpenId = userEntity.id.ToString();
                            }
                            break;
                        case LoginBase.LoginType.Google:
                            {
                                //獲取 access_token
                                var tokenEntity = Google.AccessToken(new Google_AccessToken_RequestEntity()
                                {
                                    code = authorizeResult.code
                                });

                                //獲取 user
                                var userEntity = Google.User(new Google_User_RequestEntity()
                                {
                                    access_token = tokenEntity.access_token
                                });

                                OpenId = userEntity.sub;
                            }
                            break;
                        case LoginBase.LoginType.AliPay:
                            {
                                //獲取 access_token
                                var tokenEntity = AliPay.AccessToken(new AliPay_AccessToken_RequestEntity()
                                {
                                    code = authorizeResult.auth_code
                                });

                                //實際上這一步已經獲取到 OpenId,登錄驗證可以了,獲取個人信息還需調用下麵的介面
                                //tokenEntity.user_id

                                //獲取 user
                                var userEntity = AliPay.User(new AliPay_User_RequestEntity()
                                {
                                    auth_token = tokenEntity.access_token
                                });

                                OpenId = userEntity.user_id;
                            }
                            break;
                        case LoginBase.LoginType.StackOverflow:
                            {
                                //獲取 access_token
                                var tokenEntity = StackOverflow.AccessToken(new StackOverflow_AccessToken_RequestEntity()
                                {
                                    code = authorizeResult.code
                                });

                                //獲取 user
                                var userEntity = StackOverflow.User(new StackOverflow_User_RequestEntity()
                                {
                                    access_token = tokenEntity.access_token
                                });

                                OpenId = userEntity.user_id;
                            }
                            break;
                    }

                    //拿到登錄標識
                    if (string.IsNullOrWhiteSpace(OpenId))
                    {
                        //TO DO
                    }
                }

            }
        }
    }
}

Source


您的分享是我們最大的動力!

更多相關文章
一周排行
  • C#實現(Delegate)的委托就不多說了,直接上代碼,看代碼中的註釋: namespace Delegate { delegate void DGSayiHi(string name);//聲明委托 delegate void DGDo(string name); class Program { ...
  • C#實現的從小到大的冒泡排序: public void BubbleSort(int[] array) { int length = array.Length; for (int i = 0; i < length - 1; i++) { for (int j = length - 1; j > i ...
  • string aa = DateTime.Now.ToShortDateString();//"2019/9/23" string bb = DateTime.Now.ToShortTimeString();//"上午 10:21" string ff = DateTime.Now.ToLongDa ...
  • 1. 什麼是,以及怎麼用畫中畫 Windows 10 Creators Update以後UWP提供了一個新的視圖模式CompactOverlay,中文翻譯成 緊湊的覆蓋層 ?反正大部分時間我們都會稱它為 畫中畫模式 。 上圖中右上角即為進入畫中畫模式的微軟“電影和電視”應用。 可以調用 "Appli ...
  • C#實現的對兩個Table進行Merge,兩表必須存在至少一個公共欄位作為連接項,否則連接就失去了意義。如下是對兩個table進行Merge的詳細代碼: private void button1_Click(object sender, EventArgs e)//Button點擊觸發事件 { #r ...
  • 在很多時候,我們做一些非常規化的界面的時候,往往需要創建一些用戶控制項,在其中繪製好一些基礎的界面塊,作為後續重覆使用的一個單元,用戶控制項同時也可以封裝處理一些簡單的邏輯。在開發Winform各種類型項目,我都時不時需要定製一些特殊的用戶控制項,以方便在界面模塊中反覆使用。我們一般是在自定義的用戶控制項里... ...
  • 第一部分:面向對象 封裝 繼承(里氏轉換) 多態第二部分:值類型、引用類型、字元串操作第三部分:集合文件操作第四部分:正則表達式第五部分:XML操作第六部分:委托、事件第七部分:反射。 //存儲著我們當前正在運行的進程//Process[] pro = Process.GetProcesses(); ...
  • 程式處理存放圖片的幾種方式 我的理解有4個: 1:放在項目本身得文件夾中,直接部署到伺服器上 2:存放在磁碟中,然後資料庫中存放路徑,讀取得時候傳路徑。這個適合小項目 3:將圖片轉換成二進位文件,但是不建議這樣做,因為會給資料庫造成壓力。 4:存放在雲存儲器上,也是在資料庫上存地址,不過是雲地址,使 ...
  • 什麼是AutoMapper?AutoMapper是一個簡單的小型庫,用於解決一個看似複雜的問題 - 擺脫將一個對象映射到另一個對象的代碼。這種類型的代碼是相當沉悶和無聊的寫,所以為什麼不發明一個工具來為我們做? 我們來看看在.netcore3.1中怎樣使用AutoMapper9.0。 Profile ...
  • Teigha中實體旋轉 代碼: using (var trans = database.TransactionManager.StartTransaction()) { Entity ent = trans.GetObject(entityId, OpenMode.ForWrite) asEntit ...
x