Example #1
0
        public override bool Login()
        {
            CookieContainer cookieJar = new CookieContainer();
            _client = new CookieAwareWebClient(cookieJar);
            _client.Referer = HOME_URL;

            // the website sets some cookie that is needed for login
            string response = _client.DownloadString(HOME_URL);

            Cookie tokenCookie = cookieJar.List().FirstOrDefault(c => c.Name == "csrftoken");

            StringBuilder postData = new StringBuilder();
            postData.Append("email=" + HttpUtility.UrlEncode(Username) + "&");
            postData.Append("password="******"&");
            postData.Append("remember=False=");

            // the csrf token is sent in the hader
            _client.Headers.Add("User-Agent", USER_AGENT);
            _client.Headers.Add("Accept", "application/json, text/javascript, */*; q=0.01");
            _client.Headers.Add("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
            _client.Headers.Add("Referer", HOME_URL);
            _client.Headers.Add("X-Requested-With", "XMLHttpRequest");
            _client.Headers.Add("X-CSRFToken", tokenCookie.Value);

            response = _client.UploadString(LOGIN_API, postData.ToString());
            JObject jObject = JObject.Parse(response);
            JToken jToken = jObject.GetValue("success");
            if (!jToken.Value<bool>())
            {
                //The query returned false, either not authenticated or forbiddent (403)
                Console.WriteLine("Wrong email or password logging into Edx.");
                return false;
            }

            //Now get the goods (cookies should be set!)

            return true;
        }