RuoVea.ExOAuth
6.0.1
dotnet add package RuoVea.ExOAuth --version 6.0.1
NuGet\Install-Package RuoVea.ExOAuth -Version 6.0.1
<PackageReference Include="RuoVea.ExOAuth" Version="6.0.1" />
paket add RuoVea.ExOAuth --version 6.0.1
#r "nuget: RuoVea.ExOAuth, 6.0.1"
// Install RuoVea.ExOAuth as a Cake Addin #addin nuget:?package=RuoVea.ExOAuth&version=6.0.1 // Install RuoVea.ExOAuth as a Cake Tool #tool nuget:?package=RuoVea.ExOAuth&version=6.0.1
<h1 align="center">RuoVea.ExOAuth</h1> <p align="center">是 .Net Core 项目集成 第三方OAuth2 登录全面的、方便的框架</p>
<div align="center">
</div>
.net core 5.0 以上项目可用
<hr /> <img src="doc/QQ截图20220721163721.png" height="300" title="截图">
已支持平台
- [ ✔ ] Gitee
- [ ✔ ] Github
- [ ✔ ] 百度
- [ ✔ ] Coding.net
- [ ✔ ] 支付宝
- [ ✔ ] 飞书
- [ ✔ ] 钉钉内登录
- [ ✔ ] 抖音
- [ ✔ ] 钉钉扫码登录
- [ ✔ ] 微软
- [ ✔ ] 微信公众号
- [ ✔ ] Gitlab
- [ ✔ ] 华为
- [ ✔ ] 新浪微博
- [ ✔ ] OSChina
- [ ✔ ] 迅雷
- [ ✔ ] 小米
- [ ✔ ] StackOverflow
- [ ✔ ] 美团
计划
- ☐ 快手
- ☐ 微信开放平台
- ☐ 淘宝
- ☐ 西瓜
- ☐ 今日头条
- ☐ 人人网
- ☐ Teambition
- ☐ 企业微信二维码登录
- ☐ 企业微信网页登录
- ☐ 酷家乐
- ☐ 饿了么
- ☐ 京东
- ☐ 阿里云
- ☐ 喜马拉雅...
使用方法
新建项目web项目,安装
nuget
包即可使用。
现可用的 nuget
包列表如下:
https://www.nuget.org/packages?q=RuoVea
1.Startup.cs
public void ConfigureServices(IServiceCollection services)
{
//将第三方登录组件注入进去
services.AddSingleton(new Baidu.BaiduOAuth(OAuthConfig.LoadFrom(Configuration, "oauth:gitee")));
services.AddSingleton(new Gitee.GiteeOAuth(OAuthConfig.LoadFrom(Configuration, "oauth:github")));
//... 其他登录方式
}
注意:如果用 appsettings.json
方式引入,提供了一个快捷方法从配置中加载。
OAuthConfig.LoadFrom(Configuration, "oauth:baidu")
"oauth:baidu"
这部分是配置前缀,配置格式如下:
{
"oauth": {
"github": {
"app_id": "github_app_id",
"app_key": "github_app_key",
"redirect_uri": "https://ruovea.com/oauth/githubcallback",
"scope": "repo"
},
"gitee": {
"app_id": "gitee_app_id",
"app_key": "gitee_app_key",
"redirect_uri": "https://ruovea.com/oauth/giteecallback",
"scope": "user_info"
}
}
}
2.OAuthController.cs
根据实际需要自行命名
public class OAuthController : Controller
{
[HttpGet("oauth/{type}")]
public IActionResult Index(
string type,
[FromServices] BaiduOAuth baiduOAuth,
[FromServices] WechatOAuth wechatOAuth
)
{
var redirectUrl = "";
switch (type.ToLower())
{
case "baidu":
{
redirectUrl = baiduOAuth.GetAuthorizeUrl();
break;
}
default:
return ReturnToError($"没有实现【{type}】登录方式!");
}
return Redirect(redirectUrl);
}
[HttpGet("oauth/{type}callback")]
public async Task<IActionResult> LoginCallback(
string type,
[FromServices] BaiduOAuth baiduOAuth,
[FromServices] WechatOAuth wechatOAuth,
[FromQuery] string code,
[FromQuery] string state)
{
try
{
switch (type.ToLower())
{
case "baidu":
{
var authorizeResult = await baiduOAuth.AuthorizeCallback(code, state);
if (!authorizeResult.IsSccess)
{
throw new Exception(authorizeResult.ErrorMessage);
}
return Json(authorizeResult);
}
case "wechat":
{
var authorizeResult = await wechatOAuth.AuthorizeCallback(code, state);
if (!authorizeResult.IsSccess)
{
throw new Exception(authorizeResult.ErrorMessage);
}
return Json(authorizeResult);
}
default:
throw new Exception($"没有实现【{type}】登录回调!");
}
}
catch (Exception ex)
{
return Content(ex.Message);
}
}
}
3.Views
<a href="/oauth/baidu">Baidu 登录</a>
<a href="/oauth/wechat">Wechat 扫码登录</a>
扩展
扩展其他平台非常容易,拿 Gitee
平台的代码来说:
第一步:找平台对应 OAuth 文档,找到获取用户信息接口返回JSON,转换为 C# 实体类。如下:
根据自己需要和接口标准,扩展用户属性
public class GiteeUserModel : IUserInfoModel
{
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("avatar_url")]
public string Avatar { get; set; }
[JsonPropertyName("message")]
public string ErrorMessage { get; set; }
[JsonPropertyName("email")]
public string Email { get; set; }
[JsonPropertyName("blog")]
public string Blog { get; set; }
//...其他属性类似如上
}
第二步:写对应平台的授权接口
/// <summary>
/// https://gitee.com/api/v5/oauth_doc#/
/// </summary>
public class GiteeOAuth : OAuthLoginBase<GiteeUserModel>
{
public GiteeOAuth(OAuthConfig oauthConfig) : base(oauthConfig) { }
protected override string AuthorizeUrl => "https://gitee.com/oauth/authorize";
protected override string AccessTokenUrl => "https://gitee.com/oauth/token";
protected override string UserInfoUrl => "https://gitee.com/api/v5/user";
}
加上注释,总共十行,如你所见,非常方便。如果该平台协议遵循 OAuth2 标准开发,那么就这么几行就好了。
就连修改字段的微信登录实现,也不过复杂,只需要定义基本参数就OK。代码如下:
/// <summary>
/// Wechat OAuth 相关文档参考:
/// <para>https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html</para>
/// </summary>
public class WechatOAuth : OAuthLoginBase<WechatAccessTokenModel, WechatUserInfoModel>
{
public WechatOAuth(OAuthConfig oauthConfig) : base(oauthConfig) { }
protected override string AuthorizeUrl => "https://open.weixin.qq.com/connect/oauth2/authorize";
protected override string AccessTokenUrl => "https://api.weixin.qq.com/sns/oauth2/access_token";
protected override string UserInfoUrl => "https://api.weixin.qq.com/sns/userinfo";
protected override Dictionary<string, string> BuildAuthorizeParams(string state)
{
return new Dictionary<string, string>()
{
["response_type"] = "code",
["appid"] = oauthConfig.AppId,
["redirect_uri"] = System.Web.HttpUtility.UrlEncode(oauthConfig.RedirectUri),
["scope"] = oauthConfig.Scope,
["state"] = state
};
}
public override string GetAuthorizeUrl(string state = "")
{
return $"{base.GetAuthorizeUrl(state)}#wechat_redirect";
}
protected override Dictionary<string, string> BuildGetAccessTokenParams(Dictionary<string, string> authorizeCallbackParams)
{
return new Dictionary<string, string>()
{
["grant_type"] = "authorization_code",
["appid"] = $"{oauthConfig.AppId}",
["secret"] = $"{oauthConfig.AppKey}",
["code"] = $"{authorizeCallbackParams["code"]}"
};
}
protected override Dictionary<string, string> BuildGetUserInfoParams(WechatAccessTokenModel accessTokenModel)
{
return new Dictionary<string, string>()
{
["access_token"] = accessTokenModel.AccessToken,
["openid"] = accessTokenModel.OpenId,
["lang"] = "zh_CN",
};
}
}
<hr /> 为了能让各位朋友能顺利的使用各种平台登录组件,先急求各种平台的 APPID 做测试,有这方面资源的朋友联系我。 提供测试的朋友,可以永久出现在项目首页特别贡献列表里,可带链接。
ruovea@qq.com
备注:OAuth Appid
Contribution
1.欢迎参与开发,贡献其他未完成平台代码。 2.欢迎在 issue 里提交需求平台,带上平台链接地址,我们将加入到计划之中。 3.欢迎提交各种建议,文明交流。
支持第三方登录
<table> <tr><th>三方</th><th>参考文档</th><th>应用申请(已登录)</th></tr> <tr> <td><img src="https://s1.netnr.eu.org/static/login/qq.svg" height="30" title="QQ"></td> <td><a target="_blank" href="https://wiki.connect.qq.com/准备工作_oauth2-0">参考文档</a></td> <td><a target="_blank" href="https://connect.qq.com/manage.html">应用申请</a></td> </tr> <tr> <td><img src="https://s1.netnr.eu.org/static/login/wechat.svg" height="30" title="微信/WeChat"></td> <td><a target="_blank" href="https://developers.weixin.qq.com/doc/oplatform/Website_App/WeChat_Login/Wechat_Login.html">参考文档</a></td> <td><a target="_blank" href="https://open.weixin.qq.com">应用申请</a></td> </tr> <tr> <td><img src="https://s1.netnr.eu.org/static/login/weibo.svg" height="30" title="新浪微博"></td> <td><a target="_blank" href="https://open.weibo.com/wiki/授权机制说明">参考文档</a></td> <td><a target="_blank" href="https://open.weibo.com/apps">应用申请</a></td> </tr> <tr> <td><img src="https://s1.netnr.eu.org/static/login/github.svg" height="30" title="GitHub"></td> <td><a target="_blank" href="https://docs.github.com/en/developers/apps/building-oauth-apps/authorizing-oauth-apps">参考文档</a></td> <td><a target="_blank" href="https://github.com/settings/developers">应用申请</a></td> </tr> <tr> <td><img src="https://s1.netnr.eu.org/static/login/gitee.svg" height="30" title="码云/Gitee"></td> <td><a target="_blank" href="https://gitee.com/api/v5/oauth_doc">参考文档</a></td> <td><a target="_blank" href="https://gitee.com/oauth/applications">应用申请</a></td> </tr> <tr> <td><img src="https://s1.netnr.eu.org/static/login/taobao.svg" height="30" title="淘宝/天猫"></td> <td><a target="_blank" href="https://open.taobao.com/doc.htm?spm=a219a.7386797.0.0.4e00669acnkQy6&source=search&docId=105590&docType=1">参考文档</a></td> <td><a target="_blank" href="https://console.open.taobao.com/">应用申请</a></td> </tr> <tr> <td><img src="https://s1.netnr.eu.org/static/login/microsoft.svg" height="30" title="微软/Microsoft"></td> <td><a target="_blank" href="https://docs.microsoft.com/zh-cn/graph/auth/">参考文档</a></td> <td><a target="_blank" href="https://portal.azure.com/#blade/Microsoft_AAD_IAM/ActiveDirectoryMenuBlade/RegisteredApps">应用申请</a></td> </tr> <tr> <td><img src="https://s1.netnr.eu.org/static/login/dingtalk.svg" height="30" title="钉钉/DingTalk"></td> <td><a target="_blank" href="https://developers.dingtalk.com/document/app/scan-qr-code-to-login-isvapp">参考文档</a></td> <td><a target="_blank" href="https://open-dev.dingtalk.com/#/loginMan">应用申请</a></td> </tr> <tr> <td><img src="https://s1.netnr.eu.org/static/login/google.svg" height="30" title="谷歌/Google"></td> <td><a target="_blank" href="https://developers.google.com/identity/protocols/oauth2/openid-connect">参考文档</a></td> <td><a target="_blank" href="https://console.developers.google.com/apis/credentials">应用申请</a></td> </tr> <tr> <td><img src="https://s1.netnr.eu.org/static/login/alipay.svg" height="30" title="支付宝/AliPay"></td> <td><a target="_blank" href="https://opendocs.alipay.com/open/263/105809">参考文档</a></td> <td><a target="_blank" href="https://openhome.alipay.com/platform/developerIndex.htm">应用申请</a></td> </tr> <tr> <td><img src="https://s1.netnr.eu.org/static/login/stackoverflow.svg" height="30" title="Stack Overflow"></td> <td><a target="_blank" href="https://api.stackexchange.com">参考文档</a></td> <td><a target="_blank" href="https://stackapps.com/apps/oauth/register">应用申请</a></td> </tr> </table>
License
<a href="https://gitee.com/starry123/ruovea-oauth/blob/master/LICENSE" class="muted-link">Apache-2.0 License</a>
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net6.0
- Microsoft.Extensions.Configuration (>= 6.0.1)
NuGet packages (4)
Showing the top 4 NuGet packages that depend on RuoVea.ExOAuth:
Package | Downloads |
---|---|
RuoVea.ExOAuth.Baidu
第三方登录 OAuth Baidu 百度登录 |
|
RuoVea.ExOAuth.Github
第三方登录 OAuth Github |
|
RuoVea.ExOAuth.Microsoft
第三方登录 OAuth Microsoft |
|
RuoVea.ExOAuth.Coding
第三方登录 OAuth Coding |
GitHub repositories
This package is not used by any popular GitHub repositories.