Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Xml;
- using System.Xml.Linq;
- using System.Xml.XPath;
- namespace workingtruefalsequiz1.Account
- {
- public partial class WebForm1 : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- Label1.Text = "";
- Label2.Text = "";
- Label3.Text = "";
- Label4.Text = "";
- }
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- string enteredUsername = TextBox1.Text;
- string enteredPassword = Password1.Value;
- if (!File.Exists("C:\\temp\\students.xml"))
- {
- Label1.Text = "No registered users";
- }
- else
- {
- var doc = XDocument.Load("C:\\temp\\students.xml");
- //int descendantsCount = doc.Descendants().Count(); // counts ALL descendants elements
- int childrenCount = doc.Root.Elements().Count(); //number of students
- bool userLoggedIn = false;
- for (int i = 0; i < childrenCount && !userLoggedIn; i++)
- {
- XElement student = doc.Root.Elements("student").ElementAt(i);
- if (student.Element("username").Value.Equals(enteredUsername))
- {
- if (student.Element("password").Value.Equals(enteredPassword))
- {
- Label1.Text = "Logged in";
- userLoggedIn = true;
- Session["boolLoggedIn"] = userLoggedIn;
- Session["username"] = enteredUsername;
- Session["id"] = i;
- Response.Redirect("~/quiz.aspx");
- }
- }
- }
- if (!userLoggedIn)
- {
- Label1.Text = "Not logged in";
- this.Controls.Add(new LiteralControl("<a href='~/Accounts/Register.aspx'>Register now?</a>"));
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment