Polhem.OAuth2
1.4.0
dotnet add package Polhem.OAuth2 --version 1.4.0
NuGet\Install-Package Polhem.OAuth2 -Version 1.4.0
<PackageReference Include="Polhem.OAuth2" Version="1.4.0" />
<PackageVersion Include="Polhem.OAuth2" Version="1.4.0" />
<PackageReference Include="Polhem.OAuth2" />
paket add Polhem.OAuth2 --version 1.4.0
#r "nuget: Polhem.OAuth2, 1.4.0"
#:package Polhem.OAuth2@1.4.0
#addin nuget:?package=Polhem.OAuth2&version=1.4.0
#tool nuget:?package=Polhem.OAuth2&version=1.4.0
Polhem.OAuth2
English | 繁體中文
Lightweight sign-in for .NET with OAuth2 and OpenID Connect (OIDC). Desktop and console applications sign in through the system
browser with a loopback redirect and PKCE, on Windows, macOS and Linux. .NET MAUI applications on Android, iOS and Mac
Catalyst sign in through WebAuthenticator, directly or through their own back end. ASP.NET Core and ASP.NET (System.Web)
applications use the authorization code flow with PKCE, and keep each sign-in in a protected cookie.
Supported accounts
Every account signs in with the OAuth2 authorization code flow. "OAuth2 + OIDC" means the account also returns an OIDC ID token, which the library checks or reads the user from; "OAuth2" means the user comes from the provider's own API.
| Account | Protocol | Desktop and console | .NET MAUI | ASP.NET Core and System.Web |
|---|---|---|---|---|
| OAuth2 + OIDC | Yes | Directly on iOS and Mac Catalyst; through the back end on Android | Yes | |
| OAuth2 | Yes | Directly or through the back end | Yes | |
| LINE | OAuth2, with the email address from the OIDC ID token | Yes | Directly on iOS and Mac Catalyst; through the back end on Android | Yes |
| Microsoft Entra ID | OAuth2 + OIDC | Yes | Directly or through the back end | Yes |
| Auth0 | OAuth2 + OIDC | Yes | Directly or through the back end | Yes |
| Okta | OAuth2 + OIDC | Yes | Directly or through the back end | Yes |
| GitHub | OAuth2 | Yes | Through the back end only | Yes |
| Apple | OAuth2 + OIDC | No | Through the back end only | Yes |
| Any other OIDC provider, such as Keycloak or GitLab | OAuth2 + OIDC | Yes, when the provider accepts a public client | Directly or through the back end | Yes |
"Through the back end" means that the application signs in through its own ASP.NET Core back end, as the .NET MAUI guide describes. Each account's document says what to register with the provider and what it was tested with.
Packages
| Package | Target frameworks | Use it for |
|---|---|---|
| Polhem.OAuth2 | netstandard2.0, net8.0, net10.0 | The providers, sign-in from desktop, console and .NET MAUI applications, and other server frameworks |
| Polhem.OAuth2.AspNetCore | net10.0 | ASP.NET Core applications |
| Polhem.OAuth2.AspNet | net472 | ASP.NET Web Forms and MVC applications on System.Web |
dotnet add package Polhem.OAuth2
The web packages depend on Polhem.OAuth2, so a web application adds only its own package, such as
dotnet add package Polhem.OAuth2.AspNetCore.
1.x minor versions may contain breaking changes, which the changelog
marks. Reference the packages with a range that stays within one minor version, for example [1.4,1.5), rather than 1.*
(ADR-010).
Getting started with Google
OAuth2 in a desktop application
LoopbackOAuth2Client listens on the redirect URI, opens the sign-in in the default browser, and exchanges the code
when Google redirects back. Create an OAuth client of the Desktop app type in the Google Cloud console.
using Polhem.OAuth2;
var options = new GoogleOAuth2Options
{
ClientId = "your-client-id",
ClientSecret = "your-client-secret",
RedirectUri = "http://127.0.0.1:0/callback"
};
var client = new LoopbackOAuth2Client(options);
AuthorizationResult result = await client.SignInAsync();
if (result.IsSuccess)
Console.WriteLine($"{result.UserInfo.UserId} {result.UserInfo.UserName} {result.UserInfo.Email}");
else
Console.WriteLine($"The sign-in failed: {result.Exception.Message}");
OAuth2 in ASP.NET Core
Create an OAuth client of the Web application type, and register the callback URL as its redirect URI.
using Polhem.OAuth2;
builder.Services.AddControllers();
builder.Services.AddOAuth2Client("Google", new GoogleOAuth2Options
{
ClientId = "your-client-id",
ClientSecret = "your-client-secret",
RedirectUri = "https://localhost:7032/auth/callback"
});
var app = builder.Build();
app.MapControllers();
using Microsoft.AspNetCore.Mvc;
using Polhem.OAuth2;
using Polhem.OAuth2.AspNetCore;
public class AuthController(OAuth2Manager oauth2Manager) : ControllerBase
{
[HttpGet("/auth/login")]
public IActionResult Login()
{
return Redirect(oauth2Manager.CreateAuthorizationUrl(HttpContext, "Google"));
}
[HttpGet("/auth/callback")]
public async Task<IActionResult> Callback()
{
AuthorizationResult result = await oauth2Manager.CompleteAuthorizationAsync(HttpContext, HttpContext.RequestAborted);
return result.IsSuccess
? Content($"{result.UserInfo.UserId} {result.UserInfo.UserName} {result.UserInfo.Email}")
: Content($"The sign-in failed: {result.Exception.Message}");
}
}
OpenID Connect in a web application
OidcOAuth2Options.FromDiscoveryAsync reads the endpoints from the provider's discovery document. Call it once, when the
application starts:
var keycloak = await OidcOAuth2Options.FromDiscoveryAsync("https://sso.example.com/realms/example");
keycloak.ProviderName = "Keycloak";
keycloak.ClientId = "your-client-id";
keycloak.ClientSecret = "your-client-secret";
keycloak.RedirectUri = "https://localhost:7032/auth/callback";
builder.Services.AddOAuth2Client("Keycloak", keycloak);
The controller above works unchanged. For another OpenID Connect provider, such as GitLab or AWS Cognito, pass that
provider's address instead, and give it a ProviderName of its own. A provider that has options of its own, such as
Google, signs in with those options instead.
More documentation
Each account's document is linked from the table in "Supported accounts". The guides cover each kind of application and the topics they share:
- Options shared by every provider
- Desktop and console applications
- .NET MAUI applications
- ASP.NET Core
- ASP.NET (System.Web)
- Web applications: how a sign-in is kept
- Other server frameworks
- Apps with a separate back end
- Results, tokens and errors
- Identifying users
- Migrating from Bee.OAuth2
- Samples
Design decisions
The reasons behind the design are recorded in the architecture decision records.
License
MIT. Copyright (c) Polhem contributors.
Polhem.OAuth2 continues Bee.OAuth2.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. 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 is compatible. 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. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- System.Text.Json (>= 10.0.12)
-
net10.0
- No dependencies.
-
net8.0
- No dependencies.
NuGet packages (2)
Showing the top 2 NuGet packages that depend on Polhem.OAuth2:
| Package | Downloads |
|---|---|
|
Polhem.OAuth2.AspNetCore
OAuth2 sign-in for ASP.NET Core applications, with Google, Facebook, LINE, Microsoft Entra ID, Auth0, Okta, GitHub, Sign in with Apple and any OpenID Connect provider. |
|
|
Polhem.OAuth2.AspNet
OAuth2 sign-in for ASP.NET Web Forms and MVC applications on System.Web, with Google, Facebook, LINE, Microsoft Entra ID, Auth0, Okta, GitHub, Sign in with Apple and any OpenID Connect provider. |
GitHub repositories
This package is not used by any popular GitHub repositories.