【问题标题】:How to pass auth token to multiple http clients?如何将身份验证令牌传递给多个 http 客户端?
【发布时间】:2020-12-14 15:01:04
【问题描述】:

我正在尝试将相同的身份验证令牌传递给我的第二个 API。这是我的设置示例。我可以在“主机”请求中看到不记名令牌,但我的第二个 api 请求看不到任何内容。为什么?我错过了什么吗?

builder.Services
.AddHttpClient("Host.ServerAPI", client => client.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress))
.AddHttpMessageHandler<BaseAddressAuthorizationMessageHandler>();
            
builder.Services.AddHttpClient<AnotherApiClient>("Another.ServerAPI", x =>
{
    x.BaseAddress = new Uri("https://another.api.com/");
}).AddHttpMessageHandler<BaseAddressAuthorizationMessageHandler>(); 

【问题讨论】:

    标签: blazor webassembly blazor-webassembly


    【解决方案1】:

    搞定了。

    builder.Services.AddHttpClient<AnotherApiClient>("Another.ServerAPI", client =>
    {
         client.BaseAddress = new Uri("https://another.api.com/");
    }).AddHttpMessageHandler(x =>
    {
        var handler = x.GetRequiredService<AuthorizationMessageHandler>()
            .ConfigureHandler(new[] {"https://another.api.com/"},
                scopes: new[] {"anotherapi"});
    
         return handler;
    }); 
    

    【讨论】:

      猜你喜欢
      • 2019-06-16
      • 2021-04-13
      • 2011-08-16
      • 1970-01-01
      • 2021-05-18
      • 2012-09-22
      • 2015-02-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多