Example #1
0
        private async Task <string> GetToken(string ticketId)
        {
            var tokenRequest = GenerateTokenVarRequest(ticketId);

            using (WebClientEx wc = new WebClientEx())
            {
                wc.CookieContainer = cookies;
                wc.Proxy           = defaultProxy.AsWebProxy();
                wc.Timeout         = defaultTimeout;

                string tokenData = Encoding.UTF8.GetString(await wc.UploadValuesTaskAsync(Resources.PtcLoginOauth, tokenRequest));

                return(HttpUtility.ParseQueryString(tokenData)["access_token"]);
            }
        }
Example #2
0
        private async Task <string> GetLoginTicket(string username, string password, SessionData sessionData)
        {
            var loginRequest = GenerateLoginRequest(sessionData, username, password);

            using (WebClientEx wc = new WebClientEx())
            {
                wc.CookieContainer = cookies;
                wc.Proxy           = defaultProxy.AsWebProxy();
                wc.Timeout         = defaultTimeout;

                string response = Encoding.UTF8.GetString(await wc.UploadValuesTaskAsync(Resources.PtcLoginUrl, loginRequest));

                var ticketId = ExtractTicketFromResponse(response, wc.ResponseHeaders);

                return(ticketId);
            }
        }
        private void login_button_Click(object sender, EventArgs e)
        {
            if (SessionName != null)
            {
                // Already logged in
                return;
            }

            var wc = new WebClientEx();

            wc.Headers.Add("user-agent", UserAgent);

            var url  = new Uri("https://dpm.mini.pw.edu.pl/?destination=");
            var post = new NameValueCollection
            {
                { "name", username_box.Text },
                { "pass", password_box.Text },
                { "form_id", "user_login_block" }
            };

            // Execute
            wc.UploadValuesTaskAsync(url, post)
            .ContinueWith(task =>
            {
                var cookies = wc.CookieContainer.GetCookies(url).Cast <Cookie>();

                // Gather cookie data
                foreach (var cookie in cookies)
                {
                    if (cookie.Name.StartsWith("SESS", StringComparison.Ordinal))
                    {
                        SessionName   = cookie.Name;
                        SessionCookie = cookie;
                    }
                }

                if (SessionName != null)
                {
                    // Success
                    Invoke((MethodInvoker) delegate
                    {
                        login_panel.Visible = false;
                        info_panel.Visible  = true;

                        username_ro_box.Text = username_box.Text;
                        cookie_ro_box.Text   = SessionName;
                    });
                }
                else
                {
                    // Fail
                    Invoke((MethodInvoker) delegate
                    {
                        var res = MessageBox.Show("Logowanie nie powiodło się\r\nSprawdź czy podany login i hasło są poprawne", "Błąd", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
                        if (res == DialogResult.Retry)
                        {
                            login_button.PerformClick();
                        }
                    });
                }

                wc.Dispose();
            });
        }