DroidSolutions.Oss.AuthClaimBinder 1.0.0

.NET 6.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.
dotnet add package DroidSolutions.Oss.AuthClaimBinder --version 1.0.0
<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, C# scripting and .NET Interactive. 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.


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.


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


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.


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.


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)
  // ...


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 Versions
.NET net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
Compatible target framework(s)
Additional computed target framework(s)
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.0.0 280 7/15/2022
1.0.0-develop.1 180 7/6/2022