Hey micah,
Please refer below sample.
HTML
Login.aspx
<asp:Login ID="Login1" runat="server">
<LayoutTemplate>
<table>
<tr>
<td>
UserName
</td>
<td>
<asp:TextBox ID="UserName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Password
</td>
<td>
<asp:TextBox ID="Password" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Department
</td>
<td>
<asp:TextBox ID="Department" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button ID="Button1" Text="Login" runat="server" OnClick="ValidateUser" />
</td>
</tr>
</table>
</LayoutTemplate>
</asp:Login>
RecievedStock.aspx
<div>
Welcome
<asp:LoginName ID="LoginName1" runat="server" Font-Bold="true" />
<asp:Button Text="Logout" OnClick="ButLogout_Click" runat="server" />
</div>
Namespaces
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Web.Security;
Vb.Net
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Code
C#
Login.aspx.cs
protected void ValidateUser(object sender, EventArgs e)
{
TextBox txtDepartment = Login1.FindControl("Department") as TextBox;
string department;
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("Validate_User_TestDeno"))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Username", Login1.UserName);
cmd.Parameters.AddWithValue("@Password", Login1.Password);
cmd.Parameters.AddWithValue("@Department", txtDepartment.Text);
cmd.Connection = con;
con.Open();
department = cmd.ExecuteScalar().ToString();
con.Close();
}
}
if (department == "Store 1")
{
FormsAuthentication.SetAuthCookie(Login1.UserName, Login1.RememberMeSet);
Response.Redirect("RecievedStock.aspx");
}
else if (department == "Warehouse")
{
FormsAuthentication.SetAuthCookie(Login1.UserName, Login1.RememberMeSet);
Response.Redirect("WareHouse.aspx");
}
else if (department == "Admin")
{
FormsAuthentication.SetAuthCookie(Login1.UserName, Login1.RememberMeSet);
Response.Redirect("Admin.aspx");
}
}
RecievedStock.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!this.Page.User.Identity.IsAuthenticated)
{
FormsAuthentication.RedirectToLoginPage();
}
}
protected void ButLogout_Click(object sender, EventArgs e)
{
FormsAuthentication.SignOut();
FormsAuthentication.RedirectToLoginPage();
}
VB.Net
Login.aspx.cs
Protected Sub ValidateUser(ByVal sender As Object, ByVal e As EventArgs)
Dim txtDepartment As TextBox = TryCast(Login1.FindControl("Department"), TextBox)
Dim department As String
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("Validate_User_TestDeno")
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@Username", Login1.UserName)
cmd.Parameters.AddWithValue("@Password", Login1.Password)
cmd.Parameters.AddWithValue("@Department", txtDepartment.Text)
cmd.Connection = con
con.Open()
department = cmd.ExecuteScalar().ToString()
con.Close()
End Using
End Using
If department = "Store 1" Then
FormsAuthentication.SetAuthCookie(Login1.UserName, Login1.RememberMeSet)
Response.Redirect("RecievedStock.aspx")
ElseIf department = "Warehouse" Then
FormsAuthentication.SetAuthCookie(Login1.UserName, Login1.RememberMeSet)
Response.Redirect("WareHouse.aspx")
ElseIf department = "Admin" Then
FormsAuthentication.SetAuthCookie(Login1.UserName, Login1.RememberMeSet)
Response.Redirect("Admin.aspx")
End If
End Sub
RecievedStock.aspx.vb
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Me.Page.User.Identity.IsAuthenticated Then
FormsAuthentication.RedirectToLoginPage()
End If
End Sub
Protected Sub ButLogout_Click(ByVal sender As Object, ByVal e As EventArgs)
FormsAuthentication.SignOut()
FormsAuthentication.RedirectToLoginPage()
End Sub
Screenshot
