Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Domain credentials for a WebClient class don't work

I'm trying to get a HTML source of a website through C# code. When I access the site with Windows Authentication, the following code works:

using (WebClient client = new WebClient())
            {
                client.Credentials = CredentialCache.DefaultCredentials;
                using (Stream stream = client.OpenRead("http://intranet/"))
                using (StreamReader reader = new StreamReader(stream))
                {
                    MessageBox.Show(reader.ReadToEnd());
                }
            }

When I enter my domain credentials manually, I get an "unauthenticated" message.

using (WebClient client = new WebClient())
            {
                NetworkCredential credentials = new NetworkCredential("username", "pass", "domain");
                client.Credentials = credentials;
                using (Stream stream = client.OpenRead("http://intranet/"))
                using (StreamReader reader = new StreamReader(stream))
                {
                    MessageBox.Show(reader.ReadToEnd());
                }
            }

Why is it so?

like image 632
agnieszka Avatar asked Nov 05 '09 13:11

agnieszka


1 Answers

Try this:

CredentialCache cc = new CredentialCache();
cc.Add(
    new Uri("http://intranet/"), 
    "NTLM", 
    new NetworkCredential("username", "pass", "domain"));
client.Credentials = cc;
like image 194
Darin Dimitrov Avatar answered Nov 17 '22 01:11

Darin Dimitrov