Another question regarding HTTPWebRequest

  • Thread starter Thread starter Nick Jacobsen
  • Start date Start date
N

Nick Jacobsen

I am using HTTPWebRequest to connect to a server which requires
authentication, then responds with a 302 redirect message, and redirects to
DIFFERANT server, which also requires authentication (the same credentials
as the first server). My problem is I can get my client to authenticate to
the first server, and redirect to the first server, but then when the second
server responds with a 401 authentication required message, HTTPWebRequest
does not try to authenticate again... any suggestions?

TIA
Nick Jacobsen
 
Nick Jacobsen said:
I am using HTTPWebRequest to connect to a server which requires
authentication, then responds with a 302 redirect message, and redirects to
DIFFERANT server, which also requires authentication (the same credentials
as the first server). My problem is I can get my client to authenticate to
the first server, and redirect to the first server, but then when the second
server responds with a 401 authentication required message, HTTPWebRequest
does not try to authenticate again... any suggestions?

Nick,

My guess is that HttpWebRequest doesn't know it's a different server
returning the 401, so it thinks the authentication failed. Maybe if you turn
off automatic redirections and redirect on your own, the 401 from the second
server will be the only 401 HttpWebRequest sees.
 
Since you have different servers that are requiring same credentials, you
should use the CredentialCache, instead of NetworkCredential on the request.
This is how you do it:

CredentialCache cache = new CredentialCache();
cache.Add(new Uri(http://server1/path1), "digest", new
NetworkCredential("user1", "pass1", "dom1"));
cache.Add(new Uri(http://server2/path2), "digest", new
NetworkCredential("user1", "pass1", "dom1"));
req.Credentials = cache;

Now, the webrequest will know that there is a credential for the second
server, and will use those for the second request. Just change the above
lines to suit your needs.

==========================
This posting is provided as-is. It does not offer any warranties and confers
no rights.
 
Back
Top