Back to Devexpress

AuthenticationStandardEvents.OnCustomizeClaims Property

expressappframework-devexpress-dot-expressapp-dot-security-dot-authenticationstandardevents-174ceaea.md

latest4.3 KB
Original Source

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.v25.2.dll

NuGet Package : DevExpress.ExpressApp.Security

Declaration

csharp
public Action<CustomizeClaimContext> OnCustomizeClaims { get; set; }
vb
Public Property OnCustomizeClaims As Action(Of CustomizeClaimContext)

Property Value

TypeDescription
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)

csharp
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’s User.Claims property.
  • In an MVC controller, you can use the controller’s User.Claims property. In middleware, use the HttpContext’s context.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

csharp
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);
            }
        }
    }
}

See Also

OnCustomizeLoginToken

Authentication System Architecture (Blazor)

AuthenticationStandardEvents Class

AuthenticationStandardEvents Members

DevExpress.ExpressApp.Security Namespace