简体   繁体   中英

Automate logging in to website using c#

I want to login to a website every 5 hours to automate some tasks. I am having trouble login in because the website uses https.

I want to login to https://www.dealercentral.net . If I use fiddler meanwhile logging in this is the request and response that I captured where the username and password are being sent (note I modified the username and password for security reason otherwise you will be able to login to my account lol):

Request captured by fidler:

POST https://www.dealercentral.net/_layouts/15/dealercentral/login.aspx?ReturnUrl=%2f_layouts%2f15%2fAuthenticate.aspx%3fSource%3d%252F&Source=%2f HTTP/1.1
Host: www.dealercentral.net
Connection: keep-alive
Content-Length: 439
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Origin: https://www.dealercentral.net
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Referer: https://www.dealercentral.net/_layouts/15/dealercentral/login.aspx?ReturnUrl=%2f_layouts%2f15%2fAuthenticate.aspx%3fSource%3d%252F&Source=%2F
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8
Cookie: ASP.NET_SessionId=52lhv2bm45z5xx0kylecwzco; WSS_FullScreenMode=false

__LASTFOCUS=&__VIEWSTATE=%2FwEPBSpWU0tleTo3NDM1NGQ1ZS1iYjBkLTQ2ODktYTU4Mi0xMDgzNTljY2ZjY2Zk7GQ77o%2B8NvANoTxtWC0wkDqyACylBde9E2t7U%2BOXkCY%3D&__EVENTTARGET=&__EVENTARGUMENT=&__EVENTVALIDATION=%2FwEdAATYiJ%2B%2Fud3KAWRxdbdx6iTIIRPZSl6UajYNUBbXmOsn5sIjdzfOst76JdbTNlFHv6h6%2Ff%2BTYHJ2sj56xvBVZVdouyYzuVuGagw4zrs8XpBj6eWYF7phR6ki3BEBwJxSZQ0%3D&signInControl%24UserName=USERNAME_I_MODIFIED_THIS_ON_PURPOSE&signInControl%24password=_PASSWORD_THAT_I_ENTERED_&signInControl%24login=Sign+In

Response capured by fiddler

HTTP/1.1 302 Found
Cache-Control: private, no-store
Content-Type: text/html; charset=utf-8
Location: /_layouts/15/Authenticate.aspx?Source=%2F
Server: Microsoft-IIS/8.0
X-SharePointHealthScore: 0
X-AspNet-Version: 4.0.30319
Set-Cookie: FedAuth=77u/PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz48U1A+MCMuZnxsZGFwbWVtYmVyfHBhc3RvcmEsMCMuZnxsZGFwbWVtYmVyfHBhc3RvcmEsMTMwODQ4NDA2NjAyMzk1OTk4LEZhbHNlLGZDOXlxeGI0OWEzWXNaSFZHNzB1Mzh0Z0xwQnlJVTEzUEtYS1FmUTBSNmRqU2NpdldQZHQ1eVNyODAvWTdCaG4vcnpWd2t4emdqVDhMM1FNZVFranVpV1p4bXFHY0VsbGdVNWtvSHMwWnUwRjF3UmQxTU5GVGRsSXV1Z2lYbmZrMUNaVlpHckVXZ0F1NzN4WHJvRzVWVEhFVXhHeDh0R29tTlFBd0pzakI1QzE2SGtFUG5YcFc2Z01mejJJeW9lN3VtcW9mSjg2U1hHS2hvaVpJR1lQa0J2YUQ1Z2NiYlNwVWVnZ1lwd3F4TDdMU3NCRWs5d0w1YTNnOGVTZlBhT1RsMjBCWnIvbkZIbU8vRmlSVEFXeUd4TThUcmd4dUhxTG44Q2tacGNqMGNEek45RWd6T2dLcy8zYVZNS2V0aWs0UTRaREJyd0I2RTI5bjJQck9hMk1zQT09LGh0dHBzOi8vd3d3LmRlYWxlcmNlbnRyYWwubmV0LzwvU1A+; path=/; secure; HttpOnly
SPRequestGuid: e34f259d-4b05-104d-f0d8-cf3b39ea9ef5
request-id: e34f259d-4b05-104d-f0d8-cf3b39ea9ef5
X-FRAME-OPTIONS: SAMEORIGIN
SPRequestDuration: 201
SPIisLatency: 0
X-Powered-By: ASP.NET
MicrosoftSharePointTeamServices: 15.0.0.4551
X-Content-Type-Options: nosniff
X-MS-InvokeApp: 1; RequireReadOnly
Date: Tue, 18 Aug 2015 21:57:39 GMT
Content-Length: 158

<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="/_layouts/15/Authenticate.aspx?Source=%2F">here</a>.</h2>
</body></html>

If I where to have __LASTFOCUS=&__VIEWSTATE=%2FwEPBSpWU0tleTo3ND.... etc and all the cookies I will be able to sign in. Once logged in I know what request I have to made in order to parse the data that I need. Its just I am having trouble logging in. I am also doing this to learn. I know there are a lot of web automators out there.


I know how to sign in with fiddlers help (If I copy the headers and posted data that was captured using fiddler sign in works!):

WebClient c = new WebClient();
c.Headers.Add("Cookie", "ASP.NET_SessionId=52lhv2bm45z5xx0kylecwzco; WSS_FullScreenMode=false");
// add other headers captured by fiddler
var html = c.UploadString("https://www.dealercentral.net", "__LASTFOCUS=&__VIEWSTATE=%2FwEPBSpWU0tleTo3NDM1NGQ1ZS1iYjBkLTQ2ODktYTU4Mi0xMDgzNTljY2ZjY2Zk7GQ77o%2B8NvANoTxtWC0wkDqyACylBde9E2t7U%2BOXkCY%3D&__EVENTTARGET=&__EVENTARGUMENT=&__EVENTVALIDATION=%2FwEdAATYiJ%2B%2Fud3KAWRxdbdx6iTIIRPZSl6UajYNUBbXmOsn5sIjdzfOst76JdbTNlFHv6h6%2Ff%2BTYHJ2sj56xvBVZVdouyYzuVuGagw4zrs8XpBj6eWYF7phR6ki3BEBwJxSZQ0%3D&signInControl%24UserName=USERNAME_I_MODIFIED_THIS_ON_PURPOSE&signInControl%24password=_PASSWORD_THAT_I_ENTERED_&signInControl%24login=Sign+In");

The response is ok and the login is successful.

Your login url is:

https://www.dealercentral.net/_layouts/15/dealercentral/login.aspx?ReturnUrl=%2f_layouts%2f15%2fAuthenticate.aspx%3fSource%3d%252F&Source=%2f

So, when login completes, the server response is:

Object moved to <a href="/_layouts/15/Authenticate.aspx?Source=%2F">here</a>.

which means the server is redirecting to that url because your login is ok.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM