public async Task ApplyConfiguration(JToken configJson) { var config = new ConfigurationDataBasicLoginAnimeBytes(); config.LoadValuesFromJson(configJson); // Get the login form as we need the CSRF Token var loginPage = await webclient.GetString(new Utils.Clients.WebRequest() { Url = LoginUrl }); CQ loginPageDom =loginPage.Content; var csrfToken = loginPageDom["input[name=\"csrf_token\"]"].Last(); // Build login form var pairs = new Dictionary<string, string> { { "csrf_token", csrfToken.Attr("value") }, { "username", config.Username.Value }, { "password", config.Password.Value }, { "keeplogged_sent", "true" }, { "keeplogged", "on" }, { "login", "Log In!" } }; var content = new FormUrlEncodedContent(pairs); // Do the login var response = await webclient.GetString(new Utils.Clients.WebRequest() { Cookies = loginPage.Cookies, PostData = pairs, Referer = LoginUrl, Type = RequestType.POST, Url = LoginUrl }); // Follow the redirect if (response.Status == HttpStatusCode.RedirectMethod) { cookieHeader = response.Cookies; response = await webclient.GetString(new Utils.Clients.WebRequest() { Url = SearchUrl, PostData = pairs, Referer = SiteLink.ToString(), Cookies = cookieHeader }); } if (!response.Content.Contains("/user/logout")) { // Their login page appears to be broken and just gives a 500 error. throw new ExceptionWithConfigData("Failed to login, 6 failed attempts will get you banned for 6 hours.", (ConfigurationData)config); } else { AllowRaws = config.IncludeRaw.Value; var configSaveData = new JObject(); configSaveData["cookies"] = cookieHeader; configSaveData["raws"] = AllowRaws; SaveConfig(configSaveData); IsConfigured = true; } }
public async Task ApplyConfiguration(JToken configJson) { var config = new ConfigurationDataBasicLoginAnimeBytes(); config.LoadValuesFromJson(configJson); // Get the login form as we need the CSRF Token var loginPage = await client.GetAsync(LoginUrl); CQ loginPageDom = await loginPage.Content.ReadAsStringAsync(); var csrfToken = loginPageDom["input[name=\"csrf_token\"]"].Last(); // Build login form var pairs = new Dictionary<string, string> { { "csrf_token", csrfToken.Attr("value") }, { "username", config.Username.Value }, { "password", config.Password.Value }, { "keeplogged_sent", "true" }, { "keeplogged", "on" }, { "login", "Log In!" } }; var content = new FormUrlEncodedContent(pairs); // Do the login var response = await client.PostAsync(LoginUrl, content); var responseContent = await response.Content.ReadAsStringAsync(); // Compatiblity issue between the cookie format and httpclient // Pull it out manually ignoring the expiry date then set it manually // http://stackoverflow.com/questions/14681144/httpclient-not-storing-cookies-in-cookiecontainer IEnumerable<string> cookies; if (response.Headers.TryGetValues("set-cookie", out cookies)) { foreach (var c in cookies) { cookieContainer.SetCookies(new Uri(BaseUrl), c.Substring(0, c.LastIndexOf(';'))); } } foreach (Cookie cookie in cookieContainer.GetCookies(new Uri(BaseUrl))) { if (cookie.Name == "session") { cookie.Expires = DateTime.Now.AddDays(360); break; } } // Get the home page now we are logged in as AllowAutoRedirect is false as we needed to get the cookie manually. response = await client.GetAsync(BaseUrl); responseContent = await response.Content.ReadAsStringAsync(); if (!responseContent.Contains("/user/logout")) { throw new ExceptionWithConfigData("Failed to login, 6 failed attempts will get you banned for 6 hours.", (ConfigurationData)config); } else { AllowRaws = config.IncludeRaw.Value; var configSaveData = new JObject(); cookieContainer.DumpToJson(SiteLink, configSaveData); configSaveData["raws"] = AllowRaws; if (OnSaveConfigurationRequested != null) OnSaveConfigurationRequested(this, configSaveData); IsConfigured = true; } }