Language

AgentHostExtensions.AddAgentAuthorization Method

Definition

Configures token validation for inbound requests. The provided action should register authentication services (e.g., call AddAgentAspNetAuthentication). When enabled, endpoints mapped by MapDefaultAgentEndpoints(IEndpointRouteBuilder, String) will automatically require authorization.

public static Microsoft.Extensions.Hosting.IHostApplicationBuilder AddAgentAuthorization(this Microsoft.Extensions.Hosting.IHostApplicationBuilder builder, Action<Microsoft.Extensions.Hosting.IHostApplicationBuilder> configure, bool? forceEnable = default);
static member AddAgentAuthorization : Microsoft.Extensions.Hosting.IHostApplicationBuilder * Action<Microsoft.Extensions.Hosting.IHostApplicationBuilder> * Nullable<bool> -> Microsoft.Extensions.Hosting.IHostApplicationBuilder
<Extension()>
Public Function AddAgentAuthorization (builder As IHostApplicationBuilder, configure As Action(Of IHostApplicationBuilder), Optional forceEnable As Nullable(Of Boolean) = Nothing) As IHostApplicationBuilder

Parameters

builder
IHostApplicationBuilder

The host application builder.

configure
Action<IHostApplicationBuilder>

Action that configures authentication on the builder. The sample-provided AddAgentAspNetAuthentication is the default implementation, but developers may use MISE or any other ASP.NET Core authentication mechanism.

forceEnable
Nullable<Boolean>

Override for whether authorization is enabled. When null (the default), authorization is enabled in all environments except Development. Pass an explicit value to override — for example: forceEnable: !builder.Environment.IsDevelopment() && !builder.Environment.IsEnvironment("Staging").

Returns

The same builder for chaining.

Remarks

When forceEnable resolves to true, the configure action is invoked and a marker service is registered in DI. MapDefaultAgentEndpoints(IEndpointRouteBuilder, String) checks for this marker to decide whether to call RequireAuthorization() on agent endpoints.

If this method is not called or forceEnable resolves to false, no authentication is configured and endpoints will allow anonymous access.

To configure authentication manually without this method, register your authentication services directly on builder.Services and use MapAgentApplicationEndpoints(IEndpointRouteBuilder, Boolean, String) with requireAuth: true.

Applies to