Page 1 of 2

C# Login

PostPosted: Mon Mar 01, 2010 8:37 pm
by ezah
I just started learning C#, i have pretty good programming concepts after about 7 years of experience so i should be able to do this, but i cant. How would i go about logging into HTS with HttpWebRequest?

I assume it uses cookies so i tried importing those, not sure if that's right, i've messed quite a bit with it but it still says im not logged in. At times i do see my username in the source, not logged in, but i do see it and other times i don't.

Here's a super basic part of my code and hopefully one of you can find a mistake i may be making? programatically or with the way HTS logs in.

Code: Select all
CookieContainer cooks = new CookieContainer;
            HttpWebRequest webR = (HttpWebRequest)WebRequest.Create("http://www.hackthissite.org/missions/prog/2");
            webR.CookieContainer = cooks;
            webR.Method = "Post";
            // tried both get and post methods.
            HttpWebResponse webRs = (HttpWebResponse) webR.GetResponse();
            System.IO.StreamReader str = new System.IO.StreamReader( webRs.GetResponseStream());


and dont comment about my naming conventions, i know i just wanted something quick to show :p

Re: C# Login

PostPosted: Tue Mar 02, 2010 12:26 pm
by BhaaL
You need to use a different url to log in, store the cookie, and then submit your answer. The cookie container is correct for the latter, but you still need to go to login first.
And you'll need to write your HTTP to the request stream you obtain from the HttpWebRequest object before getting the response.

Re: C# Login

PostPosted: Tue Mar 02, 2010 6:04 pm
by ezah
Ok, but one how would i go about logging in? i have no experience at all with httpwebrequests. a web client see,s like it would be fine for this but from the research i have found it does not seem possible. also how would i store the cookie? ive tried adding phpsessid and its already included. im just lost with this whole thing, i actually wanna do this for hellbound hackers timed challenges because its not possible to do those in any other way. Thanks alot for the response but if you you can explain that a bit better it would be great.

im not asking for a script or anything, just some more detail.

Re: C# Login

PostPosted: Thu Mar 04, 2010 12:09 pm
by BhaaL
You dont need to take care of cookies yourself, the Cookie Container does that for your; just make sure to include the same container for all requests.
Just call GetRequestStream() on the Request object, and use whatever method you like to write to it (StreamWriter for example). Then, simply close the stream and call GetResponse() to get whatever the server sends back.

Re: C# Login

PostPosted: Thu Mar 04, 2010 4:53 pm
by ezah
Ive tried to write some data, i just dont know the format hts uses for login data, i set method to post and tried posting "username=user&password=pass" and stuff along those lines, i have no network experience but alot of programming but i cant seem to get this and i've found some good sources of info on google. so does anybody know what i should post to login?

also i did this with the webBrowser control and it works but its too slow for what i need this for, with the webBrowser i can use my active session but with this im lost :/ Thanks for the help so far too :]

Re: C# Login

PostPosted: Fri Mar 05, 2010 9:09 am
by BhaaL
It works the same as for any other Website. Maybe google a bit for what you are trying to do (like, POST some form fields in C#) or look at RFC 2616 and related.

Re: C# Login

PostPosted: Fri Mar 09, 2012 10:46 pm
by prizzy
Hey ezah, did you get this to work? I am learning programming c# via the challenges, and would like to make it turn key.

Re: C# Login

PostPosted: Sat Mar 10, 2012 12:28 am
by centip3de
prizzy wrote:Hey ezah, did you get this to work? I am learning programming c# via the challenges, and would like to make it turn key.

-- Fri Mar 09, 2012 10:46 pm --

Hey ezah, did you get this to work? I am learning programming c# via the challenges, and would like to make it turn key.


This thread is almost over 2 years old. The OP hasn't been on this site since March, 15th, 2010. Please don't necro threads, like these.

Re: C# Login

PostPosted: Sat Mar 10, 2012 6:00 pm
by prizzy
Not meaning to necro any threads. I figured I would got with the one that's started, instead of starting a new one. Perhaps I am not the only one working on this.

anyway..

I have found a way to do it with c#, but I don't like it. I want to complete the programming missions (the ones possible) with one click. Have the program, log into site, download info (picture, xml file, etc.), do the work, then submit it back. I don't like opening a webbrowser control to do it though.

Code: Select all
webBrowser1.Navigate("www.hackthissite.org/user/login");

            while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
            {
                MessageBox.Show("Loaded");

                HtmlDocument document = webBrowser1.Document;
                HtmlElement inputValue = document.GetElementById("login_username");
                inputValue.SetAttribute("value", "username");
                HtmlElement inputValuep = document.GetElementById("login_password");
                inputValuep.SetAttribute("value", "password");
              //  HtmlElement submitButton = document.GetElementById("submit");
               // HtmlElement submitButton =
                HtmlElementCollection tags = document.GetElementsByTagName("input");
                HtmlElement submitButton;

                foreach (HtmlElement tag in tags)
                {
                    richTextBox1.AppendText(tag.GetAttribute("type").ToString());
                    if (tag.GetAttribute("type").ToString() == "submit")
                    {
                        submitButton = tag;
                        submitButton.InvokeMember("click");
                    }
                }

Re: C# Login

PostPosted: Sat Mar 10, 2012 7:46 pm
by Mars7411
prizzy wrote:Not meaning to necro any threads. I figured I would got with the one that's started, instead of starting a new one. Perhaps I am not the only one working on this.

anyway..

I have found a way to do it with c#, but I don't like it. I want to complete the programming missions (the ones possible) with one click. Have the program, log into site, download info (picture, xml file, etc.), do the work, then submit it back. I don't like opening a webbrowser control to do it though.

Moron, can you read? This thread is nearly two years old. Don't post in this thread. Start a new thread.