Giter Club home page Giter Club logo

Comments (2)

jsquire avatar jsquire commented on June 30, 2024

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.

github-actions avatar github-actions commented on June 30, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.