I created a c# console application to log into a website with Selenium and it works great. Now I want to add this feature to an existing .Net Core application.
So far I have the following:
string formUrl = "https://cap.mcmaster.ca/mcauth/login.jsp?app_id=1505&app_name=Avenue";
string formParams = string.Format("user_id={0}&pin={1}", "user", "pass"); //In my program I have the correct credentials
string cookieHeader;
var cookies = new CookieContainer();
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(formUrl);
req.CookieContainer = cookies;
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
byte[] bytes = Encoding.ASCII.GetBytes(formParams);
req.ContentLength = bytes.Length;
using (Stream os = req.GetRequestStream())
{
os.Write(bytes, 0, bytes.Length);
}
WebResponse resp = req.GetResponse();
cookieHeader = resp.Headers["Set-cookie"];
string pageSource;
string getUrl = "https://avenue.cllmcmaster.ca/d2l/home";
HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create(getUrl);
getRequest.CookieContainer = new CookieContainer();
getRequest.CookieContainer.Add(resp.cookies);
getRequest.Headers.Add("Cookie", cookieHeader);
getRequest.Headers.Add("Cookie", cookieHeader);
WebResponse getResponse = getRequest.GetResponse();
using (StreamReader sr = new StreamReader(getResponse.GetResponseStream()))
{
pageSource = sr.ReadToEnd();
}
For some reason, in the above code Cookies is underlined in "resp.Cookies" as an error. (CS1061 'WebResponse' does not contain a definition for 'Cookies' and no accessible extension method 'Cookies' accepting a first argument of type 'WebResponse' could be found)
I tried to folow the posts here: Login to website, via C# but I can't seem to get it to work.
The reason you are getting a compile time error is because the WebResponse class does not have a Cookies property.
From .NET Docs
The WebResponse class is the abstract base class from which
protocol-specific response classes are derived.
Instead, take a look at the HttpWebResponse class. This class provides a HTTP specific implementation of WebResponse.
You can cast the WebResponse to a HttpWebResponse:
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
HttpWebResponse.Cookies Property
Gets or sets the cookies that are associated with this response.
Documentation
HttpWebResponse.Cookies
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I want to login to this website using C#: Here is my attempt but its sending me to first page. Not returning me the next page, that should be visible after login, please help me to resolve this:
string formParams =
string.Format("mail={0}&password={1}", store#admin.com", "admin");
string cookieHeader;
WebRequest req = WebRequest.Create("http://muslimgowns.com/dashboard/login/public_login");
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
byte[] bytes = Encoding.ASCII.GetBytes(formParams);
req.ContentLength = bytes.Length;
using (Stream os = req.GetRequestStream())
{
os.Write(bytes, 0, bytes.Length);
}
WebResponse resp = req.GetResponse();
cookieHeader = resp.Headers["Set-cookie"];
using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
{
string pageSource = sr.ReadToEnd();
File.AppendAllText("first.txt", pageSource);
}
string pageSource1;
string getUrl = "http://muslimgowns.com/dashboard/home";
WebRequest getRequest = WebRequest.Create(getUrl);
getRequest.Headers.Add("Cookie", cookieHeader);
WebResponse getResponse = getRequest.GetResponse();
using (StreamReader sr = new StreamReader(getResponse.GetResponseStream()))
{
pageSource1 = sr.ReadToEnd();
File.AppendAllText("second.txt", pageSource1);
}
}
It seems like the first GET request to public_login returns an amount of cookies, and the POST request with credentials afterwards must be sent to login_access instead of public_login
The use of an HttpWebRequest instead of WebRequest and by setting the cookie container of it helps, and actually the server responds with HTTP 302 Redirect to the POST request and the HttpWebRequest automatically follows this redirection and downloads the dashboard.
Always use an http tracing tool like Fiddler or Wireshark or Network Monitor or developer tools of your browser to see what is received (cookies, headers etc.) and what is sent back. That's how I got all this.
Here is the fix:
string formParams = string.Format("mail={0}&password={1}", "store#admin.com", "admin");
CookieContainer cookieContainer = new CookieContainer();
HttpWebRequest req = WebRequest.CreateHttp("http://muslimgowns.com/dashboard/login/public_login");
req.CookieContainer = cookieContainer;
req.GetResponse(); // This is just to get the initial cookies returned by the public_login
req = WebRequest.CreateHttp("http://muslimgowns.com/dashboard/login/login_access");
req.CookieContainer = cookieContainer; // Set the cookie container which contains the cookies returned by the public_login
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
byte[] bytes = Encoding.ASCII.GetBytes(formParams);
req.ContentLength = bytes.Length;
using (Stream os = req.GetRequestStream())
{
os.Write(bytes, 0, bytes.Length);
}
WebResponse resp = req.GetResponse();
using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
{
string pageSource = sr.ReadToEnd();
File.AppendAllText("first.txt", pageSource); // Dashboard is returned.
}
I am trying to get my c# console application to check if the credentials supplied are correct. But when I try it the web page gives a error:
<div id="login_error"> <strong>ERROR</strong>: Cookies are blocked or not supported by your browser. You must enable cookies to use WordPress.<br />
This is my code:
static bool SendRequest(string Username, string Password, string URL) {
string formUrl = URL;
string formParams = string.Format("log={0}&pwd={1}&wp-submit={2}&redirect_to={3}&testcookie={4}", Username, Password, "Log In", "http://localhost/wp-admin/", "1");
string cookieHeader;
WebRequest req = WebRequest.Create(formUrl);
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
byte[] bytes = Encoding.ASCII.GetBytes(formParams);
req.ContentLength = bytes.Length;
((HttpWebRequest)req).CookieContainer = new CookieContainer();
using (Stream os = req.GetRequestStream())
{
os.Write(bytes, 0, bytes.Length);
}
WebResponse resp = req.GetResponse();
cookieHeader = resp.Headers["Set-cookie"];
string pageSource;
using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
{
pageSource = sr.ReadToEnd();
}
Console.Write(pageSource);
return true;
}
I think the problem is that the cookies are not working but I have no clue how to fix this.
All help is appreciated!
You're not setting a CookieContainer to your HttpWebRequest, thus the default is set to null which means the client won't accept cookies.
CookieContainer from MSDN
The CookieContainer property provides an instance of the
CookieContainer class that contains the cookies associated with this
request.
CookieContainer is null by default. You must assign a
CookieContainer object to the property to have cookies returned in the
Cookies property of the HttpWebResponse returned by the GetResponse
method.
You must set a new CookieContainer before getting a response from the server.
req.ContentLength = bytes.Length;
((HttpWebRequest)req).CookieContainer = new CookieContainer();
// Rest of the code here..
I am currently in the process of automating a web interface for administrating users of an FTP.
I am trying to do this with HttpWebRequest, i have one call that logs me on the site and the second call is supose to add a new user for the FTP access.
I have tried my two urls in the browser and they work, they end up creating a user.
string login = "https://www.ftpsite.net/./panel/index.php?txvUsername=myaccount&txvPassword=myPassword&submitButton=Login";
this gets me logged in when i enter it in the browser address bar.
the second call to create a user is as follows.
string createUser = "https://www.ftpSite.net/panel/ftpsites/updatelogin?login_id=&login=toto123&realname=realnametoto&homedir=root&passwd=superpassword11&expdate=01-01-2100&neverExpire=on"a_value=0"a_unit=GB&group_id=0&status=on&ftp=on&filelist=on&ftp_download=on&http=on&web_filelist=on&web_download=on&email=";
This creates a user when i enter it in the browser's address bar if it follows the one that logs us in.
My problem is that i am trying to do this using HttpWebRequest and without success. I can get myself logged in but when i try to create the user it seems to return a "bad" error code saying i have created too many users already which isnt the case since i can create more after that call. Here is the code i use with HtttpRequest
_datCookie = new CookieContainer();
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(login);
httpWebRequest.Method = "POST";
httpWebRequest.CookieContainer = _datCookie;
WebResponse response = httpWebRequest.GetResponse();
referer = response.ResponseUri.AbsoluteUri;
Stream requestStream = response.GetResponseStream();
StreamReader streamReader = new StreamReader(requestStream);
_datCookie = httpWebRequest.CookieContainer;
response.Close();
httpWebRequest = (HttpWebRequest)WebRequest.Create(createUser);
httpWebRequest.CookieContainer = _datCookie;
httpWebRequest.Referer = referer;
httpWebRequest.Method = "POST";
response = httpWebRequest.GetResponse();
requestStream = response.GetResponseStream();
streamReader = new StreamReader(requestStream);
webBrowser.DocumentText = streamReader.ReadToEnd();
response.Close();
What i caught and tried to imitate without success here.
Are you sure they should be POST requests? The URLs seem to have all of the fields in the query-string, which suggests they should be GET requests instead.
Based on the Fiddler screen-shot, you need to make a POST request with the fields in the body, not the query-string:
var cookies = new CookieContainer();
// Request 1 : Login
var request = (HttpWebRequest)WebRequest.Create("https://www.ftpsite.net/./panel/index.php");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.CookieContainer = cookies;
string postData = "txvUsername=myaccount&txvPassword=myPassword&submitButton=Login";
byte[] postBytes = Encoding.Default.GetBytes(postData);
request.ContentLength = postBytes.Length;
using (Stream bod = request.GetRequestStream())
{
body.Write(postBytes, 0, postBytes.Length);
}
WebResponse response = request.GetResponse();
string referer = response.ResponseUri.AbsoluteUri;
// Request 2 : Create user
request = (HttpWebRequest)WebRequest.Create("https://www.ftpSite.net/panel/ftpsites/updatelogin");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.CookieContainer = cookies;
postData = "login_id=&login=toto123&realname=realnametoto&homedir=root&passwd=superpassword11&expdate=01-01-2100&neverExpire=on"a_value=0"a_unit=GB&group_id=0&status=on&ftp=on&filelist=on&ftp_download=on&http=on&web_filelist=on&web_download=on&email=";
postBytes = Encoding.Default.GetBytes(postData);
request.ContentLength = postBytes.Length;
using (Stream bod = request.GetRequestStream())
{
body.Write(postBytes, 0, postBytes.Length);
}
response = request.GetResponse();
requestStream = response.GetResponseStream();
streamReader = new StreamReader(requestStream);
webBrowser.DocumentText = streamReader.ReadToEnd();
response.Close();
I want to login to website https://ssl.aukro.ua/enter_login.php with following credential parsing and stackoverflow1
I am using following code and it is not working
HttpWebRequest http = WebRequest.Create("https://ssl.aukro.ua/enter_login.php") as HttpWebRequest;
http.KeepAlive = true;
http.Method = "POST";
http.ContentType = "application/x-www-form-urlencoded";
string postData = "user_login=" + "parsing" + "&user_password=" + "stackoverflow1";
byte[] dataBytes = UTF8Encoding.UTF8.GetBytes(postData);
http.ContentLength = dataBytes.Length;
using (Stream postStream = http.GetRequestStream())
{
postStream.Write(dataBytes, 0, dataBytes.Length);
}
HttpWebResponse httpResponse = http.GetResponse() as HttpWebResponse;
// Probably want to inspect the http.Headers here first
http = WebRequest.Create("http://aukro.ua/myaccount/bid.php") as HttpWebRequest;
http.CookieContainer = new CookieContainer();
http.CookieContainer.Add(httpResponse.Cookies);
HttpWebResponse httpResponse2 = http.GetResponse() as HttpWebResponse;
Inspection of the POST when logging in reveals several other parameters:
cod OGZkZlVlNmJk
global_login_hash 83a8ead80c5c544c86c51ab9914db0ab891d7223
session OWFhMANRVl5dVlVUVFoDCAlTBVFQB1QNDVZQVlFTU11cUAVSVFMDDwpRVVVTVgdZCFZSA1JSYmMyOA==
session_login_hash 38d1a6b20f20d7cb7a8cf93d7f3048087d8c9ffb
url ODNiOF5HRxICHE1PQUQdA01YEFcYRlI2MzNi
user_login test
user_password test
version A
You'll need to parse these out of the login page and add them to your post.
I have figured out(by inspecting both the website its-self and the response form the website when using my C# application) that the website in question does not use the form's nor cookies to do the secure connections so I am wondering if anyone knows of a way to potentially use the SSL certificate or the website headers(which by all accounts I think do have a cookie in them), to allow the log-in and then download of a certain file.
Could I get code examples or links to ways to use the headers to log-in to a site is what I am asking, I have all the necessary credentials just not the extensive knowledge to use them.
Thanks a bunch.
public void downloadStuff()
{
string formUrl = "https://secure.website.co.nz/extension/Login.aspx";
string formParams = string.Format("ctl00_main_Login1_UserName={0}&ctl00_main_Login1_Password={1}", "*********", "************");
string cookieHeader;
WebRequest req = WebRequest.Create(formUrl);
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
byte[] bytes = Encoding.ASCII.GetBytes(formParams);
req.ContentLength = bytes.Length;
using (Stream os = req.GetRequestStream())
{
os.Write(bytes, 0, bytes.Length);
}
WebResponse resp = req.GetResponse();
cookieHeader = resp.Headers["Set-cookie"];
string pageSource;
string getUrl = "https://secure.website.co.nz/extension/Downloader.axd?x=stock-all";
WebRequest getRequest = WebRequest.Create(getUrl);
getRequest.Headers.Add("Cookie", cookieHeader);
WebResponse getResponse = getRequest.GetResponse();
using (StreamReader sr = new StreamReader(getResponse.GetResponseStream()))
{
WebClient exeed = new WebClient();
exeed.DownloadFileAsync(new Uri(#"https://secure.website.co.nz/extension/Downloader.axd?x=stock-all"), "test.txt");
pageSource = sr.ReadToEnd();
}
}
Note: This question was also put up because if what I have done is correct, I would not know how to use a cookie to download a file. Potentially example code of how to capture and use a cookie and or a header would be the best option.
Thanks, This was an edit!