-
Notifications
You must be signed in to change notification settings - Fork 28
Allow specifying user roles for ApiKey and Basic Authentication #170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Co-authored-by: marcominerva <3522534+marcominerva@users.noreply.github.com>
…rences Co-authored-by: marcominerva <3522534+marcominerva@users.noreply.github.com>
… Administrator, use configured RoleClaimType Co-authored-by: marcominerva <3522534+marcominerva@users.noreply.github.com>
Co-authored-by: marcominerva <3522534+marcominerva@users.noreply.github.com>
Co-authored-by: marcominerva <3522534+marcominerva@users.noreply.github.com>
Co-authored-by: marcominerva <3522534+marcominerva@users.noreply.github.com>
… classes Co-authored-by: marcominerva <3522534+marcominerva@users.noreply.github.com>
Co-authored-by: marcominerva <3522534+marcominerva@users.noreply.github.com>
…ervice Co-authored-by: marcominerva <3522534+marcominerva@users.noreply.github.com>
Co-authored-by: marcominerva <3522534+marcominerva@users.noreply.github.com>
Updated `SimpleAuthentication.Swashbuckle.csproj` to use `SimpleAuthenticationTools.Abstractions` version 3.0.13. Added `SimpleAuthenticationTools.Abstractions` version 3.0.13 to `SimpleAuthentication.csproj`. Removed direct project reference to `SimpleAuthentication.Abstractions` in favor of package dependency.
Co-authored-by: marcominerva <3522534+marcominerva@users.noreply.github.com>
Co-authored-by: marcominerva <3522534+marcominerva@users.noreply.github.com>
…etting Remove EnableJwtBearerService setting and always register IJwtBearerService
# Conflicts: # src/SimpleAuthentication.Abstractions/ApiKey/ApiKey.cs # src/SimpleAuthentication.Abstractions/ApiKey/ApiKeySettings.cs # src/SimpleAuthentication.Abstractions/BasicAuthentication/BasicAuthenticationSettings.cs # src/SimpleAuthentication.Abstractions/BasicAuthentication/Credential.cs # src/SimpleAuthentication.Swashbuckle/SimpleAuthentication.Swashbuckle.csproj # src/SimpleAuthentication/SimpleAuthentication.csproj
Co-authored-by: marcominerva <3522534+marcominerva@users.noreply.github.com>
…-authentication Add Roles support to ApiKey and Basic Authentication
- Ensure roles in `ApiKey` and `Credential` classes are non-optional and add equality/hash code methods. - Initialize roles and claims with empty lists in settings and validation result classes to prevent null references. - Simplify role claim addition in authentication handlers by removing null checks. - Maintain consistency in `SimpleAuthenticationExtensions` by initializing roles with empty lists if null.
Updated MeController to include role-based authorization by modifying the Get method to accept role settings and return user roles. Added AdministratorOnly and UserOnly endpoints with role-based access. Updated User record to include roles. Extended appsettings.json for role configuration. Adjusted Program.cs for new authorization requirements.
Replaced `ArgumentNullException.ThrowIfNull` with `ArgumentException.ThrowIfNullOrWhiteSpace` across multiple files to ensure parameters are not null, empty, or whitespace. Updated `SwaggerExtensions.cs` and `OpenApiExtensions.cs` to use this validation for `sectionName`. In `SimpleAuthenticationExtensions.cs`, adjusted `defaultAuthenticationScheme` handling and improved validation for `JwtBearerSettings`, `ApiKeySettings`, and `BasicAuthenticationSettings` properties. These changes prevent runtime errors by ensuring meaningful content in string parameters.
This was
linked to
issues
Oct 16, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request introduces support for assigning roles to users authenticated via API Key and Basic Authentication, enhancing authorization capabilities across the library and its sample projects. The changes include updates to configuration files, controller logic, and data models to enable role-based claims and authorization checks, along with documentation improvements to guide usage.
Role-based authentication and authorization:
Rolesproperty in configuration files (appsettings.json). These roles are now added as claims to the user's identity upon successful authentication. [1] [2][Authorize(Roles = ...)]and policy requirements. TheUsermodel now includes a list of roles. [1] [2] [3] [4]Configuration and extensibility:
NameClaimType,RoleClaimType) in API Key and Basic Authentication configuration, allowing further customization of identity claims. [1] [2]IApiKeyValidator,IBasicAuthenticationValidator) in sample projects to support dynamic authentication scenarios when credentials are not fixed in configuration. [1] [2] [3] [4]Documentation improvements:
README.mdto document the new role assignment feature for API Key and Basic Authentication, including configuration examples and usage of ASP.NET Core's role-based authorization. Also clarified JWT Bearer registration behavior. [1] [2]Data model updates:
ApiKeyrecord to include aRolesproperty and implemented custom equality logic to ensure role lists do not affect key comparison.Minor adjustments:
EnableJwtBearerServicesetting from JWT Bearer configuration in sample projects for clarity. [1] [2]Closes #166
Closes #168