我相信很多同學都對接過各種各樣的第三方平臺的登入授權獲取使用者資訊(如:微信登入、支付寶登入、GitHub登入等等)。今天給大家推薦一個.NET開源最全的第三方登入整合庫:CollectiveOAuth。
.Net平臺(C#) 史上最全的整合第三方登入的開源庫 => 環境支援 .NET Framework 4.5 ~ 4.6.2 和 .NetCore 3.1。目前已包含Github、Gitee、釘釘、百度、支付寶、微信、企業微信、騰訊雲開發者平臺(Coding)、OSChina、微博、QQ、Google、Facebook、抖音、領英、小米、微軟、今日頭條、Teambition、StackOverflow、Pinterest、人人、華為、酷家樂、Gitlab、美團、餓了麼、等第三方平臺的授權登入。
企業微信掃碼授權登入官方檔案地址:https://developer.work.weixin.qq.com/document/path/91025,在進行企業微信掃碼授權繫結/登入之前需要先自建應用,同時需要開啟網頁授權登入,具體自建應用的相關操作可以參考博文:https://developer.aliyun.com/article/1136114
完成了上面企業微信管理後臺的相關設定之後,我們就可以按照檔案步驟開始操作了
關於構造企業微信掃碼繫結/登入二維條碼一共有兩種方式:構造獨立視窗登入二維條碼、構造內嵌登入二維條碼,下面簡單說一下構造獨立視窗登入二維條碼
構造獨立視窗登入二維條碼,可以在頁面放置一個 button 按鈕,新增點選事件,在觸發點選事件時存取連線https://open.work.weixin.qq.com/wwopen/sso/qrConnect?appid=CORPID&agentid=AGENTID&redirect_uri=REDIRECT_URI&state=STATE,效果如圖:
下載原始碼,新增Come.CollectiveOAuth類庫。
{
"AppSettings": {
//企業微信掃碼授權
"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": "*"
}
/// <summary>
/// 構建授權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中獲取預設設定(可以改造成從資料庫中讀取)
public Dictionary<string, ClientConfig> _clientConfigs;
public Dictionary<string, ClientConfig> ClientConfigs
{
get
{
if (_clientConfigs == null)
{
var _defaultPrefix = "CollectiveOAuth_";
_clientConfigs = new Dictionary<string, ClientConfig>();
#region 或者預設授權列表資料
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>
/// 獲取預設的 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;
}
}
}
更多實用功能特性歡迎前往開源地址檢視