Skip to content

Any complete on how to use AuthorizationHeaderValueGetter #573

Closed

Description

@ericontilt

I am trying to get the AuthorizationHeaderValueGetter to work, but no matter what I do, I can't get the authorization header set correctly.

In my container configuration I'm setting up the API in the following way:

_services
    .AddRefitClient<IMyApi>(
        new RefitSettings
        {
            AuthorizationHeaderValueGetter = () => Task.FromResult("token")
        })
    .ConfigureHttpClient(c => c.BaseAddress = new Uri("http://my.api.com"));

My API looks like this:

public interface IMyApi
{
    [Get("/SomeEndpoint/{id}")]
    [Headers("Authorization: Basic")]
    Task<ResultObj> GetStuff(int id);
}

I first tried injecting IMyApi into one of my service classes, but the header is not extenden with the token (nor is the Func passed to the settings invoked). I also tried directly gettings the service using

RestService.For<IMyApi>(new RefitSettings { AuthorizationHeaderValueGetter = ... })

but also to no avail.

Can anyone provide a working example using the ASP.NET Core DI container with the Refit.HttpClientFactory and a dynamic Authorization header?

Activity

  1. 19balazs86 commented on Jan 11, 2019

    @19balazs86

    Same problem :(
    The AuthorizationHeaderValueGetter is not called by the library, if I use the IServiceCollection.AddRefitClient.

    RestService.For works fine.

  2. ahmedalejo commented on Jan 22, 2019

    @ahmedalejo

    Using the following will work: RestService.For<T>(string hostUrl, RefitSettings settings) while others will not

  3. 19balazs86 commented on Jan 22, 2019

    @19balazs86

    services.AddRefitClient<ISomething>() .ConfigureHttpClient(c => c.BaseAddress = new Uri("...")) .AddHttpMessageHandler<AuthorizationMessageHandler>();

    For me this solution works well.

  4. added a commit that references this issue on Jun 5, 2019
  5. natelaff commented on Jul 15, 2019

    @natelaff

    Even with 4.7.9 I can't get this to work in ASP.NET Core. The docs on this topic are just about useless. Anyone getting this to actually work?

  6. ericontilt commented on Jul 15, 2019

    @ericontilt
    Author

    Even with 4.7.9 I can't get this to work in ASP.NET Core. The docs on this topic are just about useless. Anyone getting this to actually work?

    Thanks to the suggestions above, I did get it to work without problems. I would submit the example, but I don't have access to the code within the next two weeks.

  7. 19balazs86 commented on Jul 15, 2019

    @19balazs86

    Thanks in advance. (I still can use the solution, which I wrote until the official one)

  8. breenbob commented on Jul 15, 2019

    @breenbob

    I also can't get this to work in 4.7.9 when using in conjunction with Refit.HttpClientFactory, it only works when using the regular refit RestService.For<T>(string hostUrl, RefitSettings settings) as commented previously. As a workaround I was passing Authorization as parameter individually to each API method call, but this is a pain. Not sure about method above for calling AddHttpMessageHandler<AuthorizationMessageHandler>(); as AuthorizationMessageHandler doesn't appear to be something that is OOTB? Closest match is a handler which is internal to Refit assembly.

  9. natelaff commented on Jul 15, 2019

    @natelaff

    Well I'm glad that I'm not the only one that can't make this work as intended, as well as not being able to make sense of the docs. Rather than show a sample they show classes that one of which is internal. So for hours I'm looking at this like "do I copy and paste this class into my project?"

    I will use the proposed work around, but it seems like this issue should be reopened as the commit didn't seem to actually fix this, unless we're all just using it wrong?

    Edit: Hmm.. RestService.For doesn't seem to work with HttpClientFactor, unless I'm missing something there, you can't configure the client. Meanwhile the other work around using
    services.AddRefitClient() .ConfigureHttpClient(c => c.BaseAddress = new Uri("...")) .AddHttpMessageHandler(); doesn't work because of the internal class. Or are you just pulling this one in manually?

  10. 19balazs86 commented on Jul 15, 2019

    @19balazs86

    Hi All,
    Here is the Startup class, where I configured Refit with HttpMessageHandler.
    The repository is meant to work with the Refit library (4.7.9).
    For me, it works and I can use/accept this workaround.
    I hope it helps you.

  11. natelaff commented on Jul 15, 2019

    @natelaff

    Ah thanks, you're just pulling that whole handler in. Which honestly is probably what I need to do anyways since I don't think I can access the current httpcontext, which is where my access token is, directly in that func anyways. Meanwhile, if anyone has any example of just integrating this with Auth0 that would be super rad ;)

  12. locked and limited conversation to collaborators on Oct 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions