Building a CredentialCache for HttpWebRequest.Credentials when redirects are uknown

13,217

I used the code and got 401 error (SharePoint 2010 OOB Web Services). Then checked in some other site and tried "NTLM" instead of "Negotiate" in the below line. It's working fine now.

Not Working:

cache.Add(new Uri(myProxy.Url), "Negotiate", new NetworkCredential("UserName", "Password", "Domain"))

Working:

cache.Add(new Uri(myProxy.Url), "NTLM", new NetworkCredential("UserName", "Password", "Domain"))
Share:
13,217
Nate
Author by

Nate

I'm a developer with broad experience and a curious disposition. I like learning about and creating new things.

Updated on June 05, 2022

Comments

  • Nate
    Nate almost 2 years

    I recently asked a question concerning NetworkCredential and HttpWebRequest.Credentials when a server returns redirects. I determined that building a CredentialCache of NetworkCredential instances works for my scenario. Now I have a temporary method that builds a CredentialCache with all of the domain names hard coded in. It worked, which is great.

            CredentialCache cache = new CredentialCache();
            cache.Add(new Uri("http://example.com"), "Negotiate", loginCredentials);
            cache.Add(new Uri("http://redirected.example.com"), "Negotiate", loginCredentials);
            request.Credentials = cache;
    

    Now, I need to make this more flexible. The whole idea of the redirects is for load balancing on the server. The client won't know exactly where it'll get redirected to until the call to HttpWebRequest.GetResponse(). What is the preferred method to building the CredentialCache to include each redirected server as they are encountered? Also, what is the rational behind making this so difficult? Why can't a single NetworkCredentials instance satisfy HttpWebRequest.Credentials for each redirect? Does it introduce security vulnerabilities to reuse credentials across redirects?

    Thanks.

  • Nate
    Nate about 13 years
    I wish I could. We have a need for the program to be able to run under a local account. So, I've written a login dialog window the user will enter their valid kerberos credentials into.