Registration and Login By Role in ASP.NET

In this article, we will learn how to register a user having multiple roles. In the example here we have the three roles "Admin", "Free User" and "Paid User". After successful registration we can also login to the system. During login we can redirect to various webpages depending on their corresponding roles.

Table Creation

Image1.jpg

Table Data

Image2.jpg

Now let's move to the code.

Register.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Register.aspx.cs" Inherits="Login_role.Register" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml" >  
  4. <head runat="server">  
  5.     <title>Register</title>  
  6. </head>  
  7. <body>  
  8.     <form id="form1" runat="server">  
  9.     <div>  
  10.     <table align="center">  
  11.             <tr>  
  12.                 <td colspan="2">  
  13.                     <h3>  
  14.                          Registration using Role</h3>  
  15.                 </td>  
  16.             </tr>  
  17.             <tr>  
  18.                 <td>  
  19.                  <asp:Label ID="Label1" runat="server" Text="UserName:" Font-Bold="True" Width="100px" BackColor="#FFFF66" ForeColor="#FF3300"></asp:Label>  
  20.                     </td>  
  21.                 <td>  
  22.                     <asp:TextBox ID="txt_UserName" runat="server" Width="150px"></asp:TextBox>  
  23.                 </td>  
  24.                 <td><asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Enter UserName" ControlToValidate="txt_UserName"></asp:RequiredFieldValidator> </td>  
  25.             </tr>  
  26.             <tr>  
  27.                 <td>  
  28.                  <asp:Label ID="Label2" runat="server" Text="Password:" Font-Bold="True" Width="100px" BackColor="#FFFF66" ForeColor="#FF3300"></asp:Label>  
  29.                  </td>  
  30.                 <td>  
  31.                     <asp:TextBox ID="txt_Password"  TextMode="Password" runat="server"  
  32.                         Width="150px"></asp:TextBox>  
  33.                 </td>  
  34.                 <td><asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Enter Password" ControlToValidate="txt_Password"></asp:RequiredFieldValidator> </td>  
  35.             </tr>  
  36.             <tr>  
  37.                 <td>  
  38.                 <asp:Label ID="Label3" runat="server" Text="Role:" Font-Bold="True" Width="100px"  Height="100px" BackColor="#FFFF66" ForeColor="#FF3300"></asp:Label>  
  39.                     </td>  
  40.                 <td>  
  41.                     <asp:RadioButtonList ID="rbtRole" runat="server" RepeatDirection="Vertical">  
  42.                         <asp:ListItem>Admin</asp:ListItem>  
  43.                         <asp:ListItem>FreeUser</asp:ListItem>  
  44.                          <asp:ListItem>PaidUser</asp:ListItem>  
  45.                     </asp:RadioButtonList>  
  46.                 </td>  
  47.                 <td><asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="Select role" ControlToValidate="rbtRole"></asp:RequiredFieldValidator> </td>  
  48.             </tr>  
  49.             <tr>  
  50.             <td></td>  
  51.                 <td>  
  52.                     <asp:Button ID="btn_register" runat="server" BackColor="#CCFF99" Text="Register"  
  53.                         onclick="btn_register_Click" />  
  54.                 </td>  
  55.                 <td><asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/Login.aspx"  
  56.                         ForeColor="#663300">Click here to Login</asp:HyperLink></td>  
  57.             </tr>  
  58.             <tr>  
  59.                 <td align="center"  colspan="2">  
  60.                     <asp:Label ID="lblmsg" runat="server"></asp:Label>  
  61.                 </td>  
  62.             </tr>  
  63.         </table>  
  64.     </div>  
  65.     </form>  
  66. </body>  
  67. </html> 

Register.aspx.cs

  1. using System;  
  2. using System.Collections;  
  3. using System.Configuration;  
  4. using System.Data;  
  5. using System.Linq;  
  6. using System.Web;  
  7. using System.Web.Security;  
  8. using System.Web.UI;  
  9. using System.Web.UI.HtmlControls;  
  10. using System.Web.UI.WebControls;  
  11. using System.Web.UI.WebControls.WebParts;  
  12. using System.Xml.Linq;  
  13. using System.Data.SqlClient;  
  14. namespace Login_role  
  15. {  
  16.     public partial class Register : System.Web.UI.Page  
  17.     {  
  18.         string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;  
  19.         SqlCommand com;  
  20.         protected void btn_register_Click(object sender, EventArgs e)  
  21.         {  
  22.             SqlConnection con = new SqlConnection(strConnString);  
  23.             com = new SqlCommand();  
  24.             com.Connection = con;  
  25.             com.CommandType = CommandType.Text;  
  26.             com.CommandText = "Insert into Login values(@UserName,@Password,@Role)";  
  27.             com.Parameters.Clear();  
  28.             com.Parameters.AddWithValue("@UserName", txt_UserName.Text);  
  29.             com.Parameters.AddWithValue("@Password", txt_Password.Text);  
  30.             com.Parameters.AddWithValue("@Role", rbtRole.SelectedValue);  
  31.             if (con.State == ConnectionState.Closed)  
  32.             con.Open();  
  33.             com.ExecuteNonQuery();  
  34.             con.Close();  
  35.             lblmsg.Text = "Successfully Registered!!!";  
  36.             clear();  
  37.         }  
  38.         private void clear()  
  39.         {  
  40.             txt_UserName.Text = "";  
  41.             rbtRole.ClearSelection();  
  42.         }  
  43.     }  
  44. }

Login.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="Login_role.Login" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml" >  
  4. <head runat="server">  
  5.     <title>Login</title>  
  6.     <script type="text/javascript" language="javascript">  
  7.     function Validate()  
  8.     {  
  9.     var UName=document.getElementById('TextBox_user_name');  
  10.     var Password=document.getElementById('TextBox_password');  
  11.     if((UName.value=='') || (Password.value==''))  
  12.     {  
  13.      alert('UserName or Password should not be blank');  
  14.      return false;  
  15.     }  
  16.     else  
  17.     {  
  18.       return true;  
  19.     }  
  20.     }  
  21.     </script>  
  22. </head>  
  23. <body>  
  24.     <form id="form1" runat="server">  
  25.     <div>  
  26.     <asp:Label ID="lb1" runat="server" Font-Bold="True" ForeColor="#FF3300"></asp:Label><br />  
  27.      <asp:Label ID="Label1" runat="server" Text="Name" Font-Bold="True" Width="100px" BackColor="#FFFF66" ForeColor="#FF3300"></asp:Label>  
  28.         <asp:TextBox ID="TextBox_user_name" runat="server" ForeColor="#993300" Width="100px"></asp:TextBox><br />  
  29.         <asp:Label ID="Label2" runat="server" Text="Password" Font-Bold="True" Width="100px" BackColor="#FFFF66" ForeColor="#FF3300"></asp:Label>  
  30.         <asp:TextBox ID="TextBox_password" runat="server" ForeColor="#CC6600" TextMode="Password" Width="100px"></asp:TextBox><br />  
  31.         <asp:Button ID="btn_login" runat="server" Text="Login" Font-Bold="True"  
  32.             BackColor="#CCFF99"   OnClientClick="Validate()" onclick="btn_login_Click"/><br />  
  33.     </div>  
  34.     </form>  
  35. </body>  
  36. </html>

Login.aspx.cs

  1. using System;  
  2. using System.Collections;  
  3. using System.Configuration;  
  4. using System.Data;  
  5. using System.Linq;  
  6. using System.Web;  
  7. using System.Web.Security;  
  8. using System.Web.UI;  
  9. using System.Web.UI.HtmlControls;  
  10. using System.Web.UI.WebControls;  
  11. using System.Web.UI.WebControls.WebParts;  
  12. using System.Xml.Linq;  
  13. using System.Data.SqlClient;  
  14. namespace Login_role  
  15. {  
  16.     public partial class Login : System.Web.UI.Page  
  17.     {  
  18.         string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;  
  19.         string str, UserName, Password;  
  20.         SqlCommand com;  
  21.         SqlDataAdapter sqlda;  
  22.         DataTable dt;  
  23.         int RowCount;  
  24.         protected void btn_login_Click(object sender, EventArgs e)  
  25.         {  
  26.             SqlConnection con = new SqlConnection(strConnString);  
  27.             con.Open();  
  28.             str = "Select * from Login";  
  29.             com = new SqlCommand(str);  
  30.             sqlda = new SqlDataAdapter(com.CommandText, con);  
  31.             dt = new DataTable();  
  32.             sqlda.Fill(dt);  
  33.             RowCount = dt.Rows.Count;  
  34.             for (int i = 0; i < RowCount; i++)  
  35.             {  
  36.                 UserName = dt.Rows[i]["UserName"].ToString();  
  37.                 Password = dt.Rows[i]["Password"].ToString();  
  38.                 if (UserName == TextBox_user_name.Text && Password == TextBox_password.Text)  
  39.                 {  
  40.                     Session["UserName"] = UserName;  
  41.                     if (dt.Rows[i]["Role"].ToString() == "Admin")  
  42.                         Response.Redirect("~/Admin/Admin.aspx");  
  43.                     else if (dt.Rows[i]["Role"].ToString() == "FreeUser")  
  44.                         Response.Redirect("~/FreeUser/FreeUser.aspx");  
  45.                     else if (dt.Rows[i]["Role"].ToString() == "PaidUser")  
  46.                         Response.Redirect("~/PaidUser/PaidUser.aspx");  
  47.                 }  
  48.                 else  
  49.                 {  
  50.                     lb1.Text = "Invalid User Name or Password! Please try again!";  
  51.                 }  
  52.             }  
  53.         }  
  54.     }  
  55. }

Admin.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Admin.aspx.cs" Inherits="Login_role.clerk" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml" >  
  4. <head runat="server">  
  5.     <title>Admin</title>  
  6. </head>  
  7. <body>  
  8.     <form id="form1" runat="server">  
  9.     <div>  
  10.      <h1><font color="red">Admin Page</font></h1>  
  11.     <asp:Label ID="lb1" runat="server" Text="Label"></asp:Label>  
  12.     </div>  
  13.     </form>  
  14. </body>  
  15. </html>

Admin.aspx.cs

  1. using System;  
  2. using System.Collections;  
  3. using System.Configuration;  
  4. using System.Data;  
  5. using System.Linq;  
  6. using System.Web;  
  7. using System.Web.Security;  
  8. using System.Web.UI;  
  9. using System.Web.UI.HtmlControls;  
  10. using System.Web.UI.WebControls;  
  11. using System.Web.UI.WebControls.WebParts;  
  12. using System.Xml.Linq;  
  13. namespace Login_role  
  14. {  
  15.     public partial class clerk : System.Web.UI.Page  
  16.     {  
  17.         protected void Page_Load(object sender, EventArgs e)  
  18.         {  
  19.             lb1.Text = "<b><font color=Brown>" + "WELLCOME ADMIN:: " + "</font>" + "<b><font color=red>" + Session["UserName"] + "</font>";  
  20.         }  
  21.     }  
  22. }

FreeUser.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FreeUser.aspx.cs" Inherits="Login_role.doctor" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml" >  
  4. <head runat="server">  
  5.     <title>FreeUser</title>  
  6. </head>  
  7. <body>  
  8.     <form id="form1" runat="server">  
  9.     <div>  
  10.     <h1><font color="green">FreeUser Page</font></h1>  
  11.     <asp:Label ID="lb1" runat="server" Text="Label"></asp:Label>  
  12.     </div>  
  13.     </form>  
  14. </body>  
  15. </html>

FreeUser.aspx.cs

  1. using System;  
  2. using System.Collections;  
  3. using System.Configuration;  
  4. using System.Data;  
  5. using System.Linq;  
  6. using System.Web;  
  7. using System.Web.Security;  
  8. using System.Web.UI;  
  9. using System.Web.UI.HtmlControls;  
  10. using System.Web.UI.WebControls;  
  11. using System.Web.UI.WebControls.WebParts;  
  12. using System.Xml.Linq;  
  13. namespace Login_role  
  14. {  
  15.     public partial class doctor : System.Web.UI.Page  
  16.     {  
  17.         protected void Page_Load(object sender, EventArgs e)  
  18.         {  
  19.             lb1.Text = "<b><font color=Brown>" + "WELLCOME FREE USER:: " + "</font>" + "<b><font color=red>" + Session["UserName"] + "</font>";  
  20.         }  
  21.     }  
  22. }

PaidUser.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="PaidUser.aspx.cs" Inherits="Login_role.PaidUser" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml" >  
  4. <head runat="server">  
  5.     <title>PaidUser</title>  
  6. </head>  
  7. <body>  
  8.     <form id="form1" runat="server">  
  9.     <div>  
  10.     <h1><font color="olive">PaidUser Page</font></h1>  
  11.     <asp:Label ID="lb1" runat="server" Text="Label"></asp:Label>  
  12.     </div>  
  13.     </form>  
  14. </body>  
  15. </html>

PaidUser.aspx.cs

  1. using System;  
  2. using System.Collections;  
  3. using System.Configuration;  
  4. using System.Data;  
  5. using System.Linq;  
  6. using System.Web;  
  7. using System.Web.Security;  
  8. using System.Web.UI;  
  9. using System.Web.UI.HtmlControls;  
  10. using System.Web.UI.WebControls;  
  11. using System.Web.UI.WebControls.WebParts;  
  12. using System.Xml.Linq;  
  13. namespace Login_role  
  14. {  
  15.     public partial class PaidUser : System.Web.UI.Page  
  16.     {  
  17.         protected void Page_Load(object sender, EventArgs e)  
  18.         {  
  19.             lb1.Text = "<b><font color=Brown>" + "WELLCOME PAID USER:: " + "</font>" + "<b><font color=red>" + Session["UserName"] + "</font>";  
  20.         }  
  21.     }  
  22. } 

Output

Registration using role Admin

Image3.jpg

Image4.jpg

Image5.jpg
Registration using role FreeUser

Image6.jpg

Image7.jpg

Image8.jpg
Registration using role PaidUser

Image9.jpg

Image10.jpg

Image11.jpg


Similar Articles