private static async Task ObtainTokenAndSetOnClient(FhirClient fhirClient, List <KeyValuePair <string, string> > formData) { using var formContent = new FormUrlEncodedContent(formData); using HttpResponseMessage tokenResponse = await fhirClient.HttpClient.PostAsync(fhirClient.TokenUri, formContent); var openIdConnectMessage = new OpenIdConnectMessage(await tokenResponse.Content.ReadAsStringAsync()); fhirClient.SetBearerToken(openIdConnectMessage.AccessToken); }
/// <summary> /// Sets the authenticated token on the FhirClient to the supplied resource via Managed Identity. /// </summary> /// <param name="fhirClient">The FhirClient to authenticate.</param> /// <param name="resource">The resource to obtain a token to.</param> /// <returns>A task representing the successful setting of the token.</returns> public static async Task AuthenticateWithManagedIdentity(this FhirClient fhirClient, string resource) { EnsureArg.IsNotNull(fhirClient, nameof(fhirClient)); EnsureArg.IsNotNullOrWhiteSpace(resource, nameof(resource)); var azureServiceTokenProvider = new AzureServiceTokenProvider(); string accessToken = await azureServiceTokenProvider.GetAccessTokenAsync(resource); fhirClient.SetBearerToken(accessToken); }