我相信做开发的同学应该都对接过各种各样的第三方平台的登录授权,来获取用户信息(如:微信登录、支付宝登录、QQ登录、GitHub登录等等)。今天给大家推荐一个.NET开源好用的、全面的、方便第三方登录组件集合框架:MrHuo.OAuth。
MrHuo.OAuth是.NET项目集成OAuth2登录最全面的、最方便的框架,集成了国内外大部分平台(.NET Core 项目或 .NET Framework 4.6 项目均可使用)。
百度
微信公众号
Gitlab
Gitee
Github
华为
Coding.net
新浪微博
支付宝
OSChina
迅雷
钉钉内登录
钉钉扫码登录
微软
小米
StackOverflow
这里只展示部分示例代码,详细代码请前往源码地址查看:GitHub - mrhuo/MrHuo.OAuth: .netcore 下最好用的第三方登录组件集合,集成了国内外大部分平台,欢迎使用。
先熟悉流程,对接起来事半功倍:https://opendocs.alipay.com/open/284/106001
///
/// 支付宝回调URL:
/// https://oauthlogin.net/oauth/alipaycallback?app_id=2021002122645005&source=alipay_wallet&userOutputs=auth_user&scope=auth_user&alipay_token=&auth_code=2c58e763fdca4fb6b1f5a5bf4d26WA05
/// https://github.com/alipay/alipay-easysdk/tree/master/csharp
///
public class AlipayOAuth : OAuthLoginBase
{
private readonly AlipayApiRequest alipayApiRequest;
public AlipayOAuth(OAuthConfig oauthConfig, string privateRSAKey, string publicRSAKey, string encryptKey) : base(oauthConfig)
{
alipayApiRequest = new AlipayApiRequest()
{
PrivateRSAKey = privateRSAKey,
PublicRSAKey = publicRSAKey,
AppId = oauthConfig.AppId
};
}
protected override string AuthorizeUrl => "https://openauth.alipay.com/oauth2/publicAppAuthorize.htm";
protected override string AccessTokenUrl => throw new NotImplementedException();
protected override string UserInfoUrl => throw new NotImplementedException();
protected override Dictionary BuildAuthorizeParams(string state)
{
return new Dictionary()
{
["response_type"] = "code",
["app_id"] = $"{oauthConfig.AppId}",
["redirect_uri"] = $"{oauthConfig.RedirectUri}",
["scope"] = $"{oauthConfig.Scope}",
["state"] = $"{state}"
};
}
protected override Dictionary BuildGetAccessTokenParams(Dictionary authorizeCallbackParams)
{
return new Dictionary()
{
["grant_type"] = "authorization_code",
["code"] = authorizeCallbackParams["code"]
};
}
protected override Dictionary BuildGetUserInfoParams(AlipayAccessTokenModel accessTokenModel)
{
return new Dictionary()
{
["auth_token"] = accessTokenModel.AccessToken
};
}
public override async Task GetAccessTokenAsync(Dictionary authorizeCallbackParams)
{
var getAccessTokenResponse = await alipayApiRequest.PostAsync(
"alipay.system.oauth.token",
BuildGetAccessTokenParams(authorizeCallbackParams)
);
if (getAccessTokenResponse.AccessTokenResponse.SubMsg != null)
{
throw new Exception(getAccessTokenResponse.AccessTokenResponse.SubMsg);
}
return getAccessTokenResponse.AccessTokenResponse;
}
public override async Task GetUserInfoAsync(AlipayAccessTokenModel accessTokenModel)
{
var getUserInfoResponse = await alipayApiRequest.PostAsync(
"alipay.user.info.share",
BuildGetUserInfoParams(accessTokenModel)
);
if (getUserInfoResponse.AlipayUserInfoModel.SubMsg != null)
{
throw new Exception(getUserInfoResponse.AlipayUserInfoModel.SubMsg);
}
return getUserInfoResponse.AlipayUserInfoModel;
}
}
更多项目实用功能和特性欢迎前往项目开源地址查看,别忘了给项目一个Star支持。
GitHub - mrhuo/MrHuo.OAuth: .netcore 下最好用的第三方登录组件集合,集成了国内外大部分平台,欢迎使用。
该项目已收录到C#/.NET/.NET Core优秀项目和框架精选中,关注优秀项目和框架精选能让你及时了解C#、.NET和.NET Core领域的最新动态和最佳实践,提高开发工作效率和质量。坑已挖,欢迎大家踊跃提交PR推荐或自荐(让优秀的项目和框架不被埋没
)。
https://github.com/YSGStudyHards/DotNetGuide/blob/main/docs/DotNet/DotNetProjectPicks.md
文章转载自:追逐时光者
原文链接:https://www.cnblogs.com/Can-daydayup/p/17832805.html