{"id":37038583,"url":"https://github.com/droidsolutions/asp-auth-claim-binder","last_synced_at":"2026-01-14T04:35:14.116Z","repository":{"id":43199306,"uuid":"511126370","full_name":"droidsolutions/asp-auth-claim-binder","owner":"droidsolutions","description":"Custom modelbinder for ASP.NET Core MVC to allow injecting claims into controller actions.","archived":false,"fork":false,"pushed_at":"2025-11-26T10:22:23.000Z","size":1967,"stargazers_count":1,"open_issues_count":4,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-11-29T09:23:44.666Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/droidsolutions.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2022-07-06T12:22:49.000Z","updated_at":"2025-11-26T10:22:20.000Z","dependencies_parsed_at":"2023-12-25T11:25:44.014Z","dependency_job_id":"70c630c0-5de3-4f27-9096-208aa7e744ab","html_url":"https://github.com/droidsolutions/asp-auth-claim-binder","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/droidsolutions/asp-auth-claim-binder","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/droidsolutions%2Fasp-auth-claim-binder","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/droidsolutions%2Fasp-auth-claim-binder/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/droidsolutions%2Fasp-auth-claim-binder/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/droidsolutions%2Fasp-auth-claim-binder/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/droidsolutions","download_url":"https://codeload.github.com/droidsolutions/asp-auth-claim-binder/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/droidsolutions%2Fasp-auth-claim-binder/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28409540,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-14T01:52:23.358Z","status":"online","status_checked_at":"2026-01-14T02:00:06.678Z","response_time":107,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2026-01-14T04:35:13.456Z","updated_at":"2026-01-14T04:35:14.102Z","avatar_url":"https://github.com/droidsolutions.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# DroidSolutions Auth Claim Binder\n\nCustom modelbinder for ASP.NET Core MVC (and Web APIs) to allow injecting claims into controller actions.\n\n[![Coverage Status](https://coveralls.io/repos/github/droidsolutions/asp-auth-claim-binder/badge.svg?branch=main)](https://coveralls.io/github/droidsolutions/asp-auth-claim-binder?branch=main)\n![Nuget](https://img.shields.io/nuget/v/DroidSolutions.Oss.AuthClaimBinder)\n[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)\n\nThis 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.\n\nThis project was inspired by [this blogpost](https://www.davidkaya.com/custom-from-attribute-for-controller-actions-in-asp-net-core/).\n\n# Installation\n\nYou can grab this NuGet package from [NuGet.org](https://www.nuget.org/packages/DroidSolutions.Oss.AuthClaimBinder).\n\n# How it works\n\nThe 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.\nIf 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:\n\n- `string`\n- `Guid`\n- `Enum`\n- `int`\n\n**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](https://docs.microsoft.com/en-us/aspnet/core/security/authentication/?view=aspnetcore-7.0#authentication-handler) and add custom claims to the user.\n\n# Usage\n\nTo use the attribute first the `ClaimModelBinderProvider` must be added to the list of `ModelBinderProviders`.\n\n## Register\n\nThe `ClaimModelBinderProvider` can be added to the MVC options (when using Web API projects) like this\n\n```cs\nbuilder.Services.AddControllers(options =\u003e\n  {\n    options.ModelBinderProviders.Insert(0, new ClaimModelBinderProvider());\n  });\n```\n\nor you when using MVC you can use\n\n```cs\nbuilder.Services.AddMvc(options =\u003e\n  {\n    options.ModelBinderProviders.Insert(0, new ClaimModelBinderProvider());\n  });\n```\n\nSee the [official documentation](https://docs.microsoft.com/en-us/aspnet/core/mvc/advanced/custom-model-binding?view=aspnetcore-8.0#implementing-a-modelbinderprovider) for more info.\n\n## Configuration\n\nThe `ClaimsModelBinder` can be configured via `ClaimBinderSettings`. Those settings are retrieved via `IOptions\u003cClaimBinderSettings\u003e` so you just need to configure it setting up your dependency injection.\n\n### AliasConfig\n\nIf 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`.\n\nThis 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 `role` and adds an alias for `System.Security.Claims.ClaimTypes.Role`. This way the binder finds the value of the claim with the name of the `ClaimTypes.Role` when you use `role` as the argument name.\n\n```cs\nbuilder.Services.Configure\u003cClaimBinderSettings\u003e(o =\u003e o.AliasConfig = new Dictionary\u003cstring, List\u003cstring\u003e\u003e\n{\n  { \"role\", new List\u003cstring\u003e { ClaimTypes.Role } },\n});\n```\n\n## Use the attribute\n\nTo 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.\n\n```cs\npublic async Task\u003cIActionResult\u003e DoSomething([FromClaim] string user, [FromClaim] BasicAuthRole role, CancellationToken cancellationToken)\n{\n  // ...\n}\n```\n\n## Exceptions\n\nThere are special exceptions for errors during parsing of claim values which are explained below:\n\n### MissingClaimException\n\nWhen the `FromClaim` attribute is used but the claim (or it's alias) can not be found in the user claims, this exception is thrown. This is especially useful, if you want to show the caller of your API a BadRequest response or an message.\n\nFor example, let's assume you want to use a value from a special header you defined. You have set up your authentication handler to get the value from the header and put it in the user claims:\n```cs\n// Authorization handler\nif (Request.Headers.TryGetValue(\"x-myvalue\", out StringValues namespaceHeader))\n{\n  claims = claims.Append(new Claim(\"myvalue\", namespaceHeader[0]));\n}\n\n// Contoller\npublic async IActionResult MyMethod([FromClaim] string myvalue)\n{\n  // do something with myvalue\n}\n```\n\nThis works, when the x-myvalue header is provided, but if it is not, than the exception would be thrown (probably leading to a 500 beeing returned). Since you know the exception that is thrown you can set up an [Exception Filter](https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/filters) or a special controller action that handles errors and process the `MissingClaimException`. See [the ASP.NET Core docs](https://learn.microsoft.com/en-us/aspnet/core/web-api/handle-errors) for more info on how to set up error handling.\n\nThie `MissingClaimException` contains a property with the name of the claim. Be aware, that this is the name used in the controller attribute, so in case of the header example you probably need to write a custom message, indicating that the header is missing.\n\n### ClaimParsingException\n\nThis exception is thrown when a value cannot be parsed to the specified type. For example let's assume you have a Guid user id and want to use it in your controller:\n```cs\n// Contoller\npublic async IActionResult MyMethod([FromClaim] Guid user)\n{\n  // do something with user Id\n}\n```\n\nDependent on how you get the user claim it could be possible that it is not a valid Guid. In this case the `ClaimModelBinder` would throw a `ClaimParsingException` with the name of the claim (\"user\" in this case) and the destination type (`Guid`). This can help you set up special error handling for those cases.\n\n# Development\n\nIf you want to add a feature or fix a bug, be sure to read the [contribution guidelines](./CONTRIBUTING.md) first before open a pull request.\n\nYou'll need to install the .NET SDK which can be downloaded [here](https://dotnet.microsoft.com/en-us/download).\n\nTo 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`.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdroidsolutions%2Fasp-auth-claim-binder","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdroidsolutions%2Fasp-auth-claim-binder","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdroidsolutions%2Fasp-auth-claim-binder/lists"}