BFF User Endpoint Extensibility
The BFF user endpoint can be customized by implementing the IUserEndpoint.
Request Processing
Section titled “Request Processing”You can customize the behavior of the user endpoint by implementing the ProcessRequestAsync method of the
IUserEndpoint interface. The default implementation can serve as a starting point for your own implementation.
If you want to extend the default behavior of the user endpoint, you can instead add a custom endpoint and call the original endpoint implementation:
var bffOptions = app.Services.GetRequiredService<IOptions<BffOptions>>().Value;
app.MapGet(bffOptions.UserPath, async (HttpContext context, CancellationToken ct) =>{ // Custom logic before calling the original endpoint implementation var endpointProcessor = context.RequestServices.GetRequiredService<IUserEndpoint>(); await endpointProcessor.ProcessRequestAsync(context, ct); // Custom logic after calling the original endpoint implementation});ProcessRequestAsync is the top-level function called in the endpoint service DefaultUserService,
and can be used to add arbitrary logic to the endpoint.
For example, you could take whatever actions you need before normal processing of the request like this:
public override Task ProcessRequestAsync(HttpContext context, CancellationToken ct){ // Custom logic here
return base.ProcessRequestAsync(context);}Enriching User Claims
Section titled “Enriching User Claims”There are several ways how you can enrich the claims for a specific user.
The most robust way would be to implement a custom IClaimsTransformation.
services.AddScoped<IClaimsTransformation, CustomClaimsTransformer>();
public class CustomClaimsTransformer : IClaimsTransformation{ public Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal) { var identity = (ClaimsIdentity)principal.Identity;
if (!identity.HasClaim(c => c.Type == "custom_claim")) { identity.AddClaim(new Claim("custom_claim", "your_value")); }
return Task.FromResult(principal); }}See the Claims Transformation topic in the ASP.NET Core documentation for more information.