Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what's the proper way to write a .NET application that automatically logs in to www.yahoo.com?

The following code doesn't log in to yahoo. How should it be re-written?

(of course, "username" and "password" would be replaced with my actual account name & password.)

    static void Main(string[] args)
    {
        string input = string.Format("username={0}&passwd={1}", "<username>", "<password>");

        WebRequest request = HttpWebRequest.Create("https://login.yahoo.com/config/login");
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";

        StreamWriter writer = new StreamWriter(request.GetRequestStream());
        writer.Write(input);
        writer.Close();

        StreamReader reader = new StreamReader(request.GetResponse().GetResponseStream());

        string x = reader.ReadToEnd();

        Console.Read();
    }
like image 855
John Smith Avatar asked Sep 13 '12 00:09

John Smith


1 Answers

When having a look at the html source of the yahoo page, you'll notice there are many hidden fields

image showing the hidden fields of the form

which are used to protect the user, for example against CSRF

It may be, you will habe to firstly send a request to yahoo to get a valid anti CSRF token to then include it into your request. You'll also have to have a look into the javascript the site uses. Maybe there is something calculated on client site, and then send with the login data.

Be aware since those sites often change and maybe tomorrow you're implementation won't work.

like image 97
GameScripting Avatar answered Oct 15 '22 05:10

GameScripting