MicrosoftIdentityHttpClientBuilderExtensions.AddMicrosoftIdentityMessageHandler Methode
Definitie
Belangrijk
Bepaalde informatie heeft betrekking op een voorlopige productversie die aanzienlijk kan worden gewijzigd voordat deze wordt uitgebracht. Microsoft biedt geen enkele expliciete of impliciete garanties met betrekking tot de informatie die hier wordt verstrekt.
Overloads
| Name | Description |
|---|---|
| AddMicrosoftIdentityMessageHandler(IHttpClientBuilder) |
Voegt een MicrosoftIdentityMessageHandler toe aan de HTTP-clientpijplijn zonder standaardopties. Opties moeten per aanvraag worden geconfigureerd met behulp van WithAuthenticationOptions(HttpRequestMessage, MicrosoftIdentityMessageHandlerOptions). |
| AddMicrosoftIdentityMessageHandler(IHttpClientBuilder, MicrosoftIdentityMessageHandlerOptions) |
Voegt een MicrosoftIdentityMessageHandler toe aan de HTTP-clientpijplijn met de opgegeven opties. |
| AddMicrosoftIdentityMessageHandler(IHttpClientBuilder, Action<MicrosoftIdentityMessageHandlerOptions>) |
Voegt een MicrosoftIdentityMessageHandler toe aan de HTTP-clientpijplijn met opties die zijn geconfigureerd via gemachtigde. |
| AddMicrosoftIdentityMessageHandler(IHttpClientBuilder, IConfiguration, String) |
Voegt een MicrosoftIdentityMessageHandler toe aan de HTTP-clientpijplijn met opties die afhankelijk zijn van IConfiguration. |
AddMicrosoftIdentityMessageHandler(IHttpClientBuilder)
Voegt een MicrosoftIdentityMessageHandler toe aan de HTTP-clientpijplijn zonder standaardopties. Opties moeten per aanvraag worden geconfigureerd met behulp van WithAuthenticationOptions(HttpRequestMessage, MicrosoftIdentityMessageHandlerOptions).
public static Microsoft.Extensions.DependencyInjection.IHttpClientBuilder AddMicrosoftIdentityMessageHandler(this Microsoft.Extensions.DependencyInjection.IHttpClientBuilder builder);
static member AddMicrosoftIdentityMessageHandler : Microsoft.Extensions.DependencyInjection.IHttpClientBuilder -> Microsoft.Extensions.DependencyInjection.IHttpClientBuilder
<Extension()>
Public Function AddMicrosoftIdentityMessageHandler (builder As IHttpClientBuilder) As IHttpClientBuilder
Parameters
- builder
- IHttpClientBuilder
De IHttpClientBuilder te configureren.
Retouren
De IHttpClientBuilder for method chaining.
Uitzonderingen
Gegooid wanneer builder is null.
Voorbeelden
// Configure the HTTP client
services.AddHttpClient("ApiClient")
.AddMicrosoftIdentityMessageHandler();
// Use the client with per-request configuration
public class MyService
{
private readonly HttpClient _httpClient;
public MyService(IHttpClientFactory factory)
{
_httpClient = factory.CreateClient("ApiClient");
}
public async Task<string> GetDataAsync()
{
var request = new HttpRequestMessage(HttpMethod.Get, "/api/data")
.WithAuthenticationOptions(options =>
{
options.Scopes.Add("https://api.example.com/.default");
});
var response = await _httpClient.SendAsync(request);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
}
Opmerkingen
Deze overbelasting is handig wanneer u maximale flexibiliteit nodig hebt om verificatieopties per aanvraag te configureren. Aangezien er geen standaardopties worden opgegeven, moet elke aanvraag verificatieopties bevatten via de WithAuthenticationOptions extensiemethode.
De handler wordt tijdens runtime van de serviceprovider omgezet IAuthorizationHeaderProvider om autorisatieheaders voor uitgaande aanvragen te verkrijgen.
Van toepassing op
AddMicrosoftIdentityMessageHandler(IHttpClientBuilder, MicrosoftIdentityMessageHandlerOptions)
Voegt een MicrosoftIdentityMessageHandler toe aan de HTTP-clientpijplijn met de opgegeven opties.
public static Microsoft.Extensions.DependencyInjection.IHttpClientBuilder AddMicrosoftIdentityMessageHandler(this Microsoft.Extensions.DependencyInjection.IHttpClientBuilder builder, Microsoft.Identity.Web.MicrosoftIdentityMessageHandlerOptions options);
static member AddMicrosoftIdentityMessageHandler : Microsoft.Extensions.DependencyInjection.IHttpClientBuilder * Microsoft.Identity.Web.MicrosoftIdentityMessageHandlerOptions -> Microsoft.Extensions.DependencyInjection.IHttpClientBuilder
<Extension()>
Public Function AddMicrosoftIdentityMessageHandler (builder As IHttpClientBuilder, options As MicrosoftIdentityMessageHandlerOptions) As IHttpClientBuilder
Parameters
- builder
- IHttpClientBuilder
De IHttpClientBuilder te configureren.
De verificatieopties die moeten worden gebruikt voor alle aanvragen van deze client.
Retouren
De IHttpClientBuilder for method chaining.
Uitzonderingen
Gegooid wanneer builder of options is null.
Voorbeelden
// Pre-configure options
var options = new MicrosoftIdentityMessageHandlerOptions
{
Scopes = { "https://graph.microsoft.com/.default" }
};
options.WithAgentIdentity("agent-application-id");
// Configure the HTTP client with the pre-built options
services.AddHttpClient("GraphClient", client =>
{
client.BaseAddress = new Uri("https://graph.microsoft.com");
})
.AddMicrosoftIdentityMessageHandler(options);
// Use the client - authentication is automatic
public class GraphService
{
private readonly HttpClient _httpClient;
public GraphService(IHttpClientFactory factory)
{
_httpClient = factory.CreateClient("GraphClient");
}
public async Task<string> GetUserProfileAsync()
{
var response = await _httpClient.GetAsync("/v1.0/me");
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
}
Opmerkingen
Deze overbelasting is handig wanneer u een vooraf geconfigureerd MicrosoftIdentityMessageHandlerOptions exemplaar hebt dat moet worden gebruikt voor alle aanvragen van deze HTTP-client. Afzonderlijke aanvragen kunnen deze standaardopties nog steeds overschrijven met behulp van de extensiemethoden per aanvraag.
De handler wordt tijdens runtime van de serviceprovider omgezet IAuthorizationHeaderProvider om autorisatieheaders voor uitgaande aanvragen te verkrijgen.
Van toepassing op
AddMicrosoftIdentityMessageHandler(IHttpClientBuilder, Action<MicrosoftIdentityMessageHandlerOptions>)
Voegt een MicrosoftIdentityMessageHandler toe aan de HTTP-clientpijplijn met opties die zijn geconfigureerd via gemachtigde.
public static Microsoft.Extensions.DependencyInjection.IHttpClientBuilder AddMicrosoftIdentityMessageHandler(this Microsoft.Extensions.DependencyInjection.IHttpClientBuilder builder, Action<Microsoft.Identity.Web.MicrosoftIdentityMessageHandlerOptions> configureOptions);
static member AddMicrosoftIdentityMessageHandler : Microsoft.Extensions.DependencyInjection.IHttpClientBuilder * Action<Microsoft.Identity.Web.MicrosoftIdentityMessageHandlerOptions> -> Microsoft.Extensions.DependencyInjection.IHttpClientBuilder
<Extension()>
Public Function AddMicrosoftIdentityMessageHandler (builder As IHttpClientBuilder, configureOptions As Action(Of MicrosoftIdentityMessageHandlerOptions)) As IHttpClientBuilder
Parameters
- builder
- IHttpClientBuilder
De IHttpClientBuilder te configureren.
- configureOptions
- Action<MicrosoftIdentityMessageHandlerOptions>
Een gemachtigde voor het configureren van de verificatieopties.
Retouren
De IHttpClientBuilder for method chaining.
Uitzonderingen
Gegooid wanneer builder of configureOptions is null.
Voorbeelden
// Configure the HTTP client with inline options configuration
services.AddHttpClient("MyApiClient", client =>
{
client.BaseAddress = new Uri("https://api.example.com");
})
.AddMicrosoftIdentityMessageHandler(options =>
{
options.Scopes.Add("https://api.example.com/.default");
options.RequestAppToken = true;
});
// Use the client - authentication is automatic
public class ApiService
{
private readonly HttpClient _httpClient;
public ApiService(IHttpClientFactory factory)
{
_httpClient = factory.CreateClient("MyApiClient");
}
public async Task<string> GetDataAsync()
{
var response = await _httpClient.GetAsync("/api/data");
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
}
Met agentidentiteit:
services.AddHttpClient("AgentClient")
.AddMicrosoftIdentityMessageHandler(options =>
{
options.Scopes.Add("https://graph.microsoft.com/.default");
options.WithAgentIdentity("agent-application-id");
options.RequestAppToken = true;
});
Opmerkingen
Deze overbelasting is handig voor inlineconfiguratie van verificatieopties. De gemachtigde wordt eenmaal aangeroepen tijdens de serviceconfiguratie om de standaardopties voor de HTTP-client te maken. Afzonderlijke aanvragen kunnen deze standaardopties nog steeds overschrijven met behulp van de extensiemethoden per aanvraag.
De handler wordt tijdens runtime van de serviceprovider omgezet IAuthorizationHeaderProvider om autorisatieheaders voor uitgaande aanvragen te verkrijgen.
Van toepassing op
AddMicrosoftIdentityMessageHandler(IHttpClientBuilder, IConfiguration, String)
Voegt een MicrosoftIdentityMessageHandler toe aan de HTTP-clientpijplijn met opties die afhankelijk zijn van IConfiguration.
public static Microsoft.Extensions.DependencyInjection.IHttpClientBuilder AddMicrosoftIdentityMessageHandler(this Microsoft.Extensions.DependencyInjection.IHttpClientBuilder builder, Microsoft.Extensions.Configuration.IConfiguration configuration, string sectionName);
static member AddMicrosoftIdentityMessageHandler : Microsoft.Extensions.DependencyInjection.IHttpClientBuilder * Microsoft.Extensions.Configuration.IConfiguration * string -> Microsoft.Extensions.DependencyInjection.IHttpClientBuilder
<Extension()>
Public Function AddMicrosoftIdentityMessageHandler (builder As IHttpClientBuilder, configuration As IConfiguration, sectionName As String) As IHttpClientBuilder
Parameters
- builder
- IHttpClientBuilder
De IHttpClientBuilder te configureren.
- configuration
- IConfiguration
De configuratiesectie met de verificatieopties.
- sectionName
- String
De naam van de configuratiesectie (gebruikt voor diagnostische gegevens).
Retouren
De IHttpClientBuilder for method chaining.
Uitzonderingen
Gegenereerd wanneer builder, configurationof sectionName is null.
Voorbeelden
Configuratie in appsettings.json:
{
"DownstreamApi": {
"Scopes": ["https://api.example.com/.default"]
},
"GraphApi": {
"Scopes": ["https://graph.microsoft.com/.default", "User.Read"]
}
}
De HTTP-client configureren:
// In Program.cs or Startup.cs
services.AddHttpClient("DownstreamApiClient", client =>
{
client.BaseAddress = new Uri("https://api.example.com");
})
.AddMicrosoftIdentityMessageHandler(
configuration.GetSection("DownstreamApi"),
"DownstreamApi");
services.AddHttpClient("GraphClient", client =>
{
client.BaseAddress = new Uri("https://graph.microsoft.com");
})
.AddMicrosoftIdentityMessageHandler(
configuration.GetSection("GraphApi"),
"GraphApi");
Gebruik de clients:
public class MyService
{
private readonly HttpClient _apiClient;
private readonly HttpClient _graphClient;
public MyService(IHttpClientFactory factory)
{
_apiClient = factory.CreateClient("DownstreamApiClient");
_graphClient = factory.CreateClient("GraphClient");
}
public async Task<string> GetApiDataAsync()
{
var response = await _apiClient.GetAsync("/api/data");
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
public async Task<string> GetUserProfileAsync()
{
var response = await _graphClient.GetAsync("/v1.0/me");
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
}
Opmerkingen
Deze overbelasting is handig als u verificatieopties van appsettings.json of andere configuratiebronnen wilt configureren. De configuratiesectie is gebonden aan een nieuw MicrosoftIdentityMessageHandlerOptions exemplaar met behulp van standaardconfiguratiebinding. Afzonderlijke aanvragen kunnen deze standaardopties nog steeds overschrijven met behulp van de extensiemethoden per aanvraag.
De handler wordt tijdens runtime van de serviceprovider omgezet IAuthorizationHeaderProvider om autorisatieheaders voor uitgaande aanvragen te verkrijgen.