0

I am trying to figure out how to make my C# application login to my website application. I have absolutely 0 idea where to begin. Normally on the website the user just enters user and password and checks or doesn't check remember me button and clicks login. My python framework verifies the login, and then sets a cookie in the header of the response to save the login cookie. When a user tries visiting a page it checks to see if it can find the cookie, and if it does then it keeps the user logged in.

I have absolutely 0 idea how I would go about doing something like this in the desktop C# application. Can someone point me in the right direction?

Thanks

MasterGberry
  • 2,800
  • 6
  • 37
  • 56

2 Answers2

1

I am trying to figure out how to make my desktop C# application login to my website application

Use the WebClient Class.

string string loginData = "username=***&passowrd=***&next=/hurt/";

WebClient wc = new WebClient();

wc.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5");
wc.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
wc.Headers.Add("Accept-Encoding", "identity");
wc.Headers.Add("Accept-Language", "en-US,en;q=0.8");
wc.Headers.Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.3");
wc.Headers.Add("ContentType", "application/x-www-form-urlencoded");
string response = wc.UploadString("http://xyz.com/accounts/login/", "POST", loginData);
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • How would I handle the response stuff? How do I handle keeping the cookie returned in the response so the user is logged in until the application closes per say? Do i need to send some kind of thing back and forth saying that this user is the user who logged in? – MasterGberry Feb 21 '13 at 03:29
  • If you imagine it being an invisible web browser, once it logs in your `python framework verifies the login, and then sets a cookie in the header of the response to save the login cookie.` So the second time you go to http://xyz.com it shouldn't require another login. – Jeremy Thompson Feb 21 '13 at 03:43
  • So does the WebClient manage all the cookies and such on each request like a normal web browser? So on the next request it would pass the cookie received from the first response? – MasterGberry Feb 21 '13 at 03:44
  • http://stackoverflow.com/questions/1777221/using-cookiecontainer-with-webclient-class – Jeremy Thompson Feb 21 '13 at 03:51
0

If you want to simulate a browser, then use COM. Example below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

// Add a reference to "C:\Windows\System32\shdocvw.dll"

namespace BrowserAutomation
{
    class Program
    {   
        static void Main(string[] args)
        {
            var ie = new SHDocVw.InternetExplorer();
            // Ensure that ie.Quit() is called at the end via the destructor
            var raii = new IE_RAII(ie);
            // Just so you can see what's going on for now
            ie.Visible = true;
            ie.Navigate2("http://www.wellsfargo.com");
            var document = GetDocument(ie);
            var userid = document.getElementById("userid");
            userid.Value = "billy.everyteen";
            var password = document.getElementById("password");
            password.Value = "monroebot";
            var form = document.Forms("signon");
            form.Submit();
            // Hang out for a while...
            System.Threading.Thread.Sleep(10000);
        }
        // Poor man's check and wait until DOM is ready
        static dynamic GetDocument(SHDocVw.InternetExplorer ie)
        {
            while (true)
            {
                try
                {
                    return ie.Document;
                }
                catch (System.Runtime.InteropServices.COMException e)
                {
                    if (e.Message != "Error HRESULT E_FAIL has been returned " +
                                     "from a call to a COM component.")
                    {
                        throw e;
                    }
                }
                System.Threading.Thread.Sleep(1000);
            }
        }
    }
    class IE_RAII
    {
        public IE_RAII(SHDocVw.InternetExplorer ie)
        {
            _ie = ie;
        }
        ~IE_RAII()
        {
            _ie.Quit();
        }
        private SHDocVw.InternetExplorer _ie;
    }
}
pyrospade
  • 7,870
  • 4
  • 36
  • 52