minor optimization to reuse the HttpClient and added helper function
This commit is contained in:
@@ -32,31 +32,29 @@ public class JwtTokenManagerService(JwtTokenManagerOptions options, IHttpClientF
|
||||
|
||||
var client = httpClientFactory.CreateClient();
|
||||
|
||||
var formContent = new FormUrlEncodedContent(new[]
|
||||
{
|
||||
var formContent = new FormUrlEncodedContent([
|
||||
new KeyValuePair<string, string>("grant_type", "client_credentials"),
|
||||
new KeyValuePair<string, string>("client_id", options.ClientId),
|
||||
new KeyValuePair<string, string>("client_secret", options.ClientSecret),
|
||||
new KeyValuePair<string, string>("scopes", string.Join(" ", options.Scopes))
|
||||
});
|
||||
new KeyValuePair<string, string>("scopes", string.Join(" ", _scopes))
|
||||
]);
|
||||
|
||||
var request = new HttpRequestMessage(HttpMethod.Post, options.TokenEndpoint)
|
||||
{
|
||||
Content = formContent
|
||||
};
|
||||
|
||||
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
||||
|
||||
var response = await client.SendAsync(request, cancellationToken);
|
||||
|
||||
var t = await response.Content.ReadAsStringAsync(cancellationToken);
|
||||
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
var tokenResponse = await response.Content.ReadFromJsonAsync<JwtTokenResponse>(SnakeCaseOptions, cancellationToken);
|
||||
|
||||
if (tokenResponse == null)
|
||||
{
|
||||
throw new InvalidOperationException("Failed to deserialize the response content.");
|
||||
throw new InvalidOperationException("Failed to deserialize the token response content.");
|
||||
}
|
||||
|
||||
var parsedResult = Enum.TryParse<TokenType>(tokenResponse.TokenType, out var tokenType);
|
||||
|
||||
@@ -6,6 +6,10 @@
|
||||
<Nullable>enable</Nullable>
|
||||
<Authors>Mathias Beaulieu-Duncan</Authors>
|
||||
<PackageIconUrl>https://gravatar.com/avatar/9cecda5822fc5d4d2e61ec03da571b3d?size=256</PackageIconUrl>
|
||||
<RepositoryUrl>https://git.openharbor.io/Open-Harbor/dotnet-jwt-token-manager</RepositoryUrl>
|
||||
<RepositoryType>git</RepositoryType>
|
||||
<PublishRepositoryUrl>true</PublishRepositoryUrl>
|
||||
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
Reference in New Issue
Block a user