C # impersonate a site user login

Source: Internet
Author: User
Tags session id sdo unique id ticket server memory

When we write the irrigation robot, grasping the resource robot and the Web Games Auxiliary tool, the first step is to realize the user login. So how do you use C # to emulate a user's login pull? To implement a user's login, you must first understand how the general Web site determines whether the user is logged in.

The HTTP protocol is a non-connected protocol, that is, the content and state of the conversation is irrelevant to the last time, in order to achieve and the user's persistent interaction, the site and the browser before the beginning of the session in the server memory will be established a session, the session identified the user (browser), Each session has a unique ID, and the server will pass the generated ID to the browser the first time it is established, and the browser will include the SessionID in every request sent to the server in the next browsing, thus identifying its identity.

The server uses memory to save the information in the session, so what does the browser use to save the server-assigned SessionID? Yes, it's a cookie. The browser will not include SessionID in the cookie when it is first established, and the server will consider it a completely new session, allocating a memory on the server to the session, and the session ID in the HTTP Headers are sent to the browser using Set-cookie.

Now that the principle has been made clear, then we will be to achieve a website login, here is a grand sweep through login as an example.

To write this protocol-oriented network program, the capture tool is not necessary, we first want to use the capture tool to analyze the content sent and received in the ordinary browser to log in to the use of C # to simulate the browser contract. Grab a lot of tools, see personal hobbies, I mainly use HTTP Analyzer, specifically for the HTTP, too strong grasping the package of the tool to what the package is not good for our analysis.

1. It is best to clear all the cookie records of IE, in order to avoid the impact of packet analysis, and then open the Packet capture program.

2. Enter http://zh.sdo.com/web1.0/home/fastlogin.asp This fast login address in IE and we will see the packages that have caught a lot of requests and responses.

3. Enter the user name and password, click Login, ie in the normal login, stop grasping the packet, all the information we want to be crawled well.

4. The grand login mechanism is still relatively complex, the middle involves several servers, after analysis (this is a relatively lengthy process, specific analysis of the site, the analysis process I will not write) sweep through login mechanism:

1) IE request https://cas.sdo.com:80/cas/login?service=http://zh.sdo.com/web1.0/home/ Index.asp page, this page to ie a SessionId, such as set-cookie:asp.net_sessionid=avcbse55l5e03suqi4dx3555; path=/

2) IE at the same time in the body of the HTTP get a ticket, this ticket will be useful in the login, of course, other sites certainly do not do this, the analysis is sweep through. Location.href = http://www.sdo.com/login2.asp?lt=sd-1420e593-d2cf-4c9c-b249-07fe27932a21-2008-05-06_01%3a25% 3a41.484&service=http%3a%2f%2fzh.sdo.com%2fweb1.0%2fhome%2ffastlogin.asp%3ftest%3d1; Here The LT parameter is what I said ticket.

3) will get the LT, username, password and some other unimportant parameters are post to https://cas.sdo.com:80/cas/Login.PostTarget.aspx?service=http://zh.sdo.com/ In web1.0/home/fastlogin_after.asp, the data of the post that is captured specifically is:warn=false&_eventid=submit&idtype=0& gamearea=0& gametype=0&challenge=3623&lt=sd-1420e593-d2cf-4c9c-b249-07fe27932a21-2008-05-06_01%3a25%3a41.484& username=studyzy& amp;password=1234&ekey=&challenge=3623, here we are only concerned about Lt,username,password these three parameters.

4) Get a page that can only be accessed after logging in and test for successful login.

5. OK, the entire login mechanism we have finished analysis, the next is to consider the implementation of the Code. On the HTTP protocol, C # has WebRequest, WebResponse, HttpWebRequest, and HttpWebResponse classes. We are mainly based on these classes, of course, completely based on the socket programming is also possible, but there is no need for this.

The way to get HTML for a page without setting a cookie or postdata is simple:

public static string gethtml (String URL)
{
WebRequest wrt;
WRT = WebRequest.Create (URL);
Wrt. Credentials = CredentialCache.DefaultCredentials;
WebResponse WRP;
WRP = wrt. GetResponse ();
return new StreamReader (WRP. GetResponseStream (), Encoding.default). ReadToEnd ();
}

If you need to get the cookie returned by the server, you can go through WRP. Headers.get ("Set-cookie") method to get.

If you need to add a cookie to the request for additional post data, it is also very simple, just set the contentlength and request stream in the HttpWebRequest object.

Httpwebrequest.contentlength = Byterequest.length;
Stream stream;
stream = Httpwebrequest.getrequeststream ();
Stream. Write (byterequest, 0, byterequest.length);
Stream. Close ();

Code I'm not all posted, I made a demo in the annex, we are interested in the study can be looked at. Loginsdodemo.rar

Code to achieve a grand account of the login, in fact, sweep through login has not been completed, followed by the choice of specific server, the ticket to the specific server to verify, the principle is the same, I am no longer tired of the statement.

After the successful login, we only need to follow the cookie every time, the server is considered to be logged in the user operation, the next can be casually irrigation, download resources, specifically to do what you have to do, just to operate in IE, grab the packet analysis, with C # Implementing the same contract is ok!

Report:


1 private void Form1_Load (object sender, EventArgs e)
2 {
3
4 string username = "XXXX";//user name
5 String password = "xxxx";//Password
6//Create a new container for storing cookies
7 Cookiecontainer container = new Cookiecontainer ();
8//Splicing post data
9 String postdata = ("username=" + username);
Ten PostData + = ("&passwd=" + password);
One postdata + = ("&LOGIN=%B5%C7%A1%A1%C2%BC");
ASCIIEncoding encoding = new ASCIIEncoding ();
byte[] data = encoding. GetBytes (PostData);
HttpWebRequest request = (HttpWebRequest) webrequest.create ("http://xxxx/xxxx/login.asp");
Request. Method = "Post";
Request. ContentType = "application/x-www-form-urlencoded";
Request. ContentLength = data. Length;
Request. KeepAlive = true;
Request. Cookiecontainer = container; The returned cookie is appended to this container.
20//Send data
Stream newstream = Request. GetRequestStream ();
Newstream.write (data, 0, data. Length);
Newstream.close ();
24//The following two sentences are indispensable
HttpWebResponse response = (HttpWebResponse) request. GetResponse ();
Response. Cookies = container. GetCookies (Request. RequestUri);
27
HttpWebRequest Requestscore = (HttpWebRequest) webrequest.create ("http://xxxx/xxxx/Score.asp");
PostData = "Term=&termlist=%c7%eb%d1%a1%d4%f1&ckind=&lwpagesize=100&lwbtnquery=%b2%e9%d1%af";
data = Encoding. GetBytes (PostData);
Requestscore.method = "Post";
Requestscore.contenttype = "application/x-www-form-urlencoded";
Requestscore.contentlength = data. Length;
Requestscore.keepalive = true;
35
36//Use of logged-in cookies for subsequent verification
Panax Notoginseng Requestscore.cookiecontainer = container;
A. Stream stream = Requestscore.getrequeststream ();
The stream. Write (data, 0, data. Length);
The stream. Close ();
HttpWebResponse Responsesorce = (HttpWebResponse) requestscore.getresponse ();
StreamReader reader = Newstreamreader (Responsesorce.getresponsestream (), encoding.default);
String content = Reader. ReadToEnd ();
TextBox1.Text = content;
45
46}

C # impersonate a site user login

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.