C# 是否可以在 web.config 中指定代理凭据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/186800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 17:07:57  来源:igfitidea点击:

Is it possible to specify proxy credentials in your web.config?

c#web-servicesproxy

提问by spmason

I need to configure a website to access a webservice on another machine, via a proxy. I can configure the website to use a proxy, but I can't find a way of specifying the credentials that the proxy requires, is that possible? Here is my current configuration:

我需要配置一个网站以通过代理访问另一台机器上的网络服务。我可以将网站配置为使用代理,但找不到指定代理所需凭据的方法,这可能吗?这是我目前的配置:

<defaultProxy useDefaultCredentials="false">
    <proxy usesystemdefault="true" proxyaddress="<proxy address>" bypassonlocal="true" />
</defaultProxy>

I know you can do this via code, but the software the website is running is a closed-source CMS so I can't do this.

我知道你可以通过代码来做到这一点,但网站运行的软件是一个封闭源代码的 CMS,所以我不能这样做。

Is there any way to do this? MSDN isn't helping me much..

有没有办法做到这一点?MSDN 对我帮助不大。

采纳答案by Jerome Laban

Yes, it is possible to specify your own credentials without modifying the current code. It requires a small piece of code from your part though.

是的,可以在不修改当前代码的情况下指定您自己的凭据。不过,它需要您编写一小段代码。

Create an assembly called SomeAssembly.dllwith this class :

使用此类创建一个名为SomeAssembly.dll的程序集:

namespace SomeNameSpace
{
    public class MyProxy : IWebProxy
    {
        public ICredentials Credentials
        {
            get { return new NetworkCredential("user", "password"); }
            //or get { return new NetworkCredential("user", "password","domain"); }
            set { }
        }

        public Uri GetProxy(Uri destination)
        {
            return new Uri("http://my.proxy:8080");
        }

        public bool IsBypassed(Uri host)
        {
            return false;
        }
    }
}

Add this to your config file :

将此添加到您的配置文件中:

<defaultProxy enabled="true" useDefaultCredentials="false">
  <module type = "SomeNameSpace.MyProxy, SomeAssembly" />
</defaultProxy>

This "injects" a new proxy in the list, and because there are no default credentials, the WebRequest class will call your code first and request your own credentials. You will need to place the assemble SomeAssembly in the bin directory of your CMS application.

这会在列表中“注入”一个新代理,并且由于没有默认凭据,WebRequest 类将首先调用您的代码并请求您自己的凭据。您需要将 assemble SomeAssembly 放在 CMS 应用程序的 bin 目录中。

This is a somehow static code, and to get all strings like the user, password and URL, you might either need to implement your own ConfigurationSection, or add some information in the AppSettings, which is far more easier.

这是一个以某种方式静态代码,要获取所有字符串,如用户、密码和 URL,您可能需要实现自己的ConfigurationSection,或者在AppSettings 中添加一些信息,这要容易得多。

回答by questzen

Directory Services/LDAP lookups can be used to serve this purpose. It involves some changes at infrastructure level, but most production environments have such provision

目录服务/LDAP 查找可用于实现此目的。它涉及基础设施层面的一些变化,但大多数生产环境都有这样的规定

回答by Scott Ferguson

While I haven't found a good way to specify proxy network credentials in the web.config, you might find that you can still use a non-coding solution, by including this in your web.config:

虽然我还没有找到在 web.config 中指定代理网络凭据的好方法,但您可能会发现仍然可以使用非编码解决方案,将其包含在您的 web.config 中:

  <system.net>
    <defaultProxy useDefaultCredentials="true">
      <proxy proxyaddress="proxyAddress" usesystemdefault="True"/>
    </defaultProxy>
  </system.net>

The key ingredient in getting this going, is to change the IIS settings, ensuring the account that runs the process has access to the proxy server. If your process is running under LocalService, or NetworkService, then this probably won't work. Chances are, you'll want a domain account.

实现这一目标的关键因素是更改 IIS 设置,确保运行该进程的帐户可以访问代理服务器。如果您的进程在 LocalService 或 NetworkService 下运行,那么这可能不起作用。很有可能,您需要一个域帐户。

回答by Silas Humberto Souza

You can specify credentials by adding a new Generic Credential of your proxy server in Windows Credentials Manager:

您可以通过在 Windows 凭据管理器中添加代理服务器的新通用凭据来指定凭据:

1 In Web.config

1 在 Web.config 中

<system.net>    
<defaultProxy enabled="true" useDefaultCredentials="true">      
<proxy usesystemdefault="True" />      
</defaultProxy>    
</system.net>
  1. In Credential Manager >> Add a Generic Credential
  1. 在凭据管理器中>>添加通用凭据

Internet or network address: your proxy address
User name: your user name
Password: you pass

互联网或网络地址:您的代理地址
用户名:您的用户名
密码:您通过

This configuration worked for me, without change the code.

此配置对我有用,无需更改代码。