加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何在defaultProxy配置设置中传递凭据?

发布时间:2020-12-15 08:26:16 所属栏目:百科 来源:网络整理
导读:客户端无法使用我的webpart,因为他位于代理服务器后面,他们需要指定用户名和密码才能通过代理.我现在在配置文件中有这个: system.net defaultProxy proxy usesystemdefault="False" proxyaddress="http://127.0.0.1:8888" bypassonlocal="True" / /defaultP
客户端无法使用我的webpart,因为他位于代理服务器后面,他们需要指定用户名和密码才能通过代理.我现在在配置文件中有这个:
<system.net>
    <defaultProxy>        
      <proxy usesystemdefault="False" proxyaddress="http://127.0.0.1:8888" bypassonlocal="True"   />
    </defaultProxy>
  </system.net>

有没有办法为这个代理设置提供用户名和密码?

解决方法

我不知道在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>

然后将代理分配给您在webPart中使用的webClient.

编辑:

如果我做了更多的功课,我会意识到你的问题可以用一个属性修复:useDefaultCredentials =“true”

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读