How to Log into a Site with Webclient

Using WebClient or WebRequest to login to a website and access data

Update:

See my comment below.


Here's what I did and it works (credit).

Add this class first:

namespace System.Net
{
using System.Collections.Specialized;
using System.Linq;
using System.Text;

public class CookieAwareWebClient : WebClient
{
public void Login(string loginPageAddress, NameValueCollection loginData)
{
CookieContainer container;

var request = (HttpWebRequest)WebRequest.Create(loginPageAddress);

request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";

var query = string.Join("&",
loginData.Cast<string>().Select(key => $"{key}={loginData[key]}"));

var buffer = Encoding.ASCII.GetBytes(query);
request.ContentLength = buffer.Length;
var requestStream = request.GetRequestStream();
requestStream.Write(buffer, 0, buffer.Length);
requestStream.Close();

container = request.CookieContainer = new CookieContainer();

var response = request.GetResponse();
response.Close();
CookieContainer = container;
}

public CookieAwareWebClient(CookieContainer container)
{
CookieContainer = container;
}

public CookieAwareWebClient()
: this(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;
}
}
}

Usage:

public static void Main()
{
var loginAddress = "www.mywebsite.com/login";
var loginData = new NameValueCollection
{
{ "username", "shimmy" },
{ "password", "mypassword" }
};

var client = new CookieAwareWebClient();
client.Login(loginAddress, loginData);
}

How do I log into a site with WebClient?

If the problem you are having is you can authenticate but you cant keep the authentication cookie here is a cookie aware version of WebClient.

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

protected override WebRequest GetWebRequest(Uri address)
{
WebRequest request = base.GetWebRequest(address);

var castRequest = request as HttpWebRequest;
if (castRequest != null)
{
castRequest.CookieContainer = this.CookieContainer;
}

return request;
}
}

EDIT:
The link you gave me uses forms authentication with HTTP POST, I don't have the time to walk though it but at least it gives you a start with Google.

C# WebClient Log onto Website

This is posted on server if you try to login in browser:

org.apache.struts.taglib.html.TOKEN=81243ce1a02ff285745f7c25b86234a9&showLogin=true&upgrade=&username=username&password=password&submit=Log+in

Try adding those values as well, and figure out how TOKEN is generated.

EDIT: Check if cookies that page gives you are submited back too.

ANOTHER EDIT: Too see what is going on between server and browser (=Firefox) when you are making a request or posting data use LiveHttpHeaders addon.

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 log into a Flash Website with WebClient and download a file using c# ?

You cant do this with simple WebClient.

To do what you need, I suggest you to use browser automation with Selenium and Chrome Driver. Since browser already handle flash, you just need to use Selenium to navigate and do what you want.

How to use webclient in a secure site?

One way would be through automating a browser -- you mentioned WebClient, so I'm guessing you might be referring to WebClient in .NET.

Two main points:

  • There's nothing special about https related to WebClient - it just works
  • Cookies are typically used to carry authentication -- you'll need to capture and replay them

Here's the steps I'd follow:

  1. GET the login form, capture the the cookie in the response.
  2. Using Xpath and HtmlAgilityPack, find the "input type=hidden" field names and values.
  3. POST to login form's action with user name, password, and hidden field values in the request body. Include the cookie in the request headers. Again, capture the cookie in the response.
  4. GET the pages you want, again, with the cookie in the request headers.

On step 2, I mention a somewhat complicated method for automating the login. Usually, you can post with username and password directly to the known login form action without getting the initial form or relaying the hidden fields. Some sites have form validation (different from field validation) on their forms which makes this method not work.

HtmlAgilityPack is a .NET library that allows you to turn ill-formed html into an XmlDocument so you can XPath over it. Quite useful.

Finally, you may run into a situation where the form relies on client script to alter the form values before submitting. You may need to simulate this behavior.

Using a tool to view the http traffic for this type of work is extremely helpful - I recommend ieHttpHeaders, Fiddler, or FireBug (net tab).

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.

Can't login into asp.net website with WebClient

If/When configured properly, you cannot "spoof" an ASP.Net Web Forms Postback - MSDN ref: VIEWSTATE and EVENTVALIDATION



Related Topics



Leave a reply



Submit