2010-07-22 49 views
6

客户端无法使用我的web部件,因为他在代理服务器后面,他们需要指定用户名和密码才能通过代理服务器。我在配置文件中,现在有这样的:如何在defaultProxy配置设置中传递凭据?

<system.net> 
    <defaultProxy>   
     <proxy usesystemdefault="False" proxyaddress="http://127.0.0.1:8888" bypassonlocal="True" /> 
    </defaultProxy> 
    </system.net> 

是否有提供这种代理设置一个用户名和密码的方法吗?

回答

12

我不知道在web.config的defaultProxy部分执行此操作的方法,但是您绝对可以从代码执行此操作。试试这个:

// Get proxy server info from AppSettings section of Web.Config 
var proxyServerAddress = ConfigurationManager.AppSettings[ "proxyServerAddress" ]; 
var proxyServerPort = ConfigurationManager.AppSettings[ "proxyServerPort" ]; 

// Get proxy with default credentials 
WebProxy proxy =new WebProxy(proxyServerAddress, proxyServerPort); 
proxy.Credentials = System.Net.CredentialCache.DefaultCredentials(); 

Web.Config中(配置部分):

<appSettings> 
    <add key="proxyServerAddress" value="proxy.myhost.com" /> 
    <add key="proxyServerPort" value="8080" /> 
</appSettings> 

,然后分配proxy您正在使用您的WebPart WebClient的。

编辑:

如果我做了功课多,我就意识到自己的问题可能已经固定有一个属性:useDefaultCredentials =“真”

<system.net> 
    <defaultProxy useDefaultCredentials="true"> 
     <proxy usesystemdefault="False" proxyaddress="http://127.0.0.1:8888" bypassonlocal="True" /> 
    </defaultProxy> 
</system.net> 
+0

感谢。但是这需要成为我的客户的凭据......我不确定我可以得到他的密码。无论如何,我可以解决这个问题? – Prabhu 2010-07-22 17:51:10

+0

更新代码以获取当前登录用户的凭据。 – 2010-07-22 17:52:21

+0

您认为我可以在配置中设置地址和端口,只需在代码中添加proxy.Credentials行即可? – Prabhu 2010-07-22 18:05:34

相关问题