2010-11-15 100 views
1

我注意到在.NET4中SoapHttpClientProtocol似乎缓存了代理证书。有谁知道这是否属实,以及如何刷新此缓存?如果我的用户更改他们的凭证,我想尝试他们,但是一旦我获得任何SoapHttpClientProtocol成功连接,任何调用调用似乎都可以工作。SoapHttpClientProtocol缓存代理凭证?

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Net; 
using System.Web.Services; 
    class Program : System.Web.Services.Protocols.SoapHttpClientProtocol 
{ 
    public Program() 
    { 
     base.Url = "something"; 
    } 
    static void Main(string[] args) 
    { 
     Program x = new Program(); 
     x.Proxy = new WebProxy("basicproxy", 2121); 
     x.Proxy.Credentials = new NetworkCredential("proxyuser", "IncorrectPassword"); 
     Console.WriteLine("Attempt with proxyuser and IncorrectPassword: " + x.NoOp()); 

     x.Proxy.Credentials = new NetworkCredential("proxyuser", "password"); 
     Console.WriteLine("Attempt with proxyuser and password: " + x.NoOp()); 

     x.Proxy.Credentials = new NetworkCredential("proxyuser", "IncorrectPassword"); 
     Console.WriteLine("Attempt with proxyuser and IncorrectPassword: " + x.NoOp()); 

     Program y = new Program(); 
     y.Proxy = new WebProxy("basicproxy", 2121); 
     y.Proxy.Credentials = new NetworkCredential("proxyuser", "IncorrectPassword"); 
     Console.WriteLine("Attempt with proxyuser and IncorrectPassword: " + y.NoOp()); 
    } 

    /// <remarks/> 
    [System.Web.Services.Protocols.SoapDocumentMethodAttribute("...", RequestNamespace = "...", ResponseNamespace = "...", Use = System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle = System.Web.Services.Protocols.SoapParameterStyle.Wrapped)] 
    public bool NoOp() 
    { 
     try 
     { 
      object[] results = this.Invoke("NoOp", new object[0]); 
      return ((bool)(results[0])); 
     } 
     catch (WebException e) 
     { 
      if (e.Response != null && ((HttpWebResponse)e.Response).StatusCode == HttpStatusCode.ProxyAuthenticationRequired) 
      { 
       Console.WriteLine("incorrect Credential attempt!"); 
      } 
      else 
      { 
       Console.WriteLine("Exception: " + e.Message); 
      } 
     } 
     return false; 
    } 

这个程序的输出(我期待最后两个是假的)

incorrect Credential attempt! 
Attempt with proxyuser and IncorrectPassword: False 
Attempt with proxyuser and password: True 
Attempt with proxyuser and IncorrectPassword: True 
Attempt with proxyuser and IncorrectPassword: True 

回答

1

快速浏览一下HttpWebRequest与反射显示,基于它不区分ServicePoint对象代理凭证。因此,它为所有这些请求重新使用相同的http连接池,并且由于第一次成功的代理身份验证后相同的连接保持活动状态,它甚至不会尝试使用为最后两个请求提供的凭据。

在设置Proxy属性(可能有帮助器方法)时,您可以尝试将ConnectionGroupName属性设置为包含代理用户名和密码的某个字符串。

另一种选择是对WebProxy构造函数使用proxyuser:password @ basicproxy:2121 url scheme。

+0

Thanks Pent!改变连接组名称的窍门。用户:通过@主机:端口将无法验证所有在一起,奇怪。更改连接组名似乎足够安全,但您认为对于这些方法中的任何一种都存在安全隐患? – Dlongnecker 2010-11-15 15:12:30

+0

是的,proxyuser方案不起作用:未处理的异常:System.NotSupportedException:ServicePointManager不支持具有proxyuser方案的代理。 – Dlongnecker 2010-11-16 04:17:11