RA.Utilities.Authorization
10.0.4
Prefix Reserved
dotnet add package RA.Utilities.Authorization --version 10.0.4
NuGet\Install-Package RA.Utilities.Authorization -Version 10.0.4
<PackageReference Include="RA.Utilities.Authorization" Version="10.0.4" />
<PackageVersion Include="RA.Utilities.Authorization" Version="10.0.4" />
<PackageReference Include="RA.Utilities.Authorization" />
paket add RA.Utilities.Authorization --version 10.0.4
#r "nuget: RA.Utilities.Authorization, 10.0.4"
#:package RA.Utilities.Authorization@10.0.4
#addin nuget:?package=RA.Utilities.Authorization&version=10.0.4
#tool nuget:?package=RA.Utilities.Authorization&version=10.0.4
RA.Utilities.Authorization
RA.Utilities.Authorization provides a strongly-typed, injectable IUserContext service that wraps HttpContext.User so you never hand-parse claims, role-check with magic strings, or inject IHttpContextAccessor into your business logic. Register it in Program.cs and you have typed access to user identity everywhere.
Getting started
Install the package via the .NET CLI:
dotnet add package RA.Utilities.Authorization
Or through the NuGet Package Manager in Visual Studio.
🔗 Dependencies
Prerequisites
This package works on top of an existing authentication setup. It expects that the user's identity (ClaimsPrincipal) has been populated by authentication middleware. It pairs seamlessly with RA.Utilities.Authentication.JwtBearer.
Usage
1. Register the Service
In your Program.cs, register IHttpContextAccessor (required by the IUserContext implementation) and call AddUserContext() to register IUserContext as scoped:
using RA.Utilities.Authorization.Extensions;
var builder = WebApplication.CreateBuilder(args);
// Assumes you have authentication configured, e.g., using RA.Utilities.Authentication.JwtBearer
// builder.Services.AddJwtBearerAuthentication(builder.Configuration);
builder.Services.AddHttpContextAccessor();
builder.Services.AddUserContext();
2. Inject and Use IUserContext
Inject IUserContext into controllers, Minimal API endpoints, or services:
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using RA.Utilities.Authorization;
[ApiController]
[Route("api/[controller]")]
[Authorize]
public class ProfileController : ControllerBase
{
private readonly IUserContext _user;
public ProfileController(IUserContext user) => _user = user;
[HttpGet]
public IActionResult GetProfile()
{
if (!_user.IsAuthenticated)
return Unauthorized();
return Ok(new
{
UserId = _user.UserId,
Name = _user.Name,
Email = _user.Email,
IsAdmin = _user.IsInRole("Admin")
});
}
}
Minimal API Example
app.MapGet("/me", (IUserContext user) => new
{
user.UserId,
user.Name,
user.Email
}).RequireAuthorization();
API Reference
IUserContext
| Member | Type | Description |
|---|---|---|
| IsAuthenticated | bool |
Whether the current user is authenticated. |
| Id | string? |
The user's unique identifier from the NameIdentifier claim, or null. |
| UserId | Guid |
The user's ID as a Guid. Throws InvalidOperationException if not authenticated or not a valid Guid. |
| Name | string? |
The user's name from the Name claim, or null. |
string? |
The user's email from the Email claim, or null. | |
| IsInRole(string) | bool |
Whether the user is a member of the specified role. |
| HasClaim(string, string) | bool |
Whether the user has a claim with the given type and value. |
| HasScope(string) | bool |
Whether the user has the specified OAuth 2.0 / OIDC scope. Handles space-separated scopes. |
| GetClaimValue(string) | string? |
The value of the first claim with the specified type, or null. |
| GetClaimValues(string) | IEnumerable<string> |
All values for a specific claim type. |
ClaimsPrincipalExtensions
| Method | Description |
|---|---|
GetUserId() |
Parses the NameIdentifier claim into a Guid. Throws InvalidOperationException if unavailable. |
HasClaim(type, value) |
Checks for a claim with the given type and value. |
HasScope(value) |
Checks for an OIDC scope (space-separated or individual claim entries). |
Additional documentation
For more information on how this package fits into the larger RA.Utilities ecosystem, please see the main repository documentation.
- To learn about setting up JWT authentication, see the
RA.Utilities.Authentication.JwtBearerpackage documentation.
Contributing
Contributions are welcome! If you have a suggestion, find a bug, or want to provide feedback, please open an issue in the RA.Utilities GitHub repository.
Pull Request Process
- Fork the Repository: Start by forking the RA.Utilities repository.
- Create a Branch: Create a new branch for your feature or bug fix from the
mainbranch. Please use a descriptive name (e.g.,feature/add-claim-transformsorfix/scope-parsing). - Make Your Changes: Write your code, ensuring it adheres to the existing coding style. Add or update XML documentation for any new public APIs.
- Update README: If you are adding new functionality, please update the
README.mdfile accordingly. - Submit a Pull Request: Push your branch to your fork and open a pull request to the
mainbranch of the original repository. Provide a clear description of the changes you have made.
Coding Standards
- Follow the existing coding style and conventions used in the project.
- Ensure all public members are documented with clear XML comments.
- Keep changes focused. A pull request should address a single feature or bug.
Thank you for contributing!
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | 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. |
-
net10.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.