JoeDevSharp.Net.Extensions.OAuth2 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package JoeDevSharp.Net.Extensions.OAuth2 --version 1.0.0
                    
NuGet\Install-Package JoeDevSharp.Net.Extensions.OAuth2 -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="JoeDevSharp.Net.Extensions.OAuth2" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="JoeDevSharp.Net.Extensions.OAuth2" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="JoeDevSharp.Net.Extensions.OAuth2" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add JoeDevSharp.Net.Extensions.OAuth2 --version 1.0.0
                    
#r "nuget: JoeDevSharp.Net.Extensions.OAuth2, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package JoeDevSharp.Net.Extensions.OAuth2@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=JoeDevSharp.Net.Extensions.OAuth2&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=JoeDevSharp.Net.Extensions.OAuth2&version=1.0.0
                    
Install as a Cake Tool

Sure, here’s a clear and concise README in English for your OAuth2 framework:


Net.Extensions.OAuth2

Lightweight and extensible OAuth2 authentication framework for .NET, supporting multiple popular providers and enabling easy custom provider implementation.


Features

  • Built-in support for Google, GitHub, Microsoft, Apple, Facebook, LinkedIn, Twitter, Keycloak.
  • OAuth2 authorization code flow with PKCE and token refresh handling.
  • Standardized user info mapping to AuthUser.
  • Common interface IAuthProvider for unified handling.
  • Abstract base class CustomOAuth2Provider to simplify custom provider creation.
  • Compatible with .NET 6+ and uses standard HttpClient.

Supported Providers

Provider Default Scopes Authorization URL Token URL User Info URL
Google openid email profile https://accounts.google.com/o/oauth2/v2/auth https://oauth2.googleapis.com/token https://openidconnect.googleapis.com/v1/userinfo
GitHub read:user user:email https://github.com/login/oauth/authorize https://github.com/login/oauth/access_token https://api.github.com/user
Microsoft user.read https://login.microsoftonline.com/common/oauth2/v2.0/authorize https://login.microsoftonline.com/common/oauth2/v2.0/token https://graph.microsoft.com/v1.0/me
Apple name email https://appleid.apple.com/auth/authorize https://appleid.apple.com/auth/token (User info contained in id_token JWT)
Facebook public_profile email https://www.facebook.com/v12.0/dialog/oauth https://graph.facebook.com/v12.0/oauth/access_token https://graph.facebook.com/me?fields=id,name,email,picture
LinkedIn r_liteprofile r_emailaddress https://www.linkedin.com/oauth/v2/authorization https://www.linkedin.com/oauth/v2/accessToken https://api.linkedin.com/v2/me
Twitter tweet.read users.read offline.access https://twitter.com/i/oauth2/authorize https://api.twitter.com/2/oauth2/token https://api.twitter.com/2/users/me
Keycloak openid profile email Configurable per instance Configurable per instance Configurable per instance

Basic usage example with GoogleProvider

var provider = new GoogleProvider(
    clientId: "your-client-id",
    clientSecret: "your-client-secret",
    redirectUri: "http://localhost:60000/",
    scopes: new[] { "openid", "email", "profile" }
);

var user = await provider.LoginAsync();

Console.WriteLine($"Authenticated user: {user?.Username} - {user?.Email}");

Creating a Custom Provider

To support any other provider, extend the CustomOAuth2Provider base class and override the GetUserInfoAsync method.

Simple example

public class MyCustomProvider : CustomOAuth2Provider
{
    public MyCustomProvider(OAuth2Options options) : base(options) { }

    public override async Task<AuthUser> GetUserInfoAsync(string accessToken)
    {
        using var client = new HttpClient();
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

        var json = await client.GetStringAsync(Options.UserInfoEndpoint);
        var data = JsonDocument.Parse(json).RootElement;

        return new AuthUser
        {
            Id = data.GetProperty("id").GetString() ?? "",
            Username = data.TryGetProperty("username", out var u) ? u.GetString() ?? "" : "",
            Email = data.TryGetProperty("email", out var e) ? e.GetString() ?? "" : "",
            Picture = data.TryGetProperty("avatar", out var a) ? a.GetString() ?? "" : "",
            Roles = new List<string>(),
            Claims = data.EnumerateObject().ToDictionary(x => x.Name, x => x.Value.ToString())
        };
    }
}

IAuthProvider Interface

public interface IAuthProvider
{
    bool IsAuthenticated { get; }
    AuthUser? CurrentUser { get; }
    Task<AuthUser?> LoginAsync();
    Task LogoutAsync();
}

Main Models

OAuth2Options

  • ClientId
  • ClientSecret
  • RedirectUri
  • AuthorizationEndpoint
  • TokenEndpoint
  • UserInfoEndpoint
  • Scopes

OAuth2Token

  • AccessToken
  • RefreshToken
  • Scope
  • TokenType
  • ExpiresAt

AuthUser

  • Id
  • Username
  • Email
  • Picture
  • Roles
  • Claims

Helpers

  • OAuth2Helper.GetCodeViaLocalServerAsync(authUrl, redirectUri) Runs a local HTTP server to catch authorization code from redirect.

  • OAuth2Helper.ExchangeCodeForTokenAsync(code, options) Exchanges authorization code for OAuth2 token.


Requirements

  • .NET 6 or higher
  • System.Text.Json
  • HttpClient

License

MIT — free to use and modify.


If you want, I can help prepare a GitHub repo with ready-to-use examples and tests. Want me to do that?

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.5 70 6/27/2025
1.0.4 66 6/27/2025
1.0.3 67 6/27/2025
1.0.2 78 6/27/2025
1.0.1 87 6/27/2025
1.0.0 117 6/27/2025