Here is the Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Login</title> </head> <body> <form id="form1" runat="server"> <div> <table> <tr> <td>Enter User Name</td> <td> <asp:TextBox ID="txtUserName" runat="server"> </asp:TextBox></td> </tr> <tr> <td>Enter Password</td> <td> <asp:TextBox ID="txtPassword" runat="server"> </asp:TextBox></td> </tr> <tr> <td> <asp:Button ID="btnSubmint" runat="server" Text="Log in" OnClick="btnSubmint_Click" /></td> <td> <asp:Label ID="lblResult" runat="server" Text=""> </asp:Label></td> </tr> </table> </div> </form> </body> </html>
Here is the Default.aspx.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnSubmint_Click(object sender, EventArgs e) { LoginClass lc = new LoginClass(txtPassword.Text, txtUserName.Text); int result = lc.ValidateLogin(); if (result != 0) { Session["userKey"] = result; Response.Redirect("Welcome.aspx"); } else { lblResult.Text = "Invalid login"; } } }
Here is the ReviewerRegistration.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Login</title> </head> <body> <form id="form1" runat="server"> <div> <table> <tr> <td>Enter User Name</td> <td> <asp:TextBox ID="txtUserName" runat="server"> </asp:TextBox></td> </tr> <tr><%@ Page Language="C#" AutoEventWireup="true" CodeFile="ReviewerRegistration.aspx.cs" Inherits="ReviewerRegistration" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <table> <tr> <td>First Name</td> <td><asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox></td> </tr> <tr> <td>Last Name</td> <td><asp:TextBox ID="txtLastName" runat="server"></asp:TextBox></td> </tr> <tr> <td>Email</td> <td><asp:TextBox ID="txtEmail" runat="server"></asp:TextBox></td> </tr> <tr> <td>User Name</td> <td><asp:TextBox ID="txtUserName" runat="server"></asp:TextBox></td> </tr> <tr> <td>Password</td> <td><asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox></td> </tr> <tr> <td>Confirm Password</td> <td><asp:TextBox ID="txtConfirm" runat="server" TextMode="Password"></asp:TextBox></td> </tr> <tr> <td> <asp:Button ID="btnRegister" runat="server" Text="Register" OnClick="btnRegister_Click" /></td> <td> <asp:Label ID="lblErrorSuccess" runat="server" Text=""></asp:Label></td> </tr> </table> <asp:LinkButton ID="LbLogin" runat="server" PostBackUrl="~/Default.aspx">Log in</asp:LinkButton> <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtUserName" Display="None" ErrorMessage="User name required"></asp:RequiredFieldValidator> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtLastName" Display="None" ErrorMessage="Last name required"></asp:RequiredFieldValidator> </div> <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtEmail" Display="None" ErrorMessage="Invalid email" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator> <asp:ValidationSummary ID="ValidationSummary1" runat="server" /> </form> </body> </html> <td>Enter Password</td> <td> <asp:TextBox ID="txtPassword" runat="server"> </asp:TextBox></td> </tr> <tr> <td> <asp:Button ID="btnSubmint" runat="server" Text="Log in" OnClick="btnSubmint_Click" /></td> <td> <asp:Label ID="lblResult" runat="server" Text=""> </asp:Label></td> </tr> </table> </div> </form> </body> </html>
And here is the ReviewerRegistration.aspx.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class ReviewerRegistration : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnRegister_Click(object sender, EventArgs e) { BookReviewDbEntities db = new BookReviewDbEntities(); try { Reviewer r = new Reviewer(); r.ReviewerFirstName = txtFirstName.Text; r.ReviewerLastName = txtLastName.Text; r.ReviewerEmail = txtEmail.Text; r.ReviewerUserName = txtUserName.Text; r.ReviewPlainPassword = txtPassword.Text; KeyCode kc = new KeyCode(); int code = kc.GetKeyCode(); r.ReviewerKeyCode = code; PasswordHash ph = new PasswordHash(); Byte[] hashed = ph.HashIt(txtPassword.Text, code.ToString()); r.ReviewerHashedPass = hashed; r.ReviewerDateEntered = DateTime.Now; db.Reviewers.Add(r); CheckinLog log = new CheckinLog(); log.Reviewer = r; log.CheckinDateTime = DateTime.Now; db.CheckinLogs.Add(log); db.SaveChanges(); lblErrorSuccess.Text = "Sucessfully Registered"; } catch(Exception ex) { lblErrorSuccess.Text = ex.Message; } } }
And here again is the LoginClass.cs though it is the same as in the other blog. I am not including the password hash class or the key code classes
using System; using System.Collections.Generic; using System.Linq; using System.Web; /// /// This class takes in the user name and password /// retrieves information from the database /// and then hashes the password and key to /// see if it matches the database hash /// public class LoginClass { //class level variables-fields private string pass; private string username; private int seed; private byte[] dbhash; private int key; private byte[] newHash; //constructor takes in password and username public LoginClass(string pass, string username) { this.pass = pass; this.username = username; } //gets the user info from the database private void GetUserInfo() { //declare the ADO Entities BookReviewDbEntities brde = new BookReviewDbEntities(); //query the fields var info = from i in brde.Reviewers where i.ReviewerUserName.Equals(username) select new { i.ReviewerKey, i.ReviewerHashedPass, i.ReviewerKeyCode }; //loop through the results and assign the //values to the field variables foreach (var u in info) { seed = u.ReviewerKeyCode; dbhash = u.ReviewerHashedPass; key = u.ReviewerKey; } } private void GetNewHash() { //get the new hash PasswordHash h = new PasswordHash(); newHash = h.HashIt(pass, seed.ToString()); } private bool CompareHash() { //compare the hashes bool goodLogin = false; //if the hash doesn't exist //because not a valid user //the return will be false if (dbhash != null) { //if the hashes do match return true if (newHash.SequenceEqual(dbhash)) goodLogin = true; } return goodLogin; } public int ValidateLogin() { //call the methods GetUserInfo(); GetNewHash(); bool result = CompareHash(); //if the result is not true //set the key to 0 if (!result) key = 0; return key; } }
No comments:
Post a Comment