Browse Source

Using `IHttpClientFactory` to create `HttpClient`s

This is a follow up-PR to #2937, #2953 and #2962 because in the original PR (#2937) there was a missing `context.Services.AddHttpClient()` inside the module - which is now part of the module in the `dev` branch.
I haven't used named clients because currently there won't be any benefit from my point of view since the `HttpClient` only gets used to fire the `IdentityModel` requests using extension methods provided by the `IdentityModel` package.
pull/2964/head
Necati Meral 7 years ago
parent
commit
60c2dfd779
  1. 1
      framework/src/Volo.Abp.IdentityModel/Volo.Abp.IdentityModel.csproj
  2. 21
      framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityModelAuthenticationService.cs

1
framework/src/Volo.Abp.IdentityModel/Volo.Abp.IdentityModel.csproj

@ -16,6 +16,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="IdentityModel" Version="4.1.1" /> <PackageReference Include="IdentityModel" Version="4.1.1" />
<PackageReference Include="Microsoft.Extensions.Http" Version="3.1.2" />
<ProjectReference Include="..\Volo.Abp.Threading\Volo.Abp.Threading.csproj" /> <ProjectReference Include="..\Volo.Abp.Threading\Volo.Abp.Threading.csproj" />
</ItemGroup> </ItemGroup>

21
framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityModelAuthenticationService.cs

@ -21,13 +21,16 @@ namespace Volo.Abp.IdentityModel
public ILogger<IdentityModelAuthenticationService> Logger { get; set; } public ILogger<IdentityModelAuthenticationService> Logger { get; set; }
protected AbpIdentityClientOptions ClientOptions { get; } protected AbpIdentityClientOptions ClientOptions { get; }
protected ICancellationTokenProvider CancellationTokenProvider { get; } protected ICancellationTokenProvider CancellationTokenProvider { get; }
protected IHttpClientFactory HttpClientFactory { get; }
public IdentityModelAuthenticationService( public IdentityModelAuthenticationService(
IOptions<AbpIdentityClientOptions> options, IOptions<AbpIdentityClientOptions> options,
ICancellationTokenProvider cancellationTokenProvider) ICancellationTokenProvider cancellationTokenProvider,
IHttpClientFactory httpClientFactory)
{ {
CancellationTokenProvider = cancellationTokenProvider;
ClientOptions = options.Value; ClientOptions = options.Value;
CancellationTokenProvider = cancellationTokenProvider;
HttpClientFactory = httpClientFactory;
Logger = NullLogger<IdentityModelAuthenticationService>.Instance; Logger = NullLogger<IdentityModelAuthenticationService>.Instance;
} }
@ -95,7 +98,7 @@ namespace Volo.Abp.IdentityModel
protected virtual async Task<DiscoveryDocumentResponse> GetDiscoveryResponse( protected virtual async Task<DiscoveryDocumentResponse> GetDiscoveryResponse(
IdentityClientConfiguration configuration) IdentityClientConfiguration configuration)
{ {
using (var httpClient = new HttpClient()) using (var httpClient = HttpClientFactory.CreateClient())
{ {
return await httpClient.GetDiscoveryDocumentAsync(new DiscoveryDocumentRequest return await httpClient.GetDiscoveryDocumentAsync(new DiscoveryDocumentRequest
{ {
@ -109,10 +112,10 @@ namespace Volo.Abp.IdentityModel
} }
protected virtual async Task<TokenResponse> GetTokenResponse( protected virtual async Task<TokenResponse> GetTokenResponse(
DiscoveryDocumentResponse discoveryResponse, DiscoveryDocumentResponse discoveryResponse,
IdentityClientConfiguration configuration) IdentityClientConfiguration configuration)
{ {
using (var httpClient = new HttpClient()) using (var httpClient = HttpClientFactory.CreateClient())
{ {
switch (configuration.GrantType) switch (configuration.GrantType)
{ {
@ -134,7 +137,7 @@ namespace Volo.Abp.IdentityModel
protected virtual Task<PasswordTokenRequest> CreatePasswordTokenRequestAsync(DiscoveryDocumentResponse discoveryResponse, IdentityClientConfiguration configuration) protected virtual Task<PasswordTokenRequest> CreatePasswordTokenRequestAsync(DiscoveryDocumentResponse discoveryResponse, IdentityClientConfiguration configuration)
{ {
var request = new PasswordTokenRequest var request = new PasswordTokenRequest
{ {
Address = discoveryResponse.TokenEndpoint, Address = discoveryResponse.TokenEndpoint,
Scope = configuration.Scope, Scope = configuration.Scope,
@ -149,11 +152,11 @@ namespace Volo.Abp.IdentityModel
return Task.FromResult(request); return Task.FromResult(request);
} }
protected virtual Task<ClientCredentialsTokenRequest> CreateClientCredentialsTokenRequestAsync( protected virtual Task<ClientCredentialsTokenRequest> CreateClientCredentialsTokenRequestAsync(
DiscoveryDocumentResponse discoveryResponse, DiscoveryDocumentResponse discoveryResponse,
IdentityClientConfiguration configuration) IdentityClientConfiguration configuration)
{ {
var request = new ClientCredentialsTokenRequest var request = new ClientCredentialsTokenRequest
{ {
Address = discoveryResponse.TokenEndpoint, Address = discoveryResponse.TokenEndpoint,
Scope = configuration.Scope, Scope = configuration.Scope,

Loading…
Cancel
Save