protected void Page_Load(object sender, EventArgs e) { Page.Title = "Log In"; //Once user has submitted form, log them into SOHH if (Page.IsPostBack) { //Submit form using provided data HtmlDocumentScraper scraper = new HtmlDocumentScraper("http://forums.projectcovo.com/login.php?do=login", String.Empty); String postData = String.Format("vb_login_username={0}&cookieuser=1&vb_login_password=&s=&securitytoken=guest&do=login&vb_login_md5password={1}&vb_login_md5password_utf={2}", Username, HashedPassword, HashedPasswordUtf); CookieContainer sessionCookies = Session["cookies"] as CookieContainer; scraper.SubmitForm(ref sessionCookies, postData); Session["cookies"] = sessionCookies; //Check status of SOHH login HtmlNode errorNode = scraper.HtmlDocument.DocumentNode.SelectSingleNode("//td[@class='panelsurround']/div[@class='panel']/div/div"); HtmlNode successNode = scraper.HtmlDocument.DocumentNode.SelectSingleNode("//td[@class='panelsurround']"); int usernameIndex = successNode.InnerText.IndexOf(Username, StringComparison.OrdinalIgnoreCase); if (successNode != null && errorNode == null && usernameIndex >= 0) { //Successful login - add Session variables and cookies Session["username"] = successNode.InnerText.Substring(usernameIndex, Username.Length); var authTicket = new FormsAuthenticationTicket( 1, Session["username"].ToString(), DateTime.Now, DateTime.Now.AddHours(3), true, String.Empty); string encryptedTicket = FormsAuthentication.Encrypt(authTicket); HttpCookie authenticationCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket); Response.Cookies.Add(authenticationCookie); Response.SetCookie(new HttpCookie("username", Session["username"].ToString())); CookieCollection sessionCookieCollection = sessionCookies.GetCookies(new Uri(SOHH_HOSTNAME)); foreach (Cookie cookie in sessionCookieCollection) { String cookieName = "SOHH_" + cookie.Name; Response.Cookies[cookieName].Value = cookie.Value; Response.Cookies[cookieName].Expires = DateTime.Now.AddDays(30); } //Redirect back to index Response.Redirect("index.aspx"); } else { //Failed login - explain why errorMessage.Text = errorNode.InnerHtml + "<br/><br/>"; } } }
protected void Page_Load(object sender, EventArgs e) { #region First Visit if (!Page.IsPostBack) { String action = Request.QueryString["do"]; IsNewReply = Request.QueryString["do"] == "newreply"; String webPageUrl = String.Empty; switch (action) { case "newreply": Page.Title = "New Reply"; webPageUrl = String.Format(SOHH_POSTPAGE_URL, Request.QueryString); break; case "editpost": Page.Title = "Edit Post"; webPageUrl = String.Format(SOHH_EDITPAGE_URL, Request.QueryString["p"]); break; case "newthread": Page.Title = "New Thread"; webPageUrl = String.Format(SOHH_NEWTHREAD_URL, Request.QueryString["f"]); break; default: break; } HtmlDocumentScraper scraper = new HtmlDocumentScraper(webPageUrl, String.Empty); CookieContainer cookies = Session["cookies"] as CookieContainer; scraper.LoadWebPage(ref cookies); Session["cookies"] = cookies; message.Text = Server.HtmlDecode(scraper.HtmlDocument.DocumentNode.SelectSingleNode("//td[@class='controlbar']/textarea").InnerText); HtmlNode threadNameNode = scraper.HtmlDocument.DocumentNode.SelectSingleNode("//span[@class='navbar']/a[starts-with(@href, 'showthread.php?p=')]"); if (threadNameNode != null) { ThreadName = threadNameNode.InnerText; } int securityTokenBegin = scraper.HtmlDocument.DocumentNode.InnerText.IndexOf("var SECURITYTOKEN = \""); int securityTokenEnd = scraper.HtmlDocument.DocumentNode.InnerText.IndexOf("\";", securityTokenBegin); SecurityToken = scraper.HtmlDocument.DocumentNode.InnerText.Substring( securityTokenBegin + SECURITYTOKEN_PREFIX_LENGTH, (securityTokenEnd - (securityTokenBegin + SECURITYTOKEN_PREFIX_LENGTH))); String userIdLink = scraper.HtmlDocument.DocumentNode.SelectSingleNode("//a[starts-with(@href, 'member.php?u=')]").Attributes["href"].Value; LoggedInUserId = userIdLink.Substring(userIdLink.IndexOf('=')); //LoggedInUserId = scraper.HtmlDocument.DocumentNode.SelectSingleNode("//input[@name='loggedinuser']").Attributes["value"].Value; //Get Unique Thread Id HtmlNode threadNode = scraper.HtmlDocument.DocumentNode.SelectSingleNode("//input[@name='t']"); if (Request.QueryString["t"] != null && Request.QueryString["t"] != String.Empty) { ThreadId = Request.QueryString["t"]; } else if (threadNode != null) { ThreadId = threadNode.Attributes["value"].Value; } hThreadId.Value = ThreadId; hReferredPostId.Value = ReferredPostId; hPageNumber.Value = Request.QueryString["page"]; hForumId.Value = Request.QueryString["f"]; hSecurityToken.Value = SecurityToken; hLoggedInUserId.Value = LoggedInUserId; hDo.Value = Request.QueryString["do"]; } #endregion First Visit #region Postback else { String action = hDo.Value; String webPageUrl = String.Empty; String postData = String.Empty; switch (action) { case "newreply": webPageUrl = String.Format(SOHH_POSTPAGE_URL, "do=postreply&t=" + hThreadId.Value); postData = String.Format(NEWREPLY_POST_DATA, HttpUtility.UrlEncode(message.Text), hSecurityToken.Value, hThreadId.Value, hLoggedInUserId.Value, hReferredPostId.Value, ddlEmailUpdate.SelectedValue); break; case "editpost": webPageUrl = String.Format(SOHH_EDITPAGE_URL, hReferredPostId.Value); postData = String.Format(UPDATEPOST_POST_DATA, HttpUtility.UrlEncode(message.Text), hSecurityToken.Value, hReferredPostId.Value, ddlEmailUpdate.SelectedValue); break; case "newthread": webPageUrl = String.Format(SOHH_NEWTHREAD_URL, hForumId.Value); postData = String.Format(NEWTHREAD_POST_DATA, HttpUtility.UrlEncode(txtTitle.Text), HttpUtility.UrlEncode(message.Text), hSecurityToken.Value, hForumId.Value, hLoggedInUserId.Value, ddlEmailUpdate.SelectedValue); break; default: break; } HtmlDocumentScraper scraper = new HtmlDocumentScraper(webPageUrl, String.Empty); CookieContainer cookies = Session["cookies"] as CookieContainer; HttpWebResponse response; scraper.SubmitForm(ref cookies, postData, out response); Session["cookies"] = cookies; Response.Redirect("showthread.aspx" + response.ResponseUri.Query); } #endregion Postback }