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
                    
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="RA.Utilities.Authorization" Version="10.0.4" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="RA.Utilities.Authorization" Version="10.0.4" />
                    
Directory.Packages.props
<PackageReference Include="RA.Utilities.Authorization" />
                    
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 RA.Utilities.Authorization --version 10.0.4
                    
#r "nuget: RA.Utilities.Authorization, 10.0.4"
                    
#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 RA.Utilities.Authorization@10.0.4
                    
#: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=RA.Utilities.Authorization&version=10.0.4
                    
Install as a Cake Addin
#tool nuget:?package=RA.Utilities.Authorization&version=10.0.4
                    
Install as a Cake Tool

RA.Utilities.Authorization

NuGet version Codecov NuGet Downloads Documentation GitHub license

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.
Email 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.JwtBearer package 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

  1. Fork the Repository: Start by forking the RA.Utilities repository.
  2. Create a Branch: Create a new branch for your feature or bug fix from the main branch. Please use a descriptive name (e.g., feature/add-claim-transforms or fix/scope-parsing).
  3. Make Your Changes: Write your code, ensuring it adheres to the existing coding style. Add or update XML documentation for any new public APIs.
  4. Update README: If you are adding new functionality, please update the README.md file accordingly.
  5. Submit a Pull Request: Push your branch to your fork and open a pull request to the main branch 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • 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.

Version Downloads Last Updated
10.0.4 128 10/19/2026
10.0.3 107 10/18/2026
10.0.2 115 9/29/2026
10.0.1 110 9/28/2026
10.0.0 279 11/24/2025
10.0.0-rc.2 219 10/30/2025