.NET Core 3.1 中的默认代理 [英] Default Proxy in .NET Core 3.1

查看:24
本文介绍了.NET Core 3.1 中的默认代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些有关 .NET Core 3.1 的帮助,以获取需要脱离公司代理的代码.通过将以下内容放在 app.config 中,代码在 .NET 4.7.2 中工作.这个,我在这个网站上学的(谢谢!),可以通过公司代理服务器.

I need some help with .NET Core 3.1 for code that needs to get out of the corporate proxy. The code works in .NET 4.7.2 by putting the following in the app.config. This, I learned on this site (Thank you!), allows one to get through the corporate proxy server.

 <system.net>
    <defaultProxy useDefaultCredentials="true">
    </defaultProxy>
  </system.net>

以下代码片段适用于 .NET 4.7.2,并且可以通过代理退出.代理存储为名为 ALL_PROXY 的环境变量,其值为 http://我们的内部代理:端口(参见 [https://github.com/dotnet/corefx/pull/37238/commits/9ba8879ea104afac9dea9a78d3009b5bc700b7c3][1]).这是一项 Azure 认知服务,因此你需要一个密钥保管库和一项认知服务.

The following code snippet works in .NET 4.7.2, and can get out past the proxy. The proxy is stored as an env variable named ALL_PROXY with a value of http:// our internal proxy:port (see [https://github.com/dotnet/corefx/pull/37238/commits/9ba8879ea104afac9dea9a78d3009b5bc700b7c3][1]). This is an Azure Cognitive service, so you will need a key vault, and a Cognitive Service.

  var keyVaultName = Environment.GetEnvironmentVariable("KEY_VAULT_NAME");
  var kvUri = string.Format("{0}{1}{2}", 
                "https://", 
                keyVaultName, 
                ".vault.azure.net");
  var secretClient = new SecretClient(new Uri(kvUri), 
                new DefaultAzureCredential());
  KeyVaultSecret secret = secretClient.GetSecret("FacesSubscription");
  var subscriptionKey = secret.Value;

  var credentials = new ApiKeyServiceClientCredentials(subscriptionKey);
  var textClient = new TextAnalyticsClient(credentials)
  {
      Endpoint = endpoint
   };

在 .Net 核心上,我收到一个 http 407 异常.Azure.RequestFailedException: '服务请求失败.状态:407(ADAuth-AuthenticationFailed)此异常发生在 KeyVaultSecret 行上.

On .Net core, I get an http 407 exception. Azure.RequestFailedException: 'Service request failed. Status: 407 (ADAuth-AuthenticationFailed) This exception occurs on the KeyVaultSecret line.

我已经研究过这个问题@

I have researched the issue @

  • [https://github.com/dotnet/corefx/pull/37238/commits/9ba8879ea104afac9dea9a78d3009b5bc700b7c3][1]

  • [https://github.com/dotnet/corefx/pull/37238/commits/9ba8879ea104afac9dea9a78d3009b5bc700b7c3][1]

https://github.com/dotnet/corefx/pull/37238

https://github.com/dotnet/corefx/pull/37333

https://github.com/dotnet/runtime/issues/29147

并尝试添加诸如

var handler = new HttpClientHandler();
handler.DefaultProxyCredentials = CredentialCache.DefaultCredentials;
var httpClient = new HttpClient(handler);

我查看了签入的单元测试,但它们似乎都与网络代理有关.以上链接提供了有关如何实施代理的详细信息.

I looked through the unit tests for the check-ins, but they all seemed to be about web proxies. The links above provide details of how the proxy is implemented.

有没有人知道如何使用 Azure 服务通过 NET Core 中的工作代理?

Does anyone have idea around getting through a work proxy in NET Core with Azure services?

推荐答案

我遇到了类似的问题,此问题已解决:

I have had a similar issue and this fixed it:

IWebProxy proxy = WebRequest.GetSystemWebProxy();
proxy.Credentials = CredentialCache.DefaultCredentials;
HttpClient.DefaultProxy = proxy;

这篇关于.NET Core 3.1 中的默认代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆