I am trying to connect to a website programatically, and have the following basic code so far but the responseString just returns the websites Login page in html and doesn't actually login to the website:
var client = new HttpClient();
var values = new Dictionary<string, string>
{
{ "username", "[email protected]" },
{ "password", "pass1234" }
};
var content = new FormUrlEncodedContent(values);
var response = await client.PostAsync("https://www.website.com/Portal/open/Logon.aspx", content);
var responseString = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseString);
The Request and Form Data when I log in through the actual website is shown below:
The Request and Form Data is shown below:
Request Header
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Cache-Control: max-age=0
Connection: keep-alive
Content-Length: 991
Content-Type: application/x-www-form-urlencoded
Cookie: ASP.NET_SessionId=ABCD
Host: www.website.com
Origin: https://www.website.com
Referer: https://www.website.com/Portal/open/Logon.aspx
Sec-Fetch-Dest: document
Sec-Fetch-Mode: navigate
Sec-Fetch-Site: same-origin
Sec-Fetch-User: ?1
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36
Form Data
__LASTFOCUS:
__EVENTTARGET:
__EVENTARGUMENT:
__VIEWSTATE: /ABCC
__VIEWSTATEGENERATOR: 123456
__SCROLLPOSITIONX: 0
__SCROLLPOSITIONY: 0
__EVENTVALIDATION: /ABCD
chatQueueId: 127
chatPartition: 258TEL
chatDisplayType: dialog
chatTitle: WebLink Support
guestName: Guest
ctl00$ContentPlaceHolder1$Login1$UserName: [email protected]
ctl00$ContentPlaceHolder1$Login1$Password: pass1234
ctl00$ContentPlaceHolder1$Login1$LoginButton: Log In
I read that there are numerous ways to do this but I haven't figured it out, hence this post.
I have gone through the suggestions below but getting a "NotFound" response and I think it is due to the javascript call below in the form.
<input type="submit" name="ctl00$ContentPlaceHolder1$Login1$LoginButton" value="Log In" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$ContentPlaceHolder1$Login1$LoginButton", "", true, "", "", false, false))" id="ctl00_ContentPlaceHolder1_Login1_LoginButton" class="submitBtn" />
Thank you for the responses so far.
ctl00$ContentPlaceHolder1$Login1$UserName: [email protected])