gpt4 book ai didi

c# - 在 HttpClient 中将多个 token 添加到 DefaultRequestHeaders.Authorization

转载 作者:太空宇宙 更新时间:2023-11-03 12:16:14 26 4
gpt4 key购买 nike

我从我的 ASP.NET Web API 应用调用的 API 需要两个 token ,即 accessTokenuserToken

以下代码无法正常工作,因为它只需要第二个 token ,而不是两个。看起来第二行覆盖了第一行。

如何向我的请求 header 添加多个 token ?

_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("APIAccessToken", "token1");
_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("UserToken", "token2");

更新:这是我设置它的方式,但它不起作用。基本上,我的 API 调用似乎无处可去。我没有错误。只是没有回应。

首先,我有如下所示的 HttpClientAccessor:

public static class HttpClientAccessor
{
private static Lazy<HttpClient> client = new Lazy<HttpClient>(() => new HttpClient());

public static HttpClient HttpClient
{
get
{
client.Value.BaseAddress = new Uri("https://api.someurl.com");
client.Value.DefaultRequestHeaders.Accept.Clear();
client.Value.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

client.Value.DefaultRequestHeaders.TryAddWithoutValidation("APIAccessToken", "token1");
client.Value.DefaultRequestHeaders.TryAddWithoutValidation("UserToken", "token2");

return client.Value;
}
}
}

然后我的 ApiClient 将执行我的 API 调用,如下所示:

public class MyApiClient
{
HttpClient _client;

public MyApiClient()
{
_client = HttpClientAccessor.HttpClient;
}

public async Task Get()
{
try
{
HttpResponseMessage response = await _client.GetAsync("/myendpoint"); // This is where it gets lost
var data = await response.Content.ReadAsStringAsync();
}
catch(Exception e)
{
var error = e.Message;
}
}
}

这是我的 Controller 操作:

public class MyController : Controller
{
private readonly MyApiClient _client;

public MyController()
{
_client = new MyApiClient();
}

public IActionResult SomeAction()
{
_client.Get().Wait();
}
}

最佳答案

您将标准授权 header 与自定义 header 混淆了

根据链接文档

Request Header

Add the generated tokens to the request headers "APIAccessToken" and "UserToken"

Example Request

APIAccessToken: zjhVgRIvcZItU8sCNjLn+0V56bJR8UOKOTDYeLTa43eQX9eynX90QntWtINDjLaRjAyOPgrWdrGK12xPaOdDZQ==
UserToken: 5sb8Wf94B0g3n4RGOqkBdPfX+wr2pmBTegIK73S3h7uL8EzU6cjsnJ0+B6vt5iqn0q+jkZgN+gMRU4Y5+2AaXw==

要获取上面的标题,请将它们添加到客户端,如下所示

_client.DefaultRequestHeaders.TryAddWithoutValidation("APIAccessToken", "token1");
_client.DefaultRequestHeaders.TryAddWithoutValidation("UserToken", "token2");

根据显示的更新,每次调用客户端时,客户端都会添加 header 。这应该在懒客户端的值工厂中。

public static class HttpClientAccessor {

public static Func<HttpClient> ValueFactory = () => {
var client = new HttpClient();
client.BaseAddress = new Uri("https://someApiUrl");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.TryAddWithoutValidation("APIAccessToken", "token1");
client.DefaultRequestHeaders.TryAddWithoutValidation("UserToken", "token2");
return client;
};

private static Lazy<HttpClient> client = new Lazy<HttpClient>(ValueFactory);

public static HttpClient HttpClient {
get {
return client.Value;
}
}
}

Controller 操作也需要重构以避免死锁,因为混合了异步和阻塞调用,如 .Wait().Result

public class MyController : Controller {
private readonly MyApiClient _client;

public MyController() {
_client = new MyApiClient();
}

public async Task<IActionResult> SomeAction() {
await _client.Get();

//... code removed for brevity
}
}

关于c# - 在 HttpClient 中将多个 token 添加到 DefaultRequestHeaders.Authorization,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49634266/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com