gpt4 book ai didi

api - 如何使用 AutoRest 客户端传递客户端证书

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

我们使用 AutoRest 基于 API Swagger 文件生成客户端代码。

我正在尝试将客户端证书传递给 API。但注意到生成的客户端代码不接受 WebRequestHandler。

生成的代码如下所示:

public MyTestApiV1(Uri baseUri, params DelegatingHandler[] handlers) : this(handlers)
{
if (baseUri == null)
{
throw new ArgumentNullException("baseUri");
}
this.BaseUri = baseUri;
}

我觉得我在这里遗漏了什么。有没有人设法使用 AutoRest 发送客户端证书?

试过了但是 webRequestHandler 总是空的:

var webRequestHandler = client.HttpMessageHandlers.First() as WebRequestHandler;
if (webRequestHandler != null)
{
var secretRetrieved = keyVault.GetSecretAsync("my-cert");
var pfxBytes = Convert.FromBase64String(secretRetrieved.Result);
// or recreate the certificate directly
var certificate = new X509Certificate2(pfxBytes);
webRequestHandler.ClientCertificates.Add(certificate);
}

最佳答案

您可以使用另一个重载的构造函数:

/// <summary>
/// Initializes ServiceClient using base HttpClientHandler and list of handlers.
/// </summary>
/// <param name="rootHandler">Base HttpClientHandler.</param>
/// <param name="handlers">List of handlers from top to bottom (outer handler is the first in the list)</param>
protected ServiceClient(HttpClientHandler rootHandler, params DelegatingHandler[] handlers)

ServiceClient 是生成客户端的基类。因此,代码可能如下所示:

var secretRetrieved = keyVault.GetSecretAsync("my-cert");
var pfxBytes = Convert.FromBase64String(secretRetrieved.Result);
// or recreate the certificate directly
var certificate = new X509Certificate2(pfxBytes);
WebRequestHandler webRequestHandler = new WebRequestHandler();
webRequestHandler.ClientCertificates.Add(certificate);
var client = new MyTestApiV1(webRequestHandler);
client.BaseUri = baseUri;

关于api - 如何使用 AutoRest 客户端传递客户端证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43795845/

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