【问题标题】:SoapHttpClientProtocol caches proxy credentials?SoapHttpClientProtocol 缓存代理凭据?
【发布时间】:2010-11-15 00:25:57
【问题描述】:

我在 .NET4 中注意到 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

【问题讨论】:

    标签: .net proxy soaphttpclientprotocol


    【解决方案1】:

    使用 Reflector 快速查看 HttpWebRequest 会发现它不会根据代理凭据区分 ServicePoint 对象。因此,它为所有这些请求重用相同的 http 连接池,并且由于在第一次成功的代理身份验证后相同的连接保持活动状态,它甚至不会尝试使用为最后两个请求提供的凭据。

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

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

    【讨论】:

    • 感谢彭特!更改连接组名称就可以了。奇怪的是, user:pass@host:port 将无法一起进行身份验证。更改连接组名称似乎足够安全,但是您认为这些方法中的任何一个都存在安全隐患吗?
    • 是的,proxyuser 方案不起作用:未处理的异常:System.NotSupportedException:ServicePointManager 不支持 proxyuser 方案的代理。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-02
    • 2019-10-04
    • 2011-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多