Webclient Accessing Page with Credentials

WebClient accessing page with credentials

I suspect that the web page that you are trying to access uses Forms Authentication. This means that you will have to provide a valid authentication cookie if you want to be able to access protected resources. And in order to obtain a valid authentication cookie you will have to first authenticate yourself by sending a POST request to the LogOn page which emits the cookie. Once you retrieve the cookie you will be able to send it along on subsequent requests on protected resources. You should also note that out of the box WebClient doesn't support cookies. For this reason you could write a custom cookie aware web client:

public class CookieAwareWebClient : WebClient
{
public CookieAwareWebClient()
{
CookieContainer = new CookieContainer();
}
public CookieContainer CookieContainer { get; private set; }

protected override WebRequest GetWebRequest(Uri address)
{
var request = (HttpWebRequest)base.GetWebRequest(address);
request.CookieContainer = CookieContainer;
return request;
}
}

Now you could use this client to fire off the 2 requests:

using (var client = new CookieAwareWebClient())
{
var values = new NameValueCollection
{
{ "username", "john" },
{ "password", "secret" },
};
client.UploadValues("http://domain.loc/logon.aspx", values);

// If the previous call succeeded we now have a valid authentication cookie
// so we could download the protected page
string result = client.DownloadString("http://domain.loc/testpage.aspx");
}

Obviously due to the ViewState crapiness of ASP.NET you might need to send a couple of other parameters along your logon request. Here's what you could do: authenticate in a web browser and look with FireBug the exact parameters and headers that need to be sent.

How do I authenticate a WebClient request?

What kind of authentication are you using? If it's Forms authentication, then at best, you'll have to find the .ASPXAUTH cookie and pass it in the WebClient request.

At worst, it won't work.

C# connecting to web page with authentication

You are using NetworkCredential to login at the webapp, but the webapp is using some kind of forms authentication.
As long as the webapp is not configured to use network credentials this will not work.

Since this is a php application I guess it uses plain forms auth and that you will need to post username/password to the login page before continuing.

Forwarding credentials with ASP.net WebClient

i assume that you have integrated authentication enabled on the web server hosting the aspx page and that the server is IIS.

if the page is not on the reporting server then you may be experiencing the double hop issue related to authentication forwarding. the solution requires kerberos authentication and proper configuration of a few items at the domain level.

Unauthorized to use WebClient on page

Try this:

var client = new WebClient();
client.Credentials = new System.Net.NetworkCredential("admin", "admin", "");
string downloadString = client.DownloadString("http://192.168.0.100/Monarch/syncconnect/sdk.aspx?command=GetStatus");
loaded.Text = downloadString;

how to make WebClient prompt for credentials

Look how the browser works using Fiddler and you will see that when you paste the URL into the browser the server returns a 401 which then prompts the browser to show the auth dialog.

If you know you will always need credentials then you should prompt before your API call. If you dont know then you will need to try first and check the response for 401 and then prompt



Related Topics



Leave a reply



Submit