是否可以在您的 web.config 中指定代理凭据? [英] Is it possible to specify proxy credentials in your web.config?

查看:20
本文介绍了是否可以在您的 web.config 中指定代理凭据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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>

我知道您可以通过代码执行此操作,但是该网站运行的软件是一个封闭源代码的 CMS,因此我无法执行此操作.

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.

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

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

推荐答案

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

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.

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

Create an assembly called SomeAssembly.dll with this class :

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>

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

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.

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

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.

这篇关于是否可以在您的 web.config 中指定代理凭据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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