我正在编写一个程序,应该可以在没有代理的情况下使用代理并使用身份验证 - 自动!它应该调用WCF服务.在此示例中,调用实例client.我还使用自编写的类(proxyHelper)来请求凭据.
BasicHttpBinding connection = client.Endpoint.Binding as BasicHttpBinding;<br/>
connection.ProxyAddress = _???_<br/>
connection.UseDefaultWebProxy = false;<br/>
connection.BypassProxyOnLocal = false;<br/>
connection.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.Basic;<br/>
client.ClientCredentials.UserName.UserName = proxyHelper.Username;
client.ClientCredentials.UserName.Password = proxyHelper.Password;
Run Code Online (Sandbox Code Playgroud)
我遇到了获取ProxyAddress的问题.如果我HttpWebRequest.GetSystemWebProxy()用来获取实际定义的代理,我在调试模式下看到正确的代理地址,但它是非公共属性.将UseDefaultWebProxy设置为true不起作用,如果我添加硬编码的代理地址并将UseDefaultWebProxy设置为false,则它可以正常工作.那么......我怎样才能收集默认Web代理的地址?
访问互联网我是一个需要身份验证的代理.我知道将网络凭据传递给代理是非常简单的,如下所示:
FtpWebRequest request = FtpWebRequest.Create(
new Uri("ftp://upload.myserver.com") as FtpWebRequest;
NetworkCredential credentials = new NetworkCredential("username", "password");
request.Credentials = credentials;
Run Code Online (Sandbox Code Playgroud)
这有效!
我也尝试使用,CredentialCache.DefaultNetworkCredentials但这不起作用.我想避免在任何地方存储用户名和密码(代码,数据库,配置文件).
我认为最简单的方法是使用Internet Explorer访问Internet时显示的相同对话框.有谁知道如何提出这个对话框?
http://services.arcgisonline.com/arcgisexplorer500/help/proxy_connect_to_on_browser_request.png
编辑
此任务的目标是通过FTP上传文件.最后我发现没有必要为FTP请求设置代理,因为.NET框架不允许通过HTTP代理进行FTP操作.但是您必须将代理属性显式设置为null.
FtpWebRequest request = FtpWebRequest.Create(
new Uri("ftp://upload.myserver.com") as FtpWebRequest;
NetworkCredential credentials = new NetworkCredential("username", "password");
request.Credentials = credentials;
request.Proxy = null;
Run Code Online (Sandbox Code Playgroud)
而已!
在我们的程序安装过程中,我们运行此方法来加密app.config的各个部分:
// Get the application configuration file.
Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// Define the Rsa provider name.
const string provider = "RsaProtectedConfigurationProvider";
// Get the section to protect.
ConfigurationSection connStrings = config.ConnectionStrings;
if (connStrings != null)
{
if (!connStrings.SectionInformation.IsProtected)
{
if (!connStrings.ElementInformation.IsLocked)
{
// Protect the section.
connStrings.SectionInformation.ProtectSection(provider);
connStrings.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Full);
}
}
}
Run Code Online (Sandbox Code Playgroud)
到目前为止工作正常.但是如果我运行这个程序,我们会遇到几个机器出现以下错误"无法使用提供程序'RsaProtectedConfigurationProvider解密'.来自提供程序的错误消息:无法打开RSA密钥容器 ".
当然,我搜索并找到了这个帮助,但这不起作用.有任何想法吗?