简体   繁体   English

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

[英]How to pass credentials in defaultProxy config setting?

A client is unable to use my webpart because he is behind a proxy server and they need to specify a username and password to get past the proxy. 客户端无法使用我的webpart,因为他位于代理服务器后面,他们需要指定用户名和密码才能通过代理。 I have this in my config file right now: 我现在在配置文件中有这个:

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

Is there a way to supply a username and password to this proxy setting? 有没有办法为这个代理设置提供用户名和密码?

I'm not aware of a way to do this in defaultProxy section of web.config, but you can definitely do it from code. 我不知道在web.config的defaultProxy部分执行此操作的方法,但您绝对可以从代码中执行此操作。 Try this: 尝试这个:

// 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 (configuration section): Web.Config(配置部分):

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

And then assign proxy to the webClient you are using in your webPart. 然后将proxy分配给您在webPart中使用的webClient。

EDIT: 编辑:

If I had done more homework, I would have realized your problem could have been fixed with one attribute: useDefaultCredentials="true" 如果我做了更多的功课,我会意识到你的问题可以用一个属性修复:useDefaultCredentials =“true”

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从web.config的system.net部分读取defaultProxy设置的值? - How can I read the values of the defaultProxy settings from the system.net section in a web.config? 让 HttpClient 使用 app.config defaultProxy - Make HttpClient use app.config defaultProxy 在运行时在app.config中设置defaultProxy - Set defaultProxy in app.config at runtime 以编程方式更改defaultproxy而不是使用app.config - Programmatically changing defaultproxy instead of using app.config 如何将凭据传递给 SOAP 网络服务? - How to pass credentials to a SOAP webservice? 如何传递消息凭证-TransportWithMessageCredential-SOAP请求中没有凭证 - How to pass message credentials - TransportWithMessageCredential - No credentials in the SOAP request 如何在Windows身份验证中传递默认凭据 - How to pass default credentials in Windows Authentication 如何将用户凭据传递给Web服务? - How to pass user credentials to web service? "如何让 HttpClient 与请求一起传递凭据?" - How to get HttpClient to pass credentials along with the request? 如何将凭据传递给httpwebrequest以访问SharePoint库 - How to pass credentials to httpwebrequest for accessing SharePoint Library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM