Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/a-postx/delobytes.aspnetcore.infrastructure
Компоненты инфраструктурного слоя для .net core: аутентификация на базе KeyCloak, Auth0.
https://github.com/a-postx/delobytes.aspnetcore.infrastructure
auth0 authentication jwt keycloak
Last synced: 25 days ago
JSON representation
Компоненты инфраструктурного слоя для .net core: аутентификация на базе KeyCloak, Auth0.
- Host: GitHub
- URL: https://github.com/a-postx/delobytes.aspnetcore.infrastructure
- Owner: a-postx
- License: mit
- Created: 2021-11-18T06:04:47.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2023-09-15T23:59:35.000Z (over 1 year ago)
- Last Synced: 2025-01-02T07:24:26.207Z (25 days ago)
- Topics: auth0, authentication, jwt, keycloak
- Language: C#
- Homepage:
- Size: 85 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.en.md
- License: LICENSE
Awesome Lists containing this project
README
# Delobytes.AspNetCore.Infrastructure
Infrastructure layer components for .Net Core web-API applications.[RU](README.md), [EN](README.en.md)
## Installation
The fastest way to add package to your app is via [NuGet](https://www.nuget.org/packages/Delobytes.AspNetCore.Infrastructure):
dotnet add package Delobytes.AspNetCore.Infrastructure
## Usage
## KeyCloak Authentication
Add JWT-authentication based on KeyCloak. You can add claim names that should be taken from JWT-token and added to the user identity if needed.1. Set up KeyCloak, create realm and open its endpoint configuration page (/.well-known/openid-configuration).
2. Add KeyCloak authentication handler to your application:
```csharp
public void ConfigureServices(IServiceCollection services)
{
services.AddKeyCloakAuthentication("SchemeName", true, options =>
{
options.Authority = "https://mykeycloakinstallation.com/auth/realms/myrealm"; //"issuer" endpoint
options.Audience = "account";
options.OpenIdConfigurationEndpoint = "https://mykeycloakinstallation.com/auth/realms/myrealm/.well-known/openid-configuration";
options.TokenValidationParameters = new TokenValidationOptions
{
RequireExpirationTime = true,
RequireSignedTokens = true,
ValidateIssuer = true,
ValidIssuer = "https://mykeycloakinstallation.com/auth/realms/myrealm",
ValidateAudience = true,
ValidAudience = "account",
ValidateIssuerSigningKey = true,
ValidateLifetime = true,
ClockSkew = TimeSpan.FromMinutes(2),
};
});
}public void Configure(IApplicationBuilder application)
{
application
.UseAuthentication();
}
```3. Set attribute Authorize to a method or controller:
```csharp
[Route("[controller]")]
[ApiController]
[Authorize]
public class HomeController : ControllerBase
{
[HttpPost]
public Task PostInfoAsync(
[FromServices] IPostClientInfoAh handler,
[FromBody] InfoSm infoSm,
CancellationToken cancellationToken)
{
return handler.ExecuteAsync(infoSm, cancellationToken);
}
}
```## Auth0 Authentication
Add JWT-authentication based on Auth0. You can add claim names that should be taken from JWT-token and added to the user identity if needed.1. Register on Auth0, create application and open its endpoint configuration page (/.well-known/openid-configuration).
2. Add authentication handler:
```csharp
public void ConfigureServices(IServiceCollection services)
{
services.AddAuth0Authentication("SchemeName", true, options =>
{
options.Authority = "https://dev-xxxxxxxx.eu.auth0.com";
options.Audience = "https://myapp-audience.com";
options.OpenIdConfigurationEndpoint = "https://dev-xxxxxxxx.eu.auth0.com/.well-known/openid-configuration";
options.TokenValidationParameters = new TokenValidationOptions
{
RequireExpirationTime = true,
RequireSignedTokens = true,
ValidateIssuer = true,
ValidIssuer = "https://dev-xxxxxxxx.eu.auth0.com/",
ValidateAudience = true,
ValidAudience = "account",
ValidateIssuerSigningKey = true,
ValidateLifetime = true,
ClockSkew = TimeSpan.FromMinutes(2),
};
});
}public void Configure(IApplicationBuilder application)
{
application
.UseAuthentication();
}
```3. Set attribute Authorize to a method or controller:
```csharp
[Route("[controller]")]
[ApiController]
[Authorize]
public class HomeController : ControllerBase
{
[HttpPost]
public Task PostInfoAsync(
[FromServices] IPostClientInfoAh handler,
[FromBody] InfoSm infoSm,
CancellationToken cancellationToken)
{
return handler.ExecuteAsync(infoSm, cancellationToken);
}
}
```## License
[MIT](https://github.com/a-postx/Delobytes.AspNetCore.Infrastructure/blob/master/LICENSE)