Revisit the dynamic claims documentation

pull/18172/head^2
Halil İbrahim Kalkan 2 years ago
parent 74a5b9cbaa
commit 0a911c6657

@ -1,14 +1,18 @@
# Dynamic Claims
## What is Dynamic Claims and Why do we need it
When a client authenticates and obtains an access token or an authentication cookie, the claims in that token or cookie are not changed unless it re-authenticates. For most of the claims, that may not be a problem since claims are not frequently changing values. However, some claims may be changed and these changes should be reflected to the current session immediately. For example, we can revoke a role from a user and that should be immediately effective, otherwise user will continue to use that role's permissions until re-login to the application.
We use claims-based authentication in ASP.NET Core, It will be store the claims in the cookie or token. But the claims are static, it will be not change after the user re-login. If the user changed its username or role, we still get the old claims.
ABP's dynamic claims feature is used to automatically and dynamically override the configured claim values in the client's authentication token/cookie by the latest values of these claims.
The `Dynamic Claims` feature is used to dynamically generate claims for the user in each request. You can always get the latest user claims.
## How to Use
## How to use it
This feature is disabled by default. You should enable it for your application and use the Dynamic Claims middleware.
This feature is disabled by default. You can enable it by following code:
> **Beginning from the v8.0, all the [startup templates](Startup-Templates/Index.md) are pre-configured and the dynamic claims feature is enabled by default. So, if you have created a solution with v8.0 and above, you don't need to make any configuration. Follow the instructions only if you've upgraded from a version lower than 8.0.**
### Enabling the Dynamic Claims
You can enable it by the following code:
````csharp
public override void ConfigureServices(ServiceConfigurationContext context)
@ -20,7 +24,7 @@ public override void ConfigureServices(ServiceConfigurationContext context)
}
````
If you are using the tiered solution you need to set the `RemoteRefreshUrl` to the Auth Server url in the UI project.
This is typically done on the authentication server. In a monolith application, you will typically have a single application, so you can configure it. If you are using the tiered solution structure (where the UI part is hosted in a separate application) you will need to also set the `RemoteRefreshUrl` to the Authentication Server's URL in the UI application. Example:
````csharp
public override void ConfigureServices(ServiceConfigurationContext context)
@ -33,45 +37,42 @@ public override void ConfigureServices(ServiceConfigurationContext context)
}
````
Then add the `DynamicClaims` middleware.
### The Dynamic Claims Middleware
Add the `DynamicClaims` middleware to all the applications that performs authentication (including the authentication server):
````csharp
public override void OnApplicationInitialization(ApplicationInitializationContext context)
public override void OnApplicationInitialization(
ApplicationInitializationContext context)
{
// Add this line before UseAuthorization.
app.UseDynamicClaims();
//...
app.UseDynamicClaims(); // Add this line before UseAuthorization.
app.UseAuthorization();
//...
}
````
## How it works
## How It Works
The `DynamicClaims` middleware will use `IAbpClaimsPrincipalFactory` to dynamically generate claims for the current user(`HttpContext.User`) in each request.
There are two implementations of `IAbpDynamicClaimsPrincipalContributor` for different scenarios.
### IdentityDynamicClaimsPrincipalContributor
There are two pre-built implementations of `IAbpDynamicClaimsPrincipalContributor` for different scenarios:
This implementation is used for the `Monolithic` solution. It will get the dynamic claims from the `IUserClaimsPrincipalFactory` and add/replace the current user claims.
It uses cache to improve performance. the cache will be invalidated when the user entity changed.
* `IdentityDynamicClaimsPrincipalContributor`: Provided by the [Identity module](Modules/Identity.md) and generates and overrides the actual dynamic claims, and writes to the distributed cache. Typically works in the authentication server in a distributed system.
* `RemoteDynamicClaimsPrincipalContributor`: For distributed scenarios, this implementation works in the UI application. It tries to get dynamic claim values in the distributed cache. If not found in the distributed cache, it makes an HTTP call to the authentication server and requests filling it by the authentication server. `AbpClaimsPrincipalFactoryOptions.RemoteRefreshUrl` should be properly configure to make it running.
### RemoteDynamicClaimsPrincipalContributor
### IAbpDynamicClaimsPrincipalContributor
This implementation is used for the `Tiered` solution. It will get the dynamic claims from the cache of the Auth Server. It will call the `RemoteRefreshUrl` of the Auth Server to refresh the cache when the cache is invalid.
## IAbpDynamicClaimsPrincipalContributor
If you want to add your own dynamic claims contributor, you can a class that implement the `IAbpDynamicClaimsPrincipalContributor` interface. The framework will call the `ContributeAsync` method when get the dynamic claims.
> It better to use cache to improve performance.
If you want to add your own dynamic claims contributor, you can create a class that implement the `IAbpDynamicClaimsPrincipalContributor` interface (and register it to the [dependency injection](Dependency-Injection.md) system. ABP Framework will call the `ContributeAsync` method to get the claims. It better to use a kind of cache to improve the performance since that is a frequently executed method (in every HTTP request).
## AbpClaimsPrincipalFactoryOptions
`AbpClaimsPrincipalFactoryOptions` is the main options class to configure the behavior of the dynamic claims system. It has the following properties:
* `IsDynamicClaimsEnabled`: Enable or disable the dynamic claims feature.
* `RemoteRefreshUrl`: The url of the Auth Server to refresh the cache. It will be used by the `RemoteDynamicClaimsPrincipalContributor`. The default value is `/api/account/dynamic-claims/refresh`.
* `DynamicClaims`: A list of dynamic claim types, `DynamicClaims contributor`` will only handle the claim type in this list.
* `ClaimsMap`: A dictionary to map the claim types. This is used when the claim types are different between the Auth Server and the client. Already set up for common claim types by default
* `RemoteRefreshUrl`: The `url ` of the Auth Server to refresh the cache. It will be used by the `RemoteDynamicClaimsPrincipalContributor`. The default value is `/api/account/dynamic-claims/refresh ` and you should provide the full URL in the authentication server, like `http://my-account-server/api/account/dynamic-claims/refresh `.
* `DynamicClaims`: A list of dynamic claim types. Only the claims in that list will be overridden by the dynamic claims system.
* `ClaimsMap`: A dictionary to map the claim types. This is used when the claim types are different between the Auth Server and the client. Already set up for common claim types by default.
## See Also

Loading…
Cancel
Save