AuthenticationStandardEvents.OnCustomizeClaims Property
Specifies a delegate method that allows you to customize the set of claims added to the authentication cookie.
Namespace: DevExpress.ExpressApp.Security
Assembly: DevExpress.ExpressApp.Security.v24.2.dll
NuGet Package: DevExpress.ExpressApp.Security
Declaration
Property Value
Type | Description |
---|---|
Action<CustomizeClaimContext> | A delegate method that takes a context object as an argument. |
Remarks
Use this property in an ASP.NET Core Blazor application to customize a logged-in user’s claims. The specified delegate method is called after the user authentication succeeds and before the authentication cookie is issued.
Normally, claims are copied from the login token to the user’s authentication cookie. The OnCustomizeClaims
property allows you to intercept this process and change the existing claims or add additional ones. The list of claims is available through the conext.Claims
object.
Example:
File: MySolution.WebApi\startup.cs (MySolution.Blazor.Server\startup.cs)
services.AddXaf(Configuration, builder => {
// ...
builder.Security
.AddPasswordAuthentication(options => {
options.IsSupportChangePassword = true;
options.Events.OnCustomizeClaims = context => {
context.Claims.Add(new Claim("CustomClaim", "ClaimValue"));
};
});
// ...
});
Access Claims
You can use one of the following techniques to access claims from an ASP.NET Core Blazor application’s code at runtime:
- Use the
DevExpress.ExpressApp.Security.IPrincipalProvider
service’sUser.Claims
property. - In an MVC controller, you can use the controller’s
User.Claims
property. In middleware, use the HttpContext’scontext.User.Claims
property.
The code sample below demonstrates how to implement a custom Controller that injects the IPrincipalProvider
service and uses it to access claims:
File: MySolution.Blazor.Server\Controllers\MyController.cs
using System.Security.Claims;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Security;
namespace MainDemo.Blazor.Server.Controllers {
public class MyController : ViewController {
readonly IPrincipalProvider principalProvider;
public MyController() { }
[ActivatorUtilitiesConstructor]
public MyController(IServiceProvider serviceProvider) : this() {
principalProvider = serviceProvider.GetRequiredService<IPrincipalProvider>();
var _claimsPrincipal = (ClaimsPrincipal)principalProvider.User;
var customClaim = _claimsPrincipal.FindFirst(c => c.Type == "CustomClaim");
if(customClaim != null && customClaim.Value == "ClaimValue") {
Active.SetItemValue("CustomClaim", false);
}
}
}
}