1 Star 0 Fork 95

lxh023 / Netnr.Login

forked from 星尘 / Netnr.Login 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

Netnr.Login


集成三方登录

  • QQ
  • 微博
  • GitHub
  • 淘宝(天猫)
  • Microsoft

如果该项目对你有帮助,请你为项目Star,谢谢,这是对我精神上的支持,也是能一直坚持下去的动力。


安装 (NuGet)

Install-Package Netnr.Login

修改配置信息(密钥、回调等)


框架

.NETStandard 2.0

.NETFramework 4.0


Code

// 配置

QQConfig.APPID = "101511xxx";
QQConfig.APPKey = "f26b4af4a9d68bec2bbcbeee443fexxx";
//回调地址,与申请填写的地址保持一致
QQConfig.Redirect_Uri = "https://rf2.netnr.com/account/authcallback/qq";

WeiboConfig.AppKey = "";
WeiboConfig.AppSecret = "";
WeiboConfig.Redirect_Uri = "";

GitHubConfig.ClientID = "";
GitHubConfig.ClientSecret = "";
GitHubConfig.Redirect_Uri = "https://rf2.netnr.com/account/authcallback/github";
//申请的应用名称,非常重要
GitHubConfig.ApplicationName = "netnrf";

TaobaoConfig.AppKey = "";
TaobaoConfig.AppSecret = "";
TaobaoConfig.Redirect_Uri = "";

MicroSoftConfig.ClientID = "";
MicroSoftConfig.ClientSecret = "";
MicroSoftConfig.Redirect_Uri = "";

/// <summary>
/// 授权登录跳转
/// 访问地址:/{controller}/{action}/{id},如:/account/auth/qq
/// </summary>
/// <returns></returns>
public IActionResult Auth()
{
    string url = "/account/login";
    string vtype = RouteData.Values["id"]?.ToString().ToLower();
    switch (vtype)
    {
        case "qq":
            url = QQ.AuthorizationHref(new QQ_Authorization_RequestEntity());
            break;
        case "weibo":
            url = Weibo.AuthorizeHref(new Weibo_Authorize_RequestEntity());
            break;
        case "github":
            url = GitHub.AuthorizeHref(new GitHub_Authorize_RequestEntity());
            break;
        case "taobao":
            url = Taobao.AuthorizeHref(new Taobao_Authorize_RequestEntity());
            break;
        case "microsoft":
            url = MicroSoft.AuthorizeHref(new MicroSoft_Authorize_RequestEntity());
            break;
    }
    return Redirect(url);
}

/// <summary>
/// 授权登录回调
/// 访问地址:/{controller}/{action}/{id},如:/account/authcallback/qq
/// </summary>
/// <param name="code">自动接收授权登录后回调参数code</param>
/// <returns></returns>
public IActionResult AuthCallback(string code)
{
    //唯一标示
    string openId = string.Empty;
    //登录类型
    string vtype = RouteData.Values["id"]?.ToString().ToLower();
    try
    {
        switch (vtype)
        {
            case "qq":
                {
                    //获取 access_token
                    var accessToken_ResultEntity = QQ.AccessToken(new QQ_AccessToken_RequestEntity()
                    {
                        code = code
                    });

                    //获取 OpendId
                    var openId_ResultEntity = QQ.OpenId(new QQ_OpenId_RequestEntity()
                    {
                        access_token = accessToken_ResultEntity.access_token
                    });

                    //获取 UserInfo
                    var openId_Get_User_Info_ResultEntity = QQ.OpenId_Get_User_Info(new QQ_OpenAPI_RequestEntity()
                    {
                        access_token = accessToken_ResultEntity.access_token,
                        openid = openId_ResultEntity.openid
                    });

                    //身份唯一标识
                    openId = openId_ResultEntity.openid;
                }
                break;
            case "weibo":
                {
                    //获取 access_token
                    var accessToken_ResultEntity = Weibo.AccessToken(new Weibo_AccessToken_RequestEntity()
                    {
                        code = code
                    });

                    //获取 access_token 的授权信息
                    var tokenInfo_ResultEntity = Weibo.GetTokenInfo(new Weibo_GetTokenInfo_RequestEntity()
                    {
                        access_token = accessToken_ResultEntity.access_token
                    });

                    //获取 users/show
                    var userShow_ResultEntity = Weibo.UserShow(new Weibo_UserShow_RequestEntity()
                    {
                        access_token = accessToken_ResultEntity.access_token,
                        uid = Convert.ToInt64(tokenInfo_ResultEntity.uid)
                    });

                    openId = accessToken_ResultEntity.access_token;
                }
                break;
            case "github":
                {
                    //申请的应用名称,非常重要
                    GitHubConfig.ApplicationName = "netnrf";

                    //获取 access_token
                    var accessToken_ResultEntity = GitHub.AccessToken(new GitHub_AccessToken_RequestEntity()
                    {
                        code = code
                    });

                    //获取 user
                    var user_ResultEntity = GitHub.User(new GitHub_User_RequestEntity()
                    {
                        access_token = accessToken_ResultEntity.access_token
                    });

                    openId = user_ResultEntity.id.ToString();
                }
                break;
            case "taobao":
                {
                    //获取 access_token
                    var accessToken_ResultEntity = Taobao.AccessToken(new Taobao_AccessToken_RequestEntity()
                    {
                        code = code
                    });

                    openId = accessToken_ResultEntity.open_uid;
                }
                break;
            case "microsoft":
                {
                    //获取 access_token
                    var accessToken_ResultEntity = MicroSoft.AccessToken(new MicroSoft_AccessToken_RequestEntity()
                    {
                        code = code
                    });

                    //获取 user
                    var user_ResultEntity = MicroSoft.User(new MicroSoft_User_RequestEntity()
                    {
                        access_token = accessToken_ResultEntity.access_token
                    });

                    openId = user_ResultEntity.id.ToString();
                }
                break;
        }
    }
    catch (Exception ex)
    {
        throw new Exception(ex.Message);
    }

    //拿到登录标识
    if (string.IsNullOrWhiteSpace(openId))
    {
        //TO DO
    }

    return View();
}
MIT License Copyright (c) 2018 Tomorrow Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

第三方OAuth授权快捷登录(QQ、微博、GitHub、淘宝/天猫、Microsoft) 展开 收起
C#
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
C#
1
https://gitee.com/lxh__023/Netnr.Login.git
git@gitee.com:lxh__023/Netnr.Login.git
lxh__023
Netnr.Login
Netnr.Login
master

搜索帮助