前言
我相信很多同學(xué)都對接過各種各樣的第三方平臺(tái)的登錄授權(quán)獲取用戶信息(如:微信登錄、支付寶登錄、GitHub登錄等等)。今天給大家推薦一個(gè).NET開源最全的第三方登錄整合庫:CollectiveOAuth。
官方項(xiàng)目介紹
.Net平臺(tái)(C#) 史上最全的整合第三方登錄的開源庫 => 環(huán)境支持 .NET Framework 4.5 ~ 4.6.2 和 .NetCore 3.1。目前已包含Github、Gitee、釘釘、百度、支付寶、微信、企業(yè)微信、騰訊云開發(fā)者平臺(tái)(Coding)、OSChina、微博、QQ、Google、Facebook、抖音、領(lǐng)英、小米、微軟、今日頭條、Teambition、StackOverflow、Pinterest、人人、華為、酷家樂、Gitlab、美團(tuán)、餓了么、等第三方平臺(tái)的授權(quán)登錄。
項(xiàng)目特點(diǎn)
- 全:已集成二十多家第三方平臺(tái)(國內(nèi)外常用的基本都已包含),仍然還在持續(xù)擴(kuò)展中!
- 簡:API就是奔著最簡單去設(shè)計(jì)的(見后面快速開始),盡量讓您用起來沒有障礙感!
企業(yè)微信掃碼授權(quán)快速開始
0、企業(yè)微信開發(fā)對接文檔
文檔介紹
企業(yè)微信掃碼授權(quán)登錄官方文檔地址:https://developer.work.weixin.qq.com/document/path/91025,在進(jìn)行企業(yè)微信掃碼授權(quán)綁定/登錄之前需要先自建應(yīng)用,同時(shí)需要開啟網(wǎng)頁授權(quán)登錄,具體自建應(yīng)用的相關(guān)操作可以參考博文:https://developer.aliyun.com/article/1136114
管理平臺(tái)接入
完成了上面企業(yè)微信管理后臺(tái)的相關(guān)配置之后,我們就可以按照文檔步驟開始操作了
構(gòu)造二維碼
關(guān)于構(gòu)造企業(yè)微信掃碼綁定/登錄二維碼一共有兩種方式:構(gòu)造獨(dú)立窗口登錄二維碼、構(gòu)造內(nèi)嵌登錄二維碼,下面簡單說一下構(gòu)造獨(dú)立窗口登錄二維碼
構(gòu)造獨(dú)立窗口登錄二維碼
構(gòu)造獨(dú)立窗口登錄二維碼,可以在頁面放置一個(gè) button 按鈕,添加點(diǎn)擊事件,在觸發(fā)點(diǎn)擊事件時(shí)訪問連接https://open.work.weixin.qq.com/wwopen/sso/qrConnect?appid=CORPID&agentid=AGENTID&redirect_uri=REDIRECT_URI&state=STATE,效果如圖:
連接參數(shù)說明:
1、引入依賴
下載源碼,添加Come.CollectiveOAuth類庫。
2、配置企業(yè)微信掃碼授權(quán)(appsettings.json)中配置
{
"AppSettings": {
//企業(yè)微信掃碼授權(quán)
"CollectiveOAuth_WECHAT_ENTERPRISE_SCAN_ClientId": "xxxxxxxxxxxxxxxxx",
"CollectiveOAuth_WECHAT_ENTERPRISE_SCAN_AgentId": "xxxxxx",
"CollectiveOAuth_WECHAT_ENTERPRISE_SCAN_ClientSecret": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"CollectiveOAuth_WECHAT_ENTERPRISE_SCAN_RedirectUri": "https://yours.domain.com/oauth2/callback?authSource=WECHAT_ENTERPRISE_SCAN"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
構(gòu)建授權(quán)Url方法
/// <summary>
/// 構(gòu)建授權(quán)Url方法
/// </summary>
/// <param name="authSource"></param>
/// <returns>RedirectUrl</returns>
public IActionResult Authorization(string authSource= "WECHAT_ENTERPRISE_SCAN")
{
AuthRequestFactory authRequest = new AuthRequestFactory();
var request = authRequest.getRequest(authSource);
var authorize = request.authorize(AuthStateUtils.createState());
Console.WriteLine(authorize);
return Redirect(authorize);
}
public class AuthRequestFactory
{
#region 從Webconfig中獲取默認(rèn)配置(可以改造成從數(shù)據(jù)庫中讀取)
public Dictionary<string, ClientConfig> _clientConfigs;
public Dictionary<string, ClientConfig> ClientConfigs
{
get
{
if (_clientConfigs == null)
{
var _defaultPrefix = "CollectiveOAuth_";
_clientConfigs = new Dictionary<string, ClientConfig>();
#region 或者默認(rèn)授權(quán)列表數(shù)據(jù)
var defaultAuthList = typeof(DefaultAuthSourceEnum).ToList().select(a => a.Name.ToUpper()).ToList();
foreach (var authSource in defaultAuthList)
{
var clientConfig = new ClientConfig();
clientConfig.clientId = AppSettingUtils.GetStrValue($"{_defaultPrefix}{authSource}_ClientId");
clientConfig.clientSecret = AppSettingUtils.GetStrValue($"{_defaultPrefix}{authSource}_ClientSecret");
clientConfig.redirectUri = AppSettingUtils.GetStrValue($"{_defaultPrefix}{authSource}_RedirectUri");
clientConfig.alipayPublicKey = AppSettingUtils.GetStrValue($"{_defaultPrefix}{authSource}_AlipayPublicKey");
clientConfig.unionId = AppSettingUtils.GetStrValue($"{_defaultPrefix}{authSource}_unionId");
clientConfig.stackOverflowKey = AppSettingUtils.GetStrValue($"{_defaultPrefix}{authSource}_StackOverflowKey");
clientConfig.agentId = AppSettingUtils.GetStrValue($"{_defaultPrefix}{authSource}_AgentId");
clientConfig.scope = AppSettingUtils.GetStrValue($"{_defaultPrefix}{authSource}_Scope");
_clientConfigs.Add(authSource, clientConfig);
}
#endregion
}
return _clientConfigs;
}
}
public ClientConfig GetClientConfig(string authSource)
{
if (authSource.IsNullOrWhiteSpace())
{
return null;
}
if (!ClientConfigs.ContainsKey(authSource))
{
return null;
}
else
{
return ClientConfigs[authSource];
}
}
#endregion
/**
* 返回AuthRequest對象
*
* @return {@link AuthRequest}
*/
public IAuthRequest getRequest(string authSource)
{
// 獲取 CollectiveOAuth 中已存在的
IAuthRequest authRequest = getDefaultRequest(authSource);
return authRequest;
}
/// <summary>
/// 獲取默認(rèn)的 Request
/// </summary>
/// <param name="authSource"></param>
/// <returns>{@link AuthRequest}</returns>
private IAuthRequest getDefaultRequest(string authSource)
{
ClientConfig clientConfig = GetClientConfig(authSource);
IAuthStateCache authStateCache = new DefaultAuthStateCache();
DefaultAuthSourceEnum authSourceEnum = GlobalAuthUtil.enumfromString<DefaultAuthSourceEnum>(authSource);
switch (authSourceEnum)
{
case DefaultAuthSourceEnum.WECHAT_MP:
return new WeChatMpAuthRequest(clientConfig, authStateCache);
case DefaultAuthSourceEnum.WECHAT_OPEN:
return new WeChatOpenAuthRequest(clientConfig, authStateCache);
case DefaultAuthSourceEnum.WECHAT_ENTERPRISE:
return new WeChatEnterpriseAuthRequest(clientConfig, authStateCache);
case DefaultAuthSourceEnum.WECHAT_ENTERPRISE_SCAN:
return new WeChatEnterpriseScanAuthRequest(clientConfig, authStateCache);
case DefaultAuthSourceEnum.ALIPAY_MP:
return new AlipayMpAuthRequest(clientConfig, authStateCache);
case DefaultAuthSourceEnum.GITEE:
return new GiteeAuthRequest(clientConfig, authStateCache);
case DefaultAuthSourceEnum.GITHUB:
return new GithubAuthRequest(clientConfig, authStateCache);
case DefaultAuthSourceEnum.BAIDU:
return new BaiduAuthRequest(clientConfig, authStateCache);
case DefaultAuthSourceEnum.XIAOMI:
return new XiaoMiAuthRequest(clientConfig, authStateCache);
case DefaultAuthSourceEnum.DINGTALK_SCAN:
return new DingTalkScanAuthRequest(clientConfig, authStateCache);
case DefaultAuthSourceEnum.OSCHINA:
return new OschinaAuthRequest(clientConfig, authStateCache);
case DefaultAuthSourceEnum.CODING:
return new CodingAuthRequest(clientConfig, authStateCache);
case DefaultAuthSourceEnum.LINKEDIN:
return new LinkedInAuthRequest(clientConfig, authStateCache);
case DefaultAuthSourceEnum.WEIBO:
return new WeiboAuthRequest(clientConfig, authStateCache);
case DefaultAuthSourceEnum.QQ:
return new QQAuthRequest(clientConfig, authStateCache);
case DefaultAuthSourceEnum.DOUYIN:
return new DouyinAuthRequest(clientConfig, authStateCache);
case DefaultAuthSourceEnum.GOOGLE:
return new GoogleAuthRequest(clientConfig, authStateCache);
case DefaultAuthSourceEnum.FACEBOOK:
return new FackbookAuthRequest(clientConfig, authStateCache);
case DefaultAuthSourceEnum.MICROSOFT:
return new MicrosoftAuthRequest(clientConfig, authStateCache);
case DefaultAuthSourceEnum.TOUTIAO:
return new ToutiaoAuthRequest(clientConfig, authStateCache);
case DefaultAuthSourceEnum.TEAMBITION:
return new TeambitionAuthRequest(clientConfig, authStateCache);
case DefaultAuthSourceEnum.RENREN:
return new RenrenAuthRequest(clientConfig, authStateCache);
case DefaultAuthSourceEnum.PINTEREST:
return new PinterestAuthRequest(clientConfig, authStateCache);
case DefaultAuthSourceEnum.STACK_OVERFLOW:
return new StackOverflowAuthRequest(clientConfig, authStateCache);
case DefaultAuthSourceEnum.HUAWEI:
return new HuaweiAuthRequest(clientConfig, authStateCache);
case DefaultAuthSourceEnum.KUJIALE:
return new KujialeAuthRequest(clientConfig, authStateCache);
case DefaultAuthSourceEnum.GITLAB:
return new GitlabAuthRequest(clientConfig, authStateCache);
case DefaultAuthSourceEnum.MEITUAN:
return new MeituanAuthRequest(clientConfig, authStateCache);
case DefaultAuthSourceEnum.ELEME:
return new ElemeAuthRequest(clientConfig, authStateCache);
default:
return null;
}
}
}
API列表
https://gitee.com/rthinking/CollectiveOAuth#api%E5%88%97%E8%A1%A8