NET projects integrate OAuth2 's most comprehensive and convenient login framework

NET projects integrate OAuth2 's most comprehensive and convenient login framework

MrHuo.OAuth is the most comprehensive and convenient framework for integrating OAuth2 login for. NET projects

最后更新 4/12/2022 10:48 PM
mrhuo
预计阅读 8 分钟
分类
.NET
专题
C#Open Source Project
标签
.NET C# open source authentication Open Source C#

MrHuo.OAuth

It is the most comprehensive and convenient framework for.NET projects to integrate OAuth2 login

Travis (.org) GitHub stars GitHub license

Available for. NET Core projects or. NET Framework 4.6 projects

体验网址:https://oauthlogin.net/


Supported platforms

  • Baidu (available)
  • Weixin Official Accounts (available)
  • Gitlab (available)
  • Gitee (available)
  • GitHub (available)
  • Huawei (available)
  • Coding.net (available)
  • Sina Weibo (available)
  • Alipay (available)
  • OSChina (available)
  • Thunder (available)
  • In-nail login (available)
  • Nail scanning code login (available)
  • QQ (available)
  • Microsoft (available)
  • Xiaomi (available)
  • StackOverflow (available)
  • Facebook(可用) by Donma Hsu
  • Google (available)
  • LinkedIn (available, nuget to be released)
  • 抖音(可用,待发布 nuget) by feng lui
  • 快手(可用,待发布 nuget) by feng lui
  • [] WeChat open platform (to be tested)
  • [] Meituan (to be tested)

plan

  • flying book
  • Taobao
  • watermelon
  • today's headlines
  • Renren. Com
  • Teambition
  • Pinterest
  • Twitter
  • Enterprise WeChat QR code login
  • Enterprise WeChat website login
  • cool Jiale
  • the hungry
  • Jingdong
  • Alibaba Cloud
  • Himalayan...

use method

新建项目 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
  1. 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"
    }
  }
}
  1. 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);
        }
    }
}
  1. Views
<!--在代码中放置授权按钮-->
<a href="/oauth/baidu">Baidu 登录</a>
<a href="/oauth/wechat">Wechat 扫码登录</a>
<!-- //其他登录方式照样子往下写 -->

extended

扩展其他平台非常容易,拿 Gitee 平台的代码来说:https://github.com/mrhuo/MrHuo.OAuth/tree/main/MrHuo.OAuth.Gitee

Step 1: Find the OAuth document corresponding to the platform, find the interface to obtain user information and return JSON, and convert it to a C#entity class. As follows:

Extend user attributes based on your needs and interface standards

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; }

    //...其他属性类似如上
}
Step 2: Write the authorization interface for the corresponding platform
/// <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";
}

With annotations, there are ten lines in total, which, as you can see, is very convenient. If the platform protocol follows the OAuth2 standard, then just a few lines will be fine.

Even the WeChat login implementation of modifying fields is just complicated. It only needs to define basic parameters. The code is as follows:

/// <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",
        };
    }
}

special contribution

  1. Carl
  2. Donma Hsu
  3. feng lui

In order to allow all friends to smoothly use various platform login components, I urgently request APPIDs of various platforms for testing. Friends who have resources in this regard contact me. Friends who provide testing can permanently appear in the special contribution list on the project's homepage, with links.

Email: admin@mrhuo.com Remarks: OAuth Appid

Contribution

  1. Welcome to participate in development and contribute other unfinished platform code. 2. Welcome to submit the requirements platform in the issue, bring the platform link address, and we will join the plan. 3. Welcome to submit various suggestions for civilized exchanges.

License

Apache-2.0 License

project address

MrHuo.OAuth

Keep Exploring

延伸阅读

更多文章
同分类 / 同标签 4/22/2026

Support for. NET by operating system versions (250707 update)

Use virtual machines and test machines to test the support of each version of the operating system for. NET. After installing the operating system, it is passed by measuring the corresponding running time of the installation and being able to run the Stardust Agent.

继续阅读
同分类 / 同标签 2/7/2026

Summary of experience in using AOT

From the very beginning of project creation, you should develop a good habit of conducting AOT release testing in a timely manner whenever new features are added or newer syntax is used.

继续阅读