protected void btnRegister_Click(object sender, EventArgs e) { string userFilePath = Path.Combine(Request.PhysicalApplicationPath, @"App_Data\userDetails.xml"); FileStream FS = new FileStream(userFilePath, FileMode.Open, FileAccess.ReadWrite); try { lblResult.Visible = false; bool bUnique = true; //Ensuring that all mandatory fields are filled. if (txtfName.Text.Length > 0 && txtlName.Text.Length > 0 && txtuName.Text.Length > 0 && txtSiteKey.Text.Length > 0) { //Verifying CAPTCHA //if (Page.IsValid) if (txtCaptcha.Text == txtEnteredCaptcha.Text) { if (txtPassword.Text.ToLower().Equals(txtRPassword.Text.ToLower())) { string SaltandHash = objHash.createHash(txtPassword.Text.Trim()); if (File.Exists(userFilePath)) { XmlDocument xdoc = new XmlDocument(); xdoc.Load(FS); FS.Close(); foreach (XmlNode xmlnode in xdoc.SelectNodes("USERS/User/UserName")) { //Checking if the user name exists if (txtuName.Text.ToLower().Equals(xmlnode.InnerText.ToLower())) { bUnique = false; break; } } if (bUnique) { //Creating a XML node and appending the required elements/nodes to the Root node XmlNode node = xdoc.CreateNode(XmlNodeType.Element, "User", null); XmlNode firstNameNode = xdoc.CreateElement("FirstName"); firstNameNode.InnerText = txtfName.Text.Trim(); XmlNode lastNameNode = xdoc.CreateElement("LastName"); lastNameNode.InnerText = txtlName.Text.Trim(); XmlNode userNameNode = xdoc.CreateElement("UserName"); userNameNode.InnerText = txtuName.Text.Trim(); XmlNode userType = xdoc.CreateElement("UserType"); userType.InnerText = rbTypeUser.SelectedValue.Trim(); XmlNode passwordSalt = xdoc.CreateElement("SALT"); passwordSalt.InnerText = SaltandHash.Split(':')[0]; XmlNode passwordHash = xdoc.CreateElement("HASH"); passwordHash.InnerText = SaltandHash.Split(':')[1]; XmlNode siteKeyIdx = xdoc.CreateElement("SiteKeyIndex"); siteKeyIdx.InnerText = strSiteKey; XmlNode siteKeyPhrase = xdoc.CreateElement("SiteKeyPhrase"); siteKeyPhrase.InnerText = txtSiteKey.Text.Trim(); node.AppendChild(firstNameNode); node.AppendChild(lastNameNode); node.AppendChild(userNameNode); node.AppendChild(userType); node.AppendChild(passwordSalt); node.AppendChild(passwordHash); node.AppendChild(siteKeyIdx); node.AppendChild(siteKeyPhrase); xdoc.DocumentElement.AppendChild(node); xdoc.Save(userFilePath); } else { lblResult.Text = "User name has to be unique"; lblResult.Visible = true; } } else { //If the file doesnt exist. string strXml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<USERS>" + "<User>" + "<FirstName>" + txtfName.Text.Trim() + "</FirstName>" + "<LastName>" + txtlName.Text.Trim() + "</LastName>" + "<UserName>" + txtuName.Text.Trim() + "</UserName>" + "<UserType>" + rbTypeUser.SelectedValue.Trim() + "</UserType>" + "<SALT>" + SaltandHash.Split(':')[0] + "</SALT>" + "<HASH>" + SaltandHash.Split(':')[1] + "</HASH>" + "<SiteKeyIndex>" + strSiteKey + "</SiteKeyIndex>" + "<SiteKeyPhrase>" + txtSiteKey.Text.Trim() + "</SiteKeyPhrase>" + "</User>" + "</USERS>"; File.WriteAllText(userFilePath, strXml); } if (bUnique) { lblResult.Text = "User Successfully Created"; lblResult.Visible = true; Session["USERID"] = txtuName.Text.Trim(); Session["UserType"] = rbTypeUser.SelectedValue.Trim(); //Redirect user to respective page. if (rbTypeUser.SelectedValue.Trim().Equals("Student_User")) { Response.Redirect("StudentHome.aspx", false); } else if (rbTypeUser.SelectedValue.Trim().Equals("Scholar_User")) { Response.Redirect("ScholarHome.aspx", false); } } else { lblResult.Text = "Please select a unique user name"; lblResult.Visible = true; txtuName.Text = ""; txtPassword.Text = ""; txtRPassword.Text = ""; } } else { lblResult.Text = "Passwords dont match"; lblResult.Visible = true; } } else { lblResult.Text = "Captcha Verification Failed"; lblResult.Visible = true; } } else { lblResult.Text = "All fields are mandatory"; lblResult.Visible = true; } } catch (Exception exp) { lblResult.Text = "User creation Failed. Try Again "; lblResult.Visible = true; Console.WriteLine("Exception : " + exp.Message); throw exp; } finally { FS.Close(); } }