Oidc.Auth
1.0.2
dotnet add package Oidc.Auth --version 1.0.2
NuGet\Install-Package Oidc.Auth -Version 1.0.2
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="Oidc.Auth" Version="1.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Oidc.Auth" Version="1.0.2" />
<PackageReference Include="Oidc.Auth" />
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 Oidc.Auth --version 1.0.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Oidc.Auth, 1.0.2"
#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 Oidc.Auth@1.0.2
#: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=Oidc.Auth&version=1.0.2
#tool nuget:?package=Oidc.Auth&version=1.0.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Oidc.Auth
A lightweight, reusable OIDC (OpenID Connect) authentication helper for .NET 8 ASP.NET Core applications.
- Build authorization redirect URLs with a secure random nonce
- Exchange authorization codes for tokens via a typed
HttpClient - Zero boilerplate: register with a single
AddOidcAuth(...)call - Fully configurable via the options pattern or
appsettings.json
Installation
dotnet add package Oidc.Auth
Quick Start
Option 1 — Inline options (environment variables, secrets manager, etc.)
// Program.cs
using Oidc.Auth.Extensions;
builder.Services.AddOidcAuth(options =>
{
options.AuthorizationEndpoint = Environment.GetEnvironmentVariable("OAUTH_URL")!;
options.TokenEndpoint = Environment.GetEnvironmentVariable("OTOKEN_URL")!;
options.ClientId = Environment.GetEnvironmentVariable("OAUTH_CLIENT_ID")!;
options.ClientSecret = Environment.GetEnvironmentVariable("OAUTH_CLIENT_SECRET")!;
options.Scope = "openid profile email"; // default: "openid"
});
Option 2 — appsettings.json
// appsettings.json
{
"OidcAuth": {
"AuthorizationEndpoint": "https://your-idp.example.com/auth",
"TokenEndpoint": "https://your-idp.example.com/token",
"ClientId": "your-client-id",
"ClientSecret": "your-client-secret",
"Scope": "openid profile email"
}
}
// Program.cs
builder.Services.AddOidcAuth(builder.Configuration);
// or with a custom section name:
builder.Services.AddOidcAuth(builder.Configuration, sectionName: "MyOidcSection");
Usage
Inject IOidcAuthService into your controller or service:
using Oidc.Auth.Abstractions;
using Microsoft.AspNetCore.Mvc;
[ApiController]
[Route("auth")]
public class AuthController(IOidcAuthService oidcAuth) : ControllerBase
{
[HttpGet("login")]
public IActionResult Login([FromQuery] string redirect_uri)
{
var url = oidcAuth.BuildAuthorizationUrl(redirect_uri);
return Ok(new { url });
}
[HttpGet("callback")]
public async Task<IActionResult> Callback([FromQuery] string code, [FromQuery] string redirect_uri)
{
var tokenResponse = await oidcAuth.ExchangeCodeForTokenAsync(code, redirect_uri);
return Ok(tokenResponse);
}
}
Configuration Reference
| Property | Type | Required | Default | Description |
|---|---|---|---|---|
AuthorizationEndpoint |
string |
✅ | — | IdP authorization URL |
TokenEndpoint |
string |
✅ | — | IdP token URL |
ClientId |
string |
✅ | — | OAuth2 client ID |
ClientSecret |
string |
✅ | — | OAuth2 client secret |
Scope |
string |
❌ | openid |
Space-separated OAuth2 scopes |
ResponseType |
string |
❌ | code |
OAuth2 response type |
ResponseMode |
string |
❌ | query |
OAuth2 response mode |
NonceLength |
int |
❌ | 32 |
Length of the generated nonce |
Error Handling
ExchangeCodeForTokenAsync throws OidcAuthException on:
- Non-2xx responses from the token endpoint (includes status code + body in message)
- Network/IO errors (wrapped with the original exception as inner)
using Oidc.Auth.Exceptions;
try
{
var token = await oidcAuth.ExchangeCodeForTokenAsync(code, redirectUri);
}
catch (OidcAuthException ex)
{
// handle token exchange failure
}
License
MIT
| Product | Versions 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.
-
net8.0
- Microsoft.Extensions.Configuration.Abstractions (>= 8.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
- Microsoft.Extensions.Http (>= 8.0.1)
- Microsoft.Extensions.Options (>= 8.0.2)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 8.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.