Ejemplo n.º 1
0
        private void Authenticate()
        {
            var requestBuilder = new HttpRequestBuilder($"{Settings.BaseUrl.Trim().TrimEnd('/')}")
            {
                LogResponseContent = true
            };

            requestBuilder.Method = HttpMethod.POST;
            requestBuilder.Resource("ajax.php?action=login");
            requestBuilder.PostProcess += r => r.RequestTimeout = TimeSpan.FromSeconds(15);

            var authKey = Settings.BaseUrl.Trim().TrimEnd('/');
            var cookies = AuthCookieCache.Find(authKey);

            if (cookies == null)
            {
                AuthCookieCache.Remove(authKey);
                var authLoginRequest = requestBuilder
                                       .AddFormParameter("username", Settings.Username)
                                       .AddFormParameter("password", Settings.Password)
                                       .AddFormParameter("passkey", Settings.Passkey)
                                       .AddFormParameter("keeplogged", "1")
                                       .AddFormParameter("login", "Log In!")
                                       .SetHeader("Content-Type", "multipart/form-data")
                                       .Accept(HttpAccept.Json)
                                       .Build();

                var response = HttpClient.Execute(authLoginRequest);
                var result   = Json.Deserialize <PassThePopcornAuthResponse>(response.Content);

                if (result.Result != "Ok" || string.IsNullOrWhiteSpace(result.Result))
                {
                    Logger.Debug("PassThePopcorn authentication failed.");
                    throw new Exception("Failed to authenticate with PassThePopcorn.");
                }

                Logger.Debug("PassThePopcorn authentication succeeded.");

                cookies = response.GetCookies();
                AuthCookieCache.Set(authKey, cookies, new TimeSpan(7, 0, 0, 0, 0)); // re-auth every 7 days
                requestBuilder.SetCookies(cookies);
            }
            else
            {
                requestBuilder.SetCookies(cookies);
            }
        }
Ejemplo n.º 2
0
        private void Authenticate()
        {
            var requestBuilder = new HttpRequestBuilder($"{Settings.BaseUrl.Trim().TrimEnd('/')}")
            {
                LogResponseContent = true
            };

            requestBuilder.Method = HttpMethod.POST;
            requestBuilder.Resource("login.php");
            requestBuilder.PostProcess += r => r.RequestTimeout = TimeSpan.FromSeconds(15);

            var authKey = Settings.BaseUrl.Trim().TrimEnd('/');
            var cookies = AuthCookieCache.Find(authKey);

            if (cookies == null)
            {
                AuthCookieCache.Remove(authKey);
                var authLoginRequest = requestBuilder
                                       .AddFormParameter("username", Settings.Username)
                                       .AddFormParameter("password", Settings.Password)
                                       .AddFormParameter("keeplogged", "1")
                                       .SetHeader("Content-Type", "multipart/form-data")
                                       .Accept(HttpAccept.Json)
                                       .Build();

                var response = HttpClient.Execute(authLoginRequest);

                cookies = response.GetCookies();

                AuthCookieCache.Set(authKey, cookies);
            }

            var index = GetIndex(cookies);

            if (index == null || index.Status.IsNullOrWhiteSpace() || index.Status != "success")
            {
                Logger.Debug("Gazelle authentication failed.");
                AuthCookieCache.Remove(authKey);
                throw new Exception("Failed to authenticate with Gazelle.");
            }

            Logger.Debug("Gazelle authentication succeeded.");

            Settings.AuthKey = index.Response.Authkey;
            Settings.PassKey = index.Response.Passkey;
        }