protected void Button2_Click(object sender, EventArgs e) { // only if image verifier is working if (Session["generatedString"].Equals(TextBox3.Text)) { // get path string filepath = HttpRuntime.AppDomainAppPath + @"\App_Data\members.xml"; // save info string user = TextBox1.Text; string password = TextBox2.Text; // ensures textboxes are filled if (!String.IsNullOrWhiteSpace(user) && !String.IsNullOrWhiteSpace(password)) { // encrypt password before written to xml doc Cryption.ServiceClient client = new Cryption.ServiceClient(); string en_pass = client.Encrypt(password); // new xml doc XmlDocument doc = new XmlDocument(); // open file doc.Load(filepath); // get root node XmlElement rootElement = doc.DocumentElement; // for each self-registered member foreach (XmlNode node in rootElement.ChildNodes) { // if name matches, do not re-register if (node["name"].InnerText == user) { Label1.Text = String.Format("The username {0} already exists, please try again", TextBox1.Text); return; } } // add a new member XmlElement myMember = doc.CreateElement("member", rootElement.NamespaceURI); rootElement.AppendChild(myMember); XmlElement myUser = doc.CreateElement("name", rootElement.NamespaceURI); myMember.AppendChild(myUser); myUser.InnerText = user; // pasword XmlElement myPwd = doc.CreateElement("pwd", rootElement.NamespaceURI); myMember.AppendChild(myPwd); myPwd.InnerText = en_pass; // save changes doc.Save(filepath); Response.Redirect("Default.aspx"); } // error handling else { Label1.Text = "Please enter username/password"; } } else { // error message Label1.Text = "The string entered does not match the image. Please Try again!"; } }
protected void Button1_Click(object sender, EventArgs e) { // cookies saved to 'key' HttpCookie myCookie = new HttpCookie("key"); // create filepath string filepath = HttpRuntime.AppDomainAppPath + @"\App_Data\members.xml"; // get user input string user = TextBox1.Text; string password = TextBox2.Text; // error handling, checking if user input empty if (!string.IsNullOrWhiteSpace(user) && !string.IsNullOrWhiteSpace(password)) { // client for encryption service Cryption.ServiceClient client = new Cryption.ServiceClient(); // open xml doc and load from filepath XmlDocument doc = new XmlDocument(); doc.Load(filepath); XmlElement rootElement = doc.DocumentElement; // for each member in members.xml foreach (XmlNode node in rootElement.ChildNodes) { // name matches if (node["name"].InnerText == user) { string temp = node["pwd"].InnerText; temp = client.Decrypt(temp); // encrypted password matches user password if (temp == password) { Label1.Text = String.Format("Success", TextBox1.Text); FormsAuthentication.RedirectFromLoginPage(user, false); Session["Username"] = user; Session["Password"] = password; myCookie["Username"] = user; myCookie["Password"] = password; myCookie.Expires = DateTime.Now.AddDays(1); Response.Cookies.Add(myCookie); Response.Redirect("Account/Members.aspx"); return; } } } } Label1.Text = "Invalid Username or Password. Please Try Again."; }