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

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

问题描述

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

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>

我知道你可以通过code做到这一点,但该网站正在运行的软件是闭源的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..

推荐答案

是的,它可以在不改变目前的code至指定自己的凭据。它需要从你的一部分,一小片code的虽然。

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类会先打电话给你的code,并要求你自己的凭据。你需要把你的应用程序CMS的bin目录中的组装SomeAssembly。

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.

这是一个莫名其妙的静态code,并获得如用户名,密码和URL的所有字符串,则可能要么需要实现自己的<一个href=\"http://msdn.microsoft.com/en-us/library/system.configuration.configurationsection.aspx\">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天全站免登陆