C# - passing authentication credentials with HttpWebRequest
12,974
Solution 1
For Basic authentication (not sure about other authentication schemes):
request.Credentials = new NetworkCredential("username", "password");
Solution 2
For forms authentication where a valid cookie is available in the page Context, you can use this answer.
https://stackoverflow.com/a/1589723/253131
Author by
Luke G
Updated on June 04, 2022Comments
-
Luke G almost 2 years
I am working on a tool that parses html source of given urls. Some of them are password protected.
Here is my question: How can I pass authentication credentials with the HttpWebRequest? Does it require setting up a cookie? These are new grounds to me, thus examples would be very helpful.
In summary, I use the following for requests that not require an authentication.
... HttpWebRequest request =(HttpWebRequest)WebRequest.Create(HttpUtility.UrlDecode(<URL STRING>)); ... HttpWebResponse response =(HttpWebResponse)request.GetResponse();
-
RBT about 7 yearsThe same works well for
Integrated Windows
authentication scheme as well if you have domain credentials of a specific user.