为了账号安全,请及时绑定邮箱和手机立即绑定

通过代理使用 KeyVaultClient

通过代理使用 KeyVaultClient

C#
大话西游666 2023-09-16 14:58:26
目前,我在启动期间使用 Azure KeyVault 来加载一些配置,如下所示:configBuilder    .AddAzureKeyVault(keyVaultConfigSection.Vault, GetKeyVaultClient(clientConfigSection, keyVaultConfigSection), new DefaultKeyVaultSecretManager())    .AddEnvironmentVariables();private static KeyVaultClient GetKeyVaultClient(ClientConfigSection clientConfigSection, KeyVaultConfigSection keyVaultConfigSection){    HttpClient httpClient = null;    //proxy    if (!CustomEnvironment.NotProductionEnvironment())    {        var handler = new HttpClientHandler()        {            Proxy = new WebProxy(keyVaultConfigSection.Proxy),            UseProxy = true        };        httpClient = new HttpClient(handler);    }    return new KeyVaultClient(async (authority, resource, scope) =>        {            var authContext = new AuthenticationContext(authority);            var clientCred = new ClientCredential(clientConfigSection.ClientId, clientConfigSection.ClientSecret);            var result = await authContext.AcquireTokenAsync(resource, clientCred);            if (result == null)                throw new InvalidOperationException("Failed to retrieve access token for Key Vault");            return result.AccessToken;        }, httpClient ?? new HttpClient()    );}当我不在生产环境中时,这工作得很好。但在我们的生产环境中,keyvault 被阻止,因此我们必须通过代理。但是当运行代码时我得到这个错误:Microsoft.Azure.KeyVault.Models.KeyVaultErrorException: 'Operation returned an invalid status code 'BadRequest''以前有人这样做过并且可以指出我正确的方向吗?
查看完整描述

1 回答

?
慕的地10843

TA贡献1785条经验 获得超8个赞

似乎还没有修复,解决方法如下。

1.引用System.Net.Http.WinHttpHandlerNuget包来访问.NET Core中的WinHttpHandler。

2.创建一个新的MyKeyVaultCredential,继承自KeyVaultCredential并重写ProcessHttpRequestAsync方法

public override async Task ProcessHttpRequestAsync(HttpRequestMessage request, CancellationToken cancellationToken)

 {

     if (request == null)

     {

         throw new ArgumentNullException("request");

     }


     var accessToken = await PreAuthenticate(request.RequestUri).ConfigureAwait(false);

     if (!string.IsNullOrEmpty(accessToken))

         request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

     else

     {

         var httpClientHandler = new WinHttpHandler()

         {

             WindowsProxyUsePolicy = WindowsProxyUsePolicy.UseCustomProxy,

             Proxy = new MyWebProxy(configuration),

             SendTimeout = TimeSpan.FromSeconds(120),

             ReceiveDataTimeout = TimeSpan.FromSeconds(120),

             ReceiveHeadersTimeout = TimeSpan.FromSeconds(120),

         };

3.当我实例化 KeyVaultService 时,我必须向 WinHttpHandler 提供我的代理和新的密钥保管库凭据实例。


var httpClientHandler = new WinHttpHandler()

     {

         WindowsProxyUsePolicy = WindowsProxyUsePolicy.UseCustomProxy,

         Proxy = new MyWebProxy(configuration),

         SendTimeout = TimeSpan.FromSeconds(120),

         ReceiveDataTimeout = TimeSpan.FromSeconds(120),

         ReceiveHeadersTimeout= TimeSpan.FromSeconds(120),

     };


     var httpClient = new HttpClient(httpClientHandler);


     client = new KeyVaultClient(new  MyKeyVaultCredential(configuration, GetToken), httpClient)

希望这可以帮助。


查看完整回答
反对 回复 2023-09-16
  • 1 回答
  • 0 关注
  • 66 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信