MrHuo.OAuth
. NETプロジェクト統合OAuth 2ログインのための最も包括的で便利なフレームワーク

NET Coreプロジェクトまたは. NET Framework 4.6プロジェクトの両方が利用可能です。
プラットフォームのサポート
- 100ドル(利用可能)
- マイクロパブリックアカウント(利用可能)
- GitLab(利用可能)
- Gitee(利用可能)
- GitHub(利用可能)
- Huawei(利用可能)
- Coding.net利用可能
- 新浪微博(利用可能)
- 有料(利用可能)
- OSChina(利用可能)
- Thread(利用可能)
- ピン内ログイン(利用可能)
- 登録商標(利用可能)
- QQ(利用可能)
- Microsoft(利用可能)
- Xiaomi(利用可能)
- StackOverflow使用可能
- Facebook(可用) by Donma Hsu
- Google(利用可能)
- LinkedIn(利用可能、nugetは公開予定)
- 抖音(可用,待发布 nuget) by feng lui
- 快手(可用,待发布 nuget) by feng lui
- オープンプラットフォーム(テスト中)
- [...テスト中]
プラン·プラン
- フライングブック。
- バオバオ
- スイカのこと
- 今日の見出し。
- みんなのネットワーク
- Teambition
- 企業マイクロ2コード登録
- 企業マイクロサイト登録
- クール·ファミリー
- 空腹ですか?
- 京都市。
- アリ·ユン
- ヒマラヤ..。
使用方法の使用
新建项目 web 项目,安装
nuget包即可使用。
现可用的 nuget 包列表如下:
https://www.nuget.org/packages?q=MrHuo.OAuth
Gitlab
Install-Package MrHuo.OAuth.Gitlab -Version 1.1.1
マイクロパブリック番号
Install-Package MrHuo.OAuth.Wechat -Version 1.1.1
oschina.net
Install-Package MrHuo.OAuth.OSChina -Version 1.1.1
coding.net
Install-Package MrHuo.OAuth.Coding -Version 1.1.1
github.com
Install-Package MrHuo.OAuth.GitHub -Version 1.1.1
alipay.com
Install-Package MrHuo.OAuth.Alipay -Version 1.1.1
baidu.com
Install-Package MrHuo.OAuth.Baidu -Version 1.1.1
huawei.com
Install-Package MrHuo.OAuth.Huawei -Version 1.1.1
gitee.com
Install-Package MrHuo.OAuth.Gitee -Version 1.1.1
weibo.com
Install-Package MrHuo.OAuth.SinaWeibo -Version 1.1.1
xunlei.com
Install-Package MrHuo.OAuth.XunLei -Version 1.1.1
qq.com
Install-Package MrHuo.OAuth.QQ -Version 1.1.1
microsoft.com
Install-Package MrHuo.OAuth.Microsoft -Version 1.1.1
mi.com
Install-Package MrHuo.OAuth.Mi -Version 1.1.1
stackoverflow.com
Install-Package MrHuo.OAuth.StackOverflow -Version 1.1.1
facebook.com
Install-Package MrHuo.OAuth.Facebook -Version 1.1.1
google.com
Install-Package MrHuo.OAuth.Google -Version 1.0.0
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
//将第三方登录组件注入进去
services.AddSingleton(new Baidu.BaiduOAuth(OAuthConfig.LoadFrom(Configuration, "oauth:baidu")));
services.AddSingleton(new Wechat.WechatOAuth(OAuthConfig.LoadFrom(Configuration, "oauth:wechat")));
services.AddSingleton(new Gitlab.GitlabOAuth(OAuthConfig.LoadFrom(Configuration, "oauth:gitlab")));
services.AddSingleton(new Gitee.GiteeOAuth(OAuthConfig.LoadFrom(Configuration, "oauth:gitee")));
//... 其他登录方式
}
注意:如果用 appsettings.json 方式引入,提供了一个快捷方法从配置中加载。
OAuthConfig.LoadFrom(Configuration, "oauth:baidu")
"oauth:baidu" 这部分是配置前缀,配置格式如下:
{
"oauth": {
"qq": {
"app_id": "qq_app_id",
"app_key": "qq_app_key",
"redirect_uri": "https://oauthlogin.net/oauth/qqcallback",
"scope": "get_user_info"
},
"github": {
"app_id": "github_app_id",
"app_key": "github_app_key",
"redirect_uri": "https://oauthlogin.net/oauth/githubcallback",
"scope": "repo"
},
"wechat": {
"app_id": "wechat_app_id",
"app_key": "wechat_app_key",
"redirect_uri": "https://oauthlogin.net/oauth/wechatcallback",
"scope": "snsapi_userinfo"
},
"huawei": {
"app_id": "huawei_app_id",
"app_key": "huawei_app_key",
"redirect_uri": "https://oauthlogin.net/oauth/huaweicallback",
"scope": "https://www.huawei.com/auth/account"
},
"gitee": {
"app_id": "gitee_app_id",
"app_key": "gitee_app_key",
"redirect_uri": "http://oauthlogin.net/oauth/giteecallback",
"scope": "user_info"
},
"baidu": {
"app_id": "baidu_app_id",
"app_key": "baidu_app_key",
"redirect_uri": "http://oauthlogin.net/oauth/baiducallback",
"scope": "basic"
},
"alipay": {
"app_id": "alipay_app_id",
"app_key": "alipay_app_key",
"redirect_uri": "https://oauthlogin.net/oauth/alipaycallback",
"scope": "auth_user",
"private_key": "private_key",
"public_key": "public_key"
},
"gitlab": {
"app_id": "gitlab_app_id",
"app_key": "gitlab_app_key",
"redirect_uri": "http://oauthlogin.net/oauth/gitlabcallback",
"scope": "read_user"
}
}
}
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;
}
case "wechat":
{
redirectUrl = wechatOAuth.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);
}
}
}
Views
<!--在代码中放置授权按钮-->
<a href="/oauth/baidu">Baidu 登录</a>
<a href="/oauth/wechat">Wechat 扫码登录</a>
<!-- //其他登录方式照样子往下写 -->
拡張子
扩展其他平台非常容易,拿 Gitee 平台的代码来说:https://github.com/mrhuo/MrHuo.OAuth/tree/main/MrHuo.OAuth.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; }
//...其他属性类似如上
}
ステップ2:対応するプラットフォームの認証インターフェイスを書く
/// <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";
}
コメントを追加すると、ご覧の通り、合計10行で、非常に便利です。プラットフォームプロトコルがOAuth 2標準に準拠して開発されている場合は、数行で十分です。
フィールドを変更するWeChatログイン実装でも、それは複雑ですが、基本的なパラメータを定義するだけで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",
};
}
}
特別貢献度
友人が様々なプラットフォームのログインコンポーネントをスムーズに使用できるようにするために、まず様々なプラットフォームのAPPIDテストを求めて、このリソースを持っている友人から連絡してください。 テストを提供する友人は、プロジェクトのフロントページにリンク付きの特別なコントリビュートリストに永久に表示されます。
メールアドレス:admin @ mrhuo.com タグ:OAuth AppID
Contribution
1.他の未完のプラットフォームコードへの貢献も歓迎です。2.プラットフォームへのリンクを添えて、課題にプラットフォームを提出してください。プログラムに参加します。3.文明交流の提案を歓迎します。
License
プロジェクトの住所
- 框架名:
MrHuo.OAuth - アーティスト:Mrhuo
- GitHub:https://github.com/mrhuo/MrHuo.OAuth
