Esempio n. 1
0
        private static async Task ObtainTokenAndSetOnClient(IFhirClient fhirClient, List <KeyValuePair <string, string> > formData, CancellationToken cancellationToken)
        {
            using var formContent = new FormUrlEncodedContent(formData);
            using HttpResponseMessage tokenResponse = await fhirClient.HttpClient.PostAsync(fhirClient.TokenUri, formContent, cancellationToken);

            var openIdConnectMessage = new OpenIdConnectMessage(await tokenResponse.Content.ReadAsStringAsync());

            fhirClient.SetBearerToken(openIdConnectMessage.AccessToken);
        }
Esempio n. 2
0
        /// <summary>
        /// Sets the authenticated token on the <see cref="IFhirClient"/> to the supplied resource via Managed Identity.
        /// </summary>
        /// <param name="fhirClient">The <see cref="IFhirClient"/> to authenticate.</param>
        /// <param name="resource">The resource to obtain a token to.</param>
        /// <param name="tenantId">The optional tenantId to use when requesting a token.</param>
        /// <param name="cancellationToken">The <see cref="CancellationToken"/> for the request.</param>
        /// <returns>A <see cref="Task"/> representing the successful setting of the token.</returns>
        public static async Task AuthenticateWithManagedIdentity(this IFhirClient fhirClient, string resource, string tenantId = null, CancellationToken cancellationToken = default)
        {
            EnsureArg.IsNotNull(fhirClient, nameof(fhirClient));
            EnsureArg.IsNotNullOrWhiteSpace(resource, nameof(resource));

            var    azureServiceTokenProvider = new AzureServiceTokenProvider();
            string accessToken = await azureServiceTokenProvider.GetAccessTokenAsync(resource, tenantId, cancellationToken);

            fhirClient.SetBearerToken(accessToken);
        }