Netnr.Login
第三方OAuth授权登录
支持第三方登录
更新日志
安装 (NuGet)
Install-Package Netnr.Login
修改配置信息(密钥、回调等)
提醒:一般所有第三方登录都有一个 state 参数,用于防止CSRF攻击(防伪),可以利用该参数添加 登录、注册 的标注前缀
框架
- .NETStandard 2.0
- .NETFramework 4.0
使用
using System;
namespace Netnr.Login.Sample
{
class Program
{
static void Main(string[] args)
{
var lc = new LoginClient(LoginBase.LoginType.Gitee);
var url = lc.Auth();
var ar = new LoginBase.AuthorizeResult();
ar.code = "";
lc.AuthCallback(ar);
}
public class LoginClient
{
private LoginBase.LoginType? loginType;
public LoginClient(LoginBase.LoginType _loginType)
{
loginType = _loginType;
// 配置
QQConfig.APPID = "XXX";
QQConfig.APPKey = "XXX";
//回调地址,与申请填写的地址保持一致
QQConfig.Redirect_Uri = "https://rf2.netnr.com/account/authcallback/qq";
WeChatConfig.AppId = "";
WeChatConfig.AppSecret = "";
WeChatConfig.Redirect_Uri = "";
WeiboConfig.AppKey = "";
WeiboConfig.AppSecret = "";
WeiboConfig.Redirect_Uri = "";
GitHubConfig.ClientID = "";
GitHubConfig.ClientSecret = "";
GitHubConfig.Redirect_Uri = "";
//申请的应用名称,非常重要
GitHubConfig.ApplicationName = "netnrf";
TaoBaoConfig.AppKey = "";
TaoBaoConfig.AppSecret = "";
TaoBaoConfig.Redirect_Uri = "";
MicroSoftConfig.ClientID = "";
MicroSoftConfig.ClientSecret = "";
MicroSoftConfig.Redirect_Uri = "";
DingTalkConfig.appId = "";
DingTalkConfig.appSecret = "";
DingTalkConfig.Redirect_Uri = "";
GiteeConfig.ClientID = "";
GiteeConfig.ClientSecret = "";
GiteeConfig.Redirect_Uri = "";
}
/// <summary>
/// 生成请求链接
/// </summary>
/// <param name="authType">在防伪参数追加信息(可用于登录、注册、绑定、解绑区分)</param>
/// <returns></returns>
public string Auth(string authType = "")
{
var url = string.Empty;
switch (loginType)
{
case LoginBase.LoginType.QQ:
{
var reqe = new QQ_Authorization_RequestEntity();
if (!string.IsNullOrWhiteSpace(authType))
{
reqe.state = authType + reqe.state;
}
url = QQ.AuthorizationHref(reqe);
}
break;
case LoginBase.LoginType.WeiBo:
{
var reqe = new Weibo_Authorize_RequestEntity();
if (!string.IsNullOrWhiteSpace(authType))
{
reqe.state = authType + reqe.state;
}
url = Weibo.AuthorizeHref(reqe);
}
break;
case LoginBase.LoginType.GitHub:
{
var reqe = new GitHub_Authorize_RequestEntity();
if (!string.IsNullOrWhiteSpace(authType))
{
reqe.state = authType + reqe.state;
}
url = GitHub.AuthorizeHref(reqe);
}
break;
case LoginBase.LoginType.TaoBao:
{
var reqe = new TaoBao_Authorize_RequestEntity();
if (!string.IsNullOrWhiteSpace(authType))
{
reqe.state = authType + reqe.state;
}
url = TaoBao.AuthorizeHref(reqe);
}
break;
case LoginBase.LoginType.MicroSoft:
{
var reqe = new MicroSoft_Authorize_RequestEntity();
if (!string.IsNullOrWhiteSpace(authType))
{
reqe.state = authType + reqe.state;
}
url = MicroSoft.AuthorizeHref(reqe);
}
break;
case LoginBase.LoginType.WeChat:
{
var reqe = new WeChat_Authorization_RequestEntity();
if (!string.IsNullOrWhiteSpace(authType))
{
reqe.state = authType + reqe.state;
}
url = WeChat.AuthorizationHref(reqe);
}
break;
case LoginBase.LoginType.DingTalk:
{
var reqe = new DingTalk_Authorize_RequestEntity();
if (!string.IsNullOrWhiteSpace(authType))
{
reqe.state = authType + reqe.state;
}
//扫描模式
url = DingTalk.AuthorizeHref_ScanCode(reqe);
//密码模式
//url = DingTalk.AuthorizeHref_Password(reqe);
}
break;
case LoginBase.LoginType.Gitee:
{
var reqe = new Gitee_Authorize_RequestEntity();
if (!string.IsNullOrWhiteSpace(authType))
{
reqe.state = authType + reqe.state;
}
url = Gitee.AuthorizeHref(reqe);
}
break;
}
return url;
}
/// <summary>
/// 回调方法
/// </summary>
/// <param name="authorizeResult">接收授权码、防伪参数</param>
public void AuthCallback(LoginBase.AuthorizeResult authorizeResult)
{
if (string.IsNullOrWhiteSpace(authorizeResult.code))
{
//打开链接没登录授权
}
else
{
//唯一标示
string OpenId = string.Empty;
switch (loginType)
{
case LoginBase.LoginType.QQ:
{
//获取 access_token
var tokenEntity = QQ.AccessToken(new QQ_AccessToken_RequestEntity()
{
code = authorizeResult.code
});
//获取 OpendId
var openidEntity = QQ.OpenId(new QQ_OpenId_RequestEntity()
{
access_token = tokenEntity.access_token
});
//获取 UserInfo
_ = QQ.OpenId_Get_User_Info(new QQ_OpenAPI_RequestEntity()
{
access_token = tokenEntity.access_token,
openid = openidEntity.openid
});
//身份唯一标识
OpenId = openidEntity.openid;
}
break;
case LoginBase.LoginType.WeiBo:
{
//获取 access_token
var tokenEntity = Weibo.AccessToken(new Weibo_AccessToken_RequestEntity()
{
code = authorizeResult.code
});
//获取 access_token 的授权信息
var tokenInfoEntity = Weibo.GetTokenInfo(new Weibo_GetTokenInfo_RequestEntity()
{
access_token = tokenEntity.access_token
});
//获取 users/show
_ = Weibo.UserShow(new Weibo_UserShow_RequestEntity()
{
access_token = tokenEntity.access_token,
uid = Convert.ToInt64(tokenInfoEntity.uid)
});
OpenId = tokenEntity.access_token;
}
break;
case LoginBase.LoginType.WeChat:
{
//获取 access_token
var tokenEntity = WeChat.AccessToken(new WeChat_AccessToken_RequestEntity()
{
code = authorizeResult.code
});
//获取 user
_ = WeChat.Get_User_Info(new WeChat_OpenAPI_RequestEntity()
{
access_token = tokenEntity.access_token,
openid = tokenEntity.openid
});
//身份唯一标识
OpenId = tokenEntity.openid;
}
break;
case LoginBase.LoginType.GitHub:
{
//获取 access_token
var tokenEntity = GitHub.AccessToken(new GitHub_AccessToken_RequestEntity()
{
code = authorizeResult.code
});
//获取 user
var userEntity = GitHub.User(new GitHub_User_RequestEntity()
{
access_token = tokenEntity.access_token
});
OpenId = userEntity.id.ToString();
}
break;
case LoginBase.LoginType.TaoBao:
{
//获取 access_token
var tokenEntity = TaoBao.AccessToken(new TaoBao_AccessToken_RequestEntity()
{
code = authorizeResult.code
});
OpenId = tokenEntity.open_uid;
}
break;
case LoginBase.LoginType.MicroSoft:
{
//获取 access_token
var tokenEntity = MicroSoft.AccessToken(new MicroSoft_AccessToken_RequestEntity()
{
code = authorizeResult.code
});
//获取 user
var userEntity = MicroSoft.User(new MicroSoft_User_RequestEntity()
{
access_token = tokenEntity.access_token
});
OpenId = userEntity.id.ToString();
}
break;
case LoginBase.LoginType.DingTalk:
{
//获取 user
var userEntity = DingTalk.User(new DingTalk_User_RequestEntity(), authorizeResult.code);
OpenId = userEntity?.openid;
}
break;
case LoginBase.LoginType.Gitee:
{
//获取 access_token
var tokenEntity = Gitee.AccessToken(new Gitee_AccessToken_RequestEntity()
{
code = authorizeResult.code
});
//获取 user
var userEntity = Gitee.User(new Gitee_User_RequestEntity()
{
access_token = tokenEntity.access_token
});
OpenId = userEntity.id.ToString();
}
break;
}
//拿到登录标识
if (string.IsNullOrWhiteSpace(OpenId))
{
//TO DO
}
}
}
}
}
}