Creating a Sign In Page
This page is a simple sign in page. Admin and user users will use the same page to Sign in.
Please note that I did not create a column named Role. Instead I use the UserName to differentiate user and admin. If you wish you can also add a new column as "Role" and extend your table. So you can provide different roles to users depending on your project. Here are my table data for your understanding.
Now we will come to the design. I have inserted a Login from Login Tool Box. You can create your own or you can use this available Tool.
This is how it looks.
Now I am manually going to write the code for "Log In" Button. So go to "convert to Template" and double click on the "Log In" Button. It will create the event for that button. "LoginButton_Click" event in the Code behind.
So now we need check the users exist, user password is correct and status is "Enable". Here we go with the "RegisterUser" class with comments. This has all the codes related to the 3 pages.
Next is the Code Behind.
I have redirected the page to "Administrator.aspx" if he is an administrator.
For user I created a dummy page which will say "hi" to the user - "UserLoginPage.aspx".
Code behind
You can have a sign out button as I have explained in the "Administrator Control and Features" at the end of the tutorial.
Demonstration
Download Full Source Code including Database (instructions for configuring).
Please note that I did not create a column named Role. Instead I use the UserName to differentiate user and admin. If you wish you can also add a new column as "Role" and extend your table. So you can provide different roles to users depending on your project. Here are my table data for your understanding.
Table Data |
Now we will come to the design. I have inserted a Login from Login Tool Box. You can create your own or you can use this available Tool.
Login |
Login |
Now I am manually going to write the code for "Log In" Button. So go to "convert to Template" and double click on the "Log In" Button. It will create the event for that button. "LoginButton_Click" event in the Code behind.
Convert to Template |
Code Snippet - RegisterUser.cs
- using System;
- using System.Data;
- using System.Configuration;
- using System.Linq;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.HtmlControls;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Xml.Linq;
- using System.Data.SqlClient;
- public class RegisterUser
- {
- //----------------------METHODS USED IN REGISTER PAGE--------------------------
- // This is for inserting the user using Register Page
- public static int InsertUserData(string Name, string UserName, string Password, stringEmail)
- {
- int rowsAffected = 0;
- using (SqlConnection connection = ConnectionManager.GetDatabaseConnection())
- {
- SqlCommand command = new SqlCommand("insertUserData", connection);
- command.CommandType = CommandType.StoredProcedure;
- command.Parameters.Add("@FName", SqlDbType.VarChar).Value = Name;
- command.Parameters.Add("@UserName", SqlDbType.VarChar).Value = UserName;
- command.Parameters.Add("@Password", SqlDbType.VarChar).Value = Password;
- command.Parameters.Add("@Email", SqlDbType.VarChar).Value = Email;
- rowsAffected = command.ExecuteNonQuery();
- }
- return rowsAffected;
- }
- //This is used to check the UserNamve Availability in the RegisterPage
- public static DataSet IsUserNameExist(string UserName)
- {
- DataSet dataSet = new DataSet();
- using (SqlConnection connection = ConnectionManager.GetDatabaseConnection())
- {
- string sql = "SELECT UserName FROM UserData WHERE UserName = @UserName";
- SqlCommand command = new SqlCommand(sql, connection);
- command.Parameters.Add("@UserName", SqlDbType.VarChar).Value = UserName;
- command.CommandType = CommandType.Text;
- SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
- dataAdapter.Fill(dataSet, "UserName");
- }
- return dataSet;
- }
- //----------------------METHODS USED IN ADMINISTRATOR PAGE--------------------------
- //This is used to update the status from Administrator Page
- public static DataSet UpdateStatus(string UserName,string Status)
- {
- DataSet dataSet = new DataSet();
- using (SqlConnection connection = ConnectionManager.GetDatabaseConnection())
- {
- string sql = "UPDATE UserData SET Status=@Status WHERE UserName=@UserName";
- SqlCommand command = new SqlCommand(sql, connection);
- command.Parameters.Add("@UserName", SqlDbType.VarChar).Value = UserName;
- command.Parameters.Add("@Status", SqlDbType.VarChar).Value = Status;
- command.CommandType = CommandType.Text;
- SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
- dataAdapter.Fill(dataSet, "UserName");
- }
- return dataSet;
- }
- //----------------------METHODS USED IN LOGIN PAGE--------------------------
- //This is used to validate in the LoginPage to check whether user is Enable or Disable
- public static DataSet GetUserStatus(string UserName)
- {
- DataSet dataSet = new DataSet();
- using (SqlConnection connection = ConnectionManager.GetDatabaseConnection())
- {
- string sql = "SELECT Status FROM UserData WHERE UserName=@UserName";
- SqlCommand command = new SqlCommand(sql, connection);
- command.Parameters.Add("@UserName", SqlDbType.VarChar).Value = UserName;
- command.CommandType = CommandType.Text;
- SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
- dataAdapter.Fill(dataSet, "Status");
- }
- return dataSet;
- }
- //This is used to validate in the LoginPage to get the User password
- public static DataSet GetUserID(string UserName)
- {
- DataSet dataSet = new DataSet();
- using (SqlConnection connection = ConnectionManager.GetDatabaseConnection())
- {
- string sql = "SELECT Password FROM UserData WHERE UserName=@UserName";
- SqlCommand command = new SqlCommand(sql, connection);
- command.Parameters.Add("@UserName", SqlDbType.VarChar).Value = UserName;
- command.CommandType = CommandType.Text;
- SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
- dataAdapter.Fill(dataSet, "Password");
- }
- return dataSet;
- }
- }
Next is the Code Behind.
Code Snippet - "LoginPage.aspx.cs"
- using System;
- using System.Collections;
- using System.Configuration;
- using System.Data;
- using System.Linq;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.HtmlControls;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Xml.Linq;
- using System.Data.SqlClient;
- public partial class LoginPage : System.Web.UI.Page
- {
- protected void LoginButton_Click(object sender, EventArgs e)
- {
- string UserName = Login1.UserName;
- string Password = Login1.Password;
- string UserPw = null;
- string UserStatus = null;
- DataSet dataSet1 = RegisterUser.GetUserID(UserName);
- DataSet dataSet2 = RegisterUser.GetUserStatus(UserName);
- foreach (DataRow row in dataSet1.Tables["password"].Rows)
- {
- UserPw = string.Format("{0}", row["password"]);
- }
- foreach (DataRow row in dataSet2.Tables["Status"].Rows)
- {
- UserStatus = string.Format("{0}", row["Status"]);
- }
- if (UserName == "admin" && UserPw == Password && UserStatus == "Enable")
- {
- Session["user"] = UserName;
- Response.Redirect("AdministratorPage.aspx");
- }
- if (UserName != "admin" && UserPw == Password && UserStatus == "Enable")
- {
- Session["user"] = UserName;
- Response.Redirect("UserLoginPage.aspx");
- }
- else
- {
- Login1.FailureText = "Authentication Failed";
- }
- }
- }
I have redirected the page to "Administrator.aspx" if he is an administrator.
For user I created a dummy page which will say "hi" to the user - "UserLoginPage.aspx".
Code Snippet - "UserLoginPage.aspx"
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="UserLoginPage.aspx.cs"Inherits="UserLoginPage" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <asp:Label ID="Label1" runat="server"></asp:Label>
- </form>
- </body>
- </html>
Code behind
Code Snippet - "UserLoginPage.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 UserLoginPage : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- if (Session["user"] != null)
- {
- Label1.Text = "Welcome "+ Session["user"].ToString();
- }
- else
- {
- Response.Redirect("Login.aspx");
- }
- }
- }
You can have a sign out button as I have explained in the "Administrator Control and Features" at the end of the tutorial.
Demonstration
Download Full Source Code including Database (instructions for configuring).
No comments:
Post a Comment