Guest User

Login.aspx.cs

a guest
May 9th, 2016
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.34 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Xml;
  9. using System.Xml.Linq;
  10. using System.Xml.XPath;
  11.  
  12. namespace workingtruefalsequiz1.Account
  13. {
  14.     public partial class WebForm1 : System.Web.UI.Page
  15.     {
  16.         protected void Page_Load(object sender, EventArgs e)
  17.         {
  18.             if (!IsPostBack)
  19.             {
  20.                 Label1.Text = "";
  21.                 Label2.Text = "";
  22.                 Label3.Text = "";
  23.                 Label4.Text = "";
  24.  
  25.             }
  26.         }
  27.  
  28.         protected void Button1_Click(object sender, EventArgs e)
  29.         {
  30.             string enteredUsername = TextBox1.Text;
  31.             string enteredPassword = Password1.Value;
  32.  
  33.             if (!File.Exists("C:\\temp\\students.xml"))
  34.             {
  35.                 Label1.Text = "No registered users";
  36.             }
  37.             else
  38.             {
  39.                 var doc = XDocument.Load("C:\\temp\\students.xml");
  40.                 //int descendantsCount = doc.Descendants().Count(); // counts ALL descendants elements
  41.                 int childrenCount = doc.Root.Elements().Count(); //number of students
  42.                 bool userLoggedIn = false;
  43.  
  44.                 for (int i = 0; i < childrenCount && !userLoggedIn; i++)
  45.                 {
  46.                     XElement student = doc.Root.Elements("student").ElementAt(i);
  47.  
  48.                     if (student.Element("username").Value.Equals(enteredUsername))
  49.                     {
  50.  
  51.                         if (student.Element("password").Value.Equals(enteredPassword))
  52.                         {
  53.                             Label1.Text = "Logged in";
  54.                             userLoggedIn = true;
  55.                             Session["boolLoggedIn"] = userLoggedIn;
  56.                             Session["username"] = enteredUsername;
  57.                             Session["id"] = i;
  58.                             Response.Redirect("~/quiz.aspx");
  59.                         }
  60.                     }
  61.                 }
  62.                 if (!userLoggedIn)
  63.                 {
  64.                     Label1.Text = "Not logged in";
  65.                     this.Controls.Add(new LiteralControl("<a href='~/Accounts/Register.aspx'>Register now?</a>"));
  66.                 }
  67.             }
  68.         }
  69.     }
  70. }
Add Comment
Please, Sign In to add comment