DroidSolutions.Oss.AuthClaimBinder 1.0.0

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package DroidSolutions.Oss.AuthClaimBinder --version 1.0.0
NuGet\Install-Package DroidSolutions.Oss.AuthClaimBinder -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="DroidSolutions.Oss.AuthClaimBinder" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DroidSolutions.Oss.AuthClaimBinder --version 1.0.0
#r "nuget: DroidSolutions.Oss.AuthClaimBinder, 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.
// Install DroidSolutions.Oss.AuthClaimBinder as a Cake Addin
#addin nuget:?package=DroidSolutions.Oss.AuthClaimBinder&version=1.0.0

// Install DroidSolutions.Oss.AuthClaimBinder as a Cake Tool
#tool nuget:?package=DroidSolutions.Oss.AuthClaimBinder&version=1.0.0

DroidSolutions Auth Claim Binder

Custom modelbinder for ASP.NET Core MVC to allow injecting claims into controller actions.

Coverage Status Nuget semantic-release

This NuGet package contains the FromClaim attribute that can be used in controller actions to inject a value from a claim, for example the user id or role. It also offers a ASP.NET Core Modelbinder and a Modelbinder provider.

This project wass inspired by this blogpost.

Installation

You can grab this NuGet package from NuGet.org.

How it works

The modelbinder will search available claims from the authentication for the given name you used as argument name in your controller action. Specifically the claims on the user property in the HttpContext objects are used. If a claim with the given name is found the modelbinder will try to convert the value to the type you have specified. Currently the following types are supported:

  • string
  • Guid
  • Enum

Note: Which claims exist in the User object is dependent on your authentication middleware and out of the scope of this repository. For example you can extend the AuthenticationHandler like described in the official docs and add custom claims to the user.

Usage

To use the attribute first the modelbinder provider must be added to the list of ModelBinderProviders.

Register

The modelbinder provider can be added to the MVC options like this

builder.Services.AddControllers(options =>
  {
    options.ModelBinderProviders.Insert(0, new ClaimModelBinderProvider());
  });

or you when using MVC you can use

builder.Services.AddMvc(options =>
  {
    options.ModelBinderProviders.Insert(0, new ClaimModelBinderProvider());
  });

See the official documentation for more info.

Configuration

The ClaimsModelBinder can be configured via ClaimBinderSettings. Those settings are retrieved via IOptions<ClaimBinderSettings> so you just need to configure it setting up your dependency injection.

AliasConfig

If the claims you have from your authentication method are complex or you want to use other argument names in your controller actions you can provide an alias list via ClaimBinderSettings.AliasConfig.

This is a dictionary of string keys (the key you want to use as argument names) and a list of strings that serve as aliases. For example if you use Open ID Connect and get you claims from the JWT they might be some long strings or urls. The example below uses the key user and adds an alias for System.Security.Claims.ClaimTypes.NameIdentifier. This way the binder finds the value of the claim with the name of the ClaimTypes.NameIdentifier when you use user as the argument name.

builder.Services.Configure<ClaimBinderSettings>(o => o.AliasConfig = new Dictionary<string, List<string>>
{
  { "role", new List<string> { ClaimTypes.Role } },
});

Use the attribute

To use it simply add the FromClaim attribute before your method parameter. The name of the argument is the name of the claim that is searched (or one of the aliases you have configured) and the value will be converted to the type you used. See above for a list of supported types.

public async Task<IActionResult> DoSomething([FromClaim] string user, [FromClaim] BasicAuthRole role, CancellationToken cancellationToken)
{
  // ...
}

Development

If you want to add a feature or fix a bug, be sure to read the contribution guidelines first before open a pull request.

You'll need to install the .NET SDK which can be downloaded here.

To build the project, just run dotnet build in the repository root. Tests can be executed with dotnet test and code coverage is generated by either running dotnet test --collect:"XPlat Code Coverage" or dotnet test /p:CollectCoverage=true.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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 was computed.  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. 
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.2.0 545 2/13/2023
1.1.0 305 1/18/2023
1.0.0 647 7/15/2022
1.0.0-develop.1 232 7/6/2022