Redirect user to different page based on logged in user in ASP.Net

Last Reply 3 months ago By Indresh

Posted 3 months ago

Example if three different companies are using same login page but i want when company A login they will be directed to page A, When company B login they will be redirected to page B same goes with company C

This is my login page

 

protected void ValidateUser(object sender, EventArgs e)
    {
        int UserID = 5;
        string constr = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand("Validate_User"))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Email", LoginINNOVATION.UserName);
                    cmd.Parameters.AddWithValue("@UserName", LoginINNOVATION.UserName);
                    cmd.Parameters.AddWithValue("@Password", LoginINNOVATION.Password);
                    cmd.Connection = con;
                    con.Open();
                    UserID = Convert.ToInt32(cmd.ExecuteScalar());
                    con.Close();
                }
                switch (UserID)
                {
                    case -1:
                        // Login1.FailureText = "Username or password not correct.";
                        break;
                    case -2:
                        LoginINNOVATION.FailureText = "Account has not been activated.";
                        break;
                    case -3:
                        LoginINNOVATION.FailureText = "Your Password.";
                        break;
                    default:
                        FormsAuthentication.RedirectFromLoginPage(LoginINNOVATION.UserName, LoginINNOVATION.RememberMeSet);
                        break;
                }



            }
        }

    }

   
















 

Posted 3 months ago Modified on 3 months ago

Hi micah,

I have created sample code which fullfill your requirement.

HTML

<div>
    UserName:
    <asp:TextBox ID="txtUserName" Placeholder="Enter a UserName" runat="server" />
    <br />
    <br />
    Password :<asp:TextBox ID="txtPassword" Placeholder="Enter a Password" TextMode="Password"
        runat="server" />
    <br />
    <br />
    <asp:Button ID="btnSubmit" Text="Submit" OnClick="Validate" runat="server" />
</div>

C#

protected void Validate(Object sender, EventArgs e)
{
    int companyId;
    string constr = ConfigurationManager.ConnectionStrings["Constring"].ToString();
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT * FROM UserDetails WHERE UserName=@UserName AND Password=@Password", con))
        {
            DataTable dt = new DataTable();
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            cmd.Parameters.AddWithValue("@UserName", txtUserName.Text);
            cmd.Parameters.AddWithValue("@Password", txtPassword.Text);
            sda.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                companyId = int.Parse(dt.Rows[0]["CompanyId"].ToString());
                switch (companyId)
                {
                    case 1:
                        Response.Redirect("A.aspx");
                        break;
                    case 2:
                        Response.Redirect("B.aspx");
                        break;
                    case 3:
                        Response.Redirect("C.aspx");
                        break;
                }
            }
        }
    }
}

SQL

CREATE TABLE UserDetails
(
 UserId INT
,UserName VARCHAR(20)
,Password VARCHAR(20)
,CompanyId INT
);

------------------------------
CREATE TABLE CompanyName
(
 CompanyId INT
,CompanyName VARCHAR(20)
);

Screenshot

 

 

 

I agree, here is the link: http://e-iceblue.com/free-api