c # - System.Net.CredentialCache og HttpWebResponse

Indlæg af Hanne Mølgaard Plasc

Problem



Jeg forsøger at kode noget funktionalitet, hvor brugeren kan logge ind på en fjernserver ved at bruge sine egne Windows-referencer eller ved at angive en bruger, et password og et domæne.


For at vide hvordan man gør det, læser jeg dette link
static void Main(string[] args)
{
    string password = "password", username = "Username", dom = "DOMAIN";
    string url = "http://my.url.com/LoginWithNativeCredentials?";

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

    /////// Different User code////
    NetworkCredential credentials = new NetworkCredential(username, password, dom);
    CredentialCache cache = new CredentialCache();
    cache.Add(new Uri(url), "NTLM", credentials);
    request.Credentials = cache;
    /////////////////////////

    ////// Current Windows user's credential
    //request.Credentials = CredentialCache.DefaultCredentials;
    /////////////////////////

    request.AllowAutoRedirect = true;
    request.CookieContainer = new CookieContainer(5);

    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {

        Console.WriteLine("In!");
    }

    Console.WriteLine("Done!");
    Console.ReadLine();
}
.


Jeg har kunnet logge ind via CredentialCache.DefaultCredentials.


Men når jeg forsøger at godkende via bruger, navn, kodeord og domæne, fortsætter jeg med at få en 401-fejl.


Efter nogle Googling og søgning her har jeg fundet nogle sandsynlige fejl (omdirigering, forskellige auth. Typer {basic, digest, ntlm, negotiate} og endog modsat [[dvs. at kunne logge ind via bruger + pasword men nej ved CredentialCache.DefaultCredentials]]).


Eventuelle hints?


Rediger : måske vil nogle kode give dig nogle spor om, hvad jeg gør galt.


static void Main(string[] args)
{
    string password = "password", username = "Username", dom = "DOMAIN";
    string url = "http://my.url.com/LoginWithNativeCredentials?";

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

    /////// Different User code////
    NetworkCredential credentials = new NetworkCredential(username, password, dom);
    CredentialCache cache = new CredentialCache();
    cache.Add(new Uri(url), "NTLM", credentials);
    request.Credentials = cache;
    /////////////////////////

    ////// Current Windows user's credential
    //request.Credentials = CredentialCache.DefaultCredentials;
    /////////////////////////

    request.AllowAutoRedirect = true;
    request.CookieContainer = new CookieContainer(5);

    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {

        Console.WriteLine("In!");
    }

    Console.WriteLine("Done!");
    Console.ReadLine();
}


Mange tak!


static void Main(string[] args)
{
    string password = "password", username = "Username", dom = "DOMAIN";
    string url = "http://my.url.com/LoginWithNativeCredentials?";

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

    /////// Different User code////
    NetworkCredential credentials = new NetworkCredential(username, password, dom);
    CredentialCache cache = new CredentialCache();
    cache.Add(new Uri(url), "NTLM", credentials);
    request.Credentials = cache;
    /////////////////////////

    ////// Current Windows user's credential
    //request.Credentials = CredentialCache.DefaultCredentials;
    /////////////////////////

    request.AllowAutoRedirect = true;
    request.CookieContainer = new CookieContainer(5);

    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {

        Console.WriteLine("In!");
    }

    Console.WriteLine("Done!");
    Console.ReadLine();
}
http://support.microsoft.com/kb/811318 [8]

Bedste reference