diff --git a/OpenHarbor.JwtTokenManager/JwtTokenManagerOptions.cs b/OpenHarbor.JwtTokenManager/JwtTokenManagerOptions.cs index 3fa5c34..1c18320 100644 --- a/OpenHarbor.JwtTokenManager/JwtTokenManagerOptions.cs +++ b/OpenHarbor.JwtTokenManager/JwtTokenManagerOptions.cs @@ -6,5 +6,6 @@ public class JwtTokenManagerOptions public required string ClientId { get; set; } public required string ClientSecret { get; set; } public IEnumerable Scopes { get; set; } = Array.Empty(); + public bool IsCredentialsInHeader { get; set; } = false; } diff --git a/OpenHarbor.JwtTokenManager/JwtTokenManagerService.cs b/OpenHarbor.JwtTokenManager/JwtTokenManagerService.cs index ceb1f02..f400758 100644 --- a/OpenHarbor.JwtTokenManager/JwtTokenManagerService.cs +++ b/OpenHarbor.JwtTokenManager/JwtTokenManagerService.cs @@ -1,5 +1,6 @@ using System.Net.Http.Headers; using System.Net.Http.Json; +using System.Text; using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Logging; using OpenHarbor.JwtTokenManager.Abstractions; @@ -23,19 +24,33 @@ public class JwtTokenManagerService(JwtTokenManagerOptions options, IHttpClientF return cachedToken; } } - - var formContent = new FormUrlEncodedContent([ - new KeyValuePair("grant_type", "client_credentials"), - new KeyValuePair("client_id", options.ClientId), - new KeyValuePair("client_secret", options.ClientSecret), - new KeyValuePair("scopes", string.Join(" ", _scopes)) - ]); + + var formContentKeyValues = new List>() + { + new ("grant_type", "client_credentials"), + new ("scopes", string.Join(" ", _scopes)) + }; + + if (options.IsCredentialsInHeader) + { + formContentKeyValues.AddRange([ + new KeyValuePair("client_id", options.ClientId), + new KeyValuePair("client_secret", options.ClientSecret) + ]); + } + var formContent = new FormUrlEncodedContent(formContentKeyValues); var request = new HttpRequestMessage(HttpMethod.Post, options.TokenEndpoint) { Content = formContent }; + if (false == options.IsCredentialsInHeader) + { + var credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{options.ClientId}:{options.ClientSecret}")); + request.Headers.Authorization = new AuthenticationHeaderValue("Basic", credentials); + } + request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var response = await _httpClient.SendAsync(request, cancellationToken);