Comments (2)
Hi @schmallaria. Thanks for reaching out and for your inquiry. While you could create a client factory that would allow you to register an HttpClient
via the AddAzureClients
extension, there's no benefit over using the standard DI registration path, as it will not achieve your goal.
The Azure clients use a well-known pattern for authentication via Azure.Identity credentials and Azure.Core's configuration options which the AddAzureClients
uses to configure and inject credentials. The HttpClient
type does not support those constructs nor the Azure SDK patterns. You would be better off configuring/registering your HttpClient
with the standard DI path and injecting a TokenCredential
into wherever you're using HttpClient
and then manually requesting the token from it.
Something like:
// Assumption "MyClass" is created by DI, which injects the registered client and credential.
public class MyClass
{
private HttpClient _httpClient;
priivate TokenCredential _credential;
public MyClass(HttpClient httpClient, TokenCredential credential) => (_ httpClient, _credential) = (httpClient, credential)
public async Task DoTheThing()
{
var bearerToken = await _credential.GetTokenAsync(
new TokenRequestContext(new[] { "http://some.service.com/.default" }), CancellationToken.None);
var message = new HttpRequestMessage(HttpMethod.Get, new Uri("https://www.google.com"));
message.Headers.Authorization = new AuthenticationHeaderValue("Bearer", bearerToken.Token);
await _httpClient.SendAsync(message);
}
}
from azure-sdk-for-net.
Hi @schmallaria. Thank you for opening this issue and giving us the opportunity to assist. We believe that this has been addressed. If you feel that further discussion is needed, please add a comment with the text "/unresolve" to remove the "issue-addressed" label and continue the conversation.
from azure-sdk-for-net.
Related Issues (20)
- QueueClient.SendMessage - Retry failed after 6 tries HOT 1
- Unable to load the proper Managed Identity HOT 2
- [FEATURE REQ] ServiceBusReceivedMessage: include QueueProperties HOT 2
- [BUG] Broken link in monitor readme
- Upgrade Azure.ResourceManager.EventHubs to latest api version
- [FormRecognizer] Deploy test resources failed in nightly pipeline.
- Upgrade Azure.ResourceManager.AppService to latest api version
- [QUERY] Does Azure.ResourceManager.CosmosDB support Cosmos DB Emulator? HOT 2
- [BUG] When saving custom events to Application Insights, Local Date Time information gets lost (it gets converted to UTC) HOT 1
- ClientModel: Migrate stable types from Azure.Core Shared source to public types
- Assistants playground (Preview) - Oops, something went wrong. HOT 1
- Cleanup of warnings in the extensions project HOT 1
- Allow the use of a client assertion in OnBehalfOfCredential HOT 2
- ClientModel: Support extending model types with additional properties in separate assemblies HOT 2
- Confidential Ledger README Issue HOT 1
- EventGrid Namespaces README Issue HOT 1
- [BUG] When working with Semantic Kernel, while invoking ChatCompletion API, unable to specify data sources HOT 3
- House or house? HOT 1
- Incomplete information. HOT 4
- [OpenAI] The `imageCount` parameter of the `ImageClient` methods should not be defaulted
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from azure-sdk-for-net.