Page Redirection based on Login user department using Form Authentication in ASP.Net

Last Reply 4 months ago By pandeyism

Posted 4 months ago

hi i have 4 different depertments and i want users to be redirected to their page based on their departments,

if A is in class1

B is in class2

C in class3

D in class4

when A is authenticated redirect to page class1

when B is authenticated redirect to page class2

ETC

LIKE  

       string email;
       int userId = 0;
       string constr = ConfigurationManager.ConnectionStrings["DB"].ConnectionString;
       using (SqlConnection con = new SqlConnection(constr))
       {
           using (SqlCommand cmd = new SqlCommand("Validate_UserS"))
           {
               using (SqlDataAdapter sda = new SqlDataAdapter())
               {
                   cmd.CommandType = CommandType.StoredProcedure;
                   //cmd.Parameters.AddWithValue("@UserName", userName);
                   cmd.Parameters.AddWithValue("@Email", LoginINNOVATION.UserName);
                   cmd.Parameters.AddWithValue("@Password", Encrypt(LoginINNOVATION.Password));
                   cmd.Connection = con;
                   con.Open();
                   email = Convert.ToString(cmd.ExecuteScalar());
                   con.Close();
               }
 
 
               if (!string.IsNullOrEmpty(email) && email != "-1" && email != "-2")
               {
                   //FormsAuthentication.RedirectFromLoginPage(LoginINNOVATION.UserName, LoginINNOVATION.RememberMeSet);
                  // Session["userName"] = email;
               }
               else
 
               {
                   authenticated = true;
                   If (Login1.UserName, "A"))
                   {
                      Response.Redirect("~/class1/Class1.aspx");
                   }
                   If (Login1.UserName, "B"))
                   {
                       Response.Redirect("~/B/Class2.aspx");
                   }

 

Posted 4 months ago Modified on 4 months ago

Hi micah,

Please refer below sample.

As per your condition can redirect different page.

In this sample i am redirecting same page for all condition.

HTML

Login.aspx

<asp:Login ID="Login1" runat="server" OnAuthenticate="ValidateUser">
</asp:Login>

Home.aspx

<div>
    Welcome
    <asp:LoginName ID="LoginName1" runat="server" Font-Bold="true" />
    <br />
    <br />
    <asp:Label ID="lblLastLoginDate" runat="server" />
    <asp:LoginStatus ID="LoginStatus1" runat="server" />
</div>

Namespaces

C#

using System.Data;
using System.Configuration;
using System.Data.SqlClient;

VB.Net

Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient

Code

C#

Login.aspx.cs

protected void ValidateUser(object sender, EventArgs e)
{
    int userId = 0;
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("Validate_User"))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Username", Login1.UserName);
            cmd.Parameters.AddWithValue("@Password", Login1.Password);
            cmd.Connection = con;
            con.Open();
            userId = Convert.ToInt32(cmd.ExecuteScalar());
            con.Close();
        }
        if (userId == 1)
        {
            FormsAuthentication.SetAuthCookie(Login1.UserName, Login1.RememberMeSet);
            Response.Redirect("Home.aspx");
        }
        else if (userId == 2)
        {
            FormsAuthentication.SetAuthCookie(Login1.UserName, Login1.RememberMeSet);
            Response.Redirect("Home1.aspx");
        }
        else if (userId == 3)
        {
            FormsAuthentication.SetAuthCookie(Login1.UserName, Login1.RememberMeSet);
            Response.Redirect("Home2.aspx");
        }
    }
}

Home.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.Page.User.Identity.IsAuthenticated)
    {
        FormsAuthentication.RedirectToLoginPage();
    }
}

VB.Net

Login.aspx.vb

Protected Sub ValidateUser(ByVal sender As Object, ByVal e As EventArgs)
    Dim userId As Integer = 0
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As SqlConnection = New SqlConnection(constr)
        Using cmd As SqlCommand = New SqlCommand("Validate_User")
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.AddWithValue("@Username", Login1.UserName)
            cmd.Parameters.AddWithValue("@Password", Login1.Password)
            cmd.Connection = con
            con.Open()
            userId = Convert.ToInt32(cmd.ExecuteScalar())
            con.Close()
        End Using
        If userId = 1 Then
            FormsAuthentication.SetAuthCookie(Login1.UserName, Login1.RememberMeSet)
            Response.Redirect("Home.aspx")
        ElseIf userId = 2 Then
            FormsAuthentication.SetAuthCookie(Login1.UserName, Login1.RememberMeSet)
            Response.Redirect("Home1.aspx")
        ElseIf userId = 3 Then
            FormsAuthentication.SetAuthCookie(Login1.UserName, Login1.RememberMeSet)
            Response.Redirect("Home2.aspx")
        End If
    End Using
End Sub

Home.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

Screenshot

Here UserId is return from procedure. So you need to return department from your procedure to redirect based on the department.