Frage

Ich habe in .NET4 bemerkt, dass der Proxy-Anmeldeinformationen Soaphttpclientprotocol zwischenzuspeichern erscheint. Weiß jemand, ob dies wahr ist, und wie diese Cache aktualisieren? Wenn meine Benutzer ihre Anmeldeinformationen ändern würde Ich mag sich, um zu versuchen, aber wenn ich eine Soaphttpclientprotocol, um erfolgreich zu verbinden bekommen, jeden Anruf aufzurufen scheint zu funktionieren.

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;
    }

Die Ausgabe dieses Programms (Ich bin die letzten beiden erwartet, falsch zu sein)

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
War es hilfreich?

Lösung

A quick look at HttpWebRequest with Reflector shows that it doesn't differentiate ServicePoint objects based on the proxy credentials. So it reuses the same http connection pool for all those requests and since the same connection stays alive after the first successful proxy authentication, it doesn't even try to use the credentials provided for the last two requests.

You could try setting the ConnectionGroupName property to some string containing the proxy username and password at the time you set the Proxy property (with a helper method maybe).

Another option is to use the proxyuser:password@basicproxy:2121 url scheme for the WebProxy constructor.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top