Click here to Skip to main content
15,886,030 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I having two tables:- Admin and Users

Admin(AdminId, Username, Password)

Users(UserId, Username, Password, Role)....[Role=:Student or Teacher]

I have created One login form for both Admin and Users..below is the code of admin login from Admin table...I want to know what changes should I do in my code for Users to login and redirect to their respective page based on their role(student or teacher)

What I have tried:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

public partial class Login : System.Web.UI.Page
{
    SqlConnection con;
    SqlCommand cmd;
    bool flag = true;
 
    public Login()
    {
        con = new SqlConnection();
        con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        cmd = new SqlCommand();
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            con.Open();
            cmd.CommandText = "select * from [ADMIN]";
            cmd.Connection = con;
            SqlDataReader rd = cmd.ExecuteReader();
            while(rd.Read())
            {
                if(rd["USERNAME"].ToString()==TextBox1.Text && rd["PASSWORD"].ToString()==TextBox2.Text)
                {
                    Session["ADMIN"] = rd["USERNAME"];
                    flag = false;
                    break;
                }
            }
            if (flag == true)
                Label1.Text = "Username and password invalid";
            else
                Response.Redirect("~/Admin_welcome.aspx");
        }
        catch(Exception ex)
        {
            Label1.Text = ex.Message;
        }
    }
}
Posted
Updated 30-Mar-16 0:25am
v2
Comments
ZurdoDev 29-Mar-16 9:55am    
if (admin)
{
Response.Redirect("admin.aspx");
}
else {
Response.Redirect("notAdmin.aspx");
}

Seems pretty simple. What is your question exactly?
Member 12170781 29-Mar-16 10:06am    
basically there are 3 users:-

1) Admin----from Admin table

2) Student & 3) Teacher :- 2 and 3 are from Users table contain column 'role' which is use to specify the user is student or teacher...

I want to know the logic how can I redirect this three users to their home page through one login window
Sinisa Hajnal 30-Mar-16 3:21am    
Why are your admins in separate table? You already have role field in users table. Put your admins in there and put the role admin to the record and you're all set.

Then you redirect based on the role.
Richard Deeming 29-Mar-16 11:36am    
Storing passwords in plain text is an extremely bad idea. You should only ever store a salted hash of the password, using a unique salt per record.

Secure Password Authentication Explained Simply[^]
Salted Password Hashing - Doing it Right[^]
Member 12170781 29-Mar-16 11:44am    
but I didn't ask about it

1 solution

The admin table is creating problem for me so I removed it....now check the solution I made:-

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

public partial class Home : System.Web.UI.Page
{

    SqlConnection con;
    SqlCommand cmd;
    bool flag = true;

    public Home()
    {
        con = new SqlConnection();
        con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        cmd = new SqlCommand();
    }


    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnLogIn_Click(object sender, EventArgs e)
    {
        try
        {
            con.Open();
            cmd.CommandText = "select * from [Users]";
            cmd.Connection = con;
            SqlDataReader rd = cmd.ExecuteReader();

            if (txtUserName.Text == "admin" && txtPwd.Text == "admin")
            {
                Session["Username"] = txtUserName.Text;
                Response.Redirect("Admin.aspx");
            }
            else
            {

                while (rd.Read())
                {
                    if (rd["UserName"].ToString() == txtUserName.Text && rd["Password"].ToString() == txtPwd.Text)
                    {
                        Session["Username"] = rd["UserName"];
                        flag = false;
                        break;
                    }
                }
                if (flag == true)
                    lblMsg.Text = "Username and password invalid";
                else
                { 
                    if(rd["Role"].ToString()=="Student")
                    Response.Redirect("Student.aspx");

                    else
                        Response.Redirect("Teacher.aspx");

                }
            }
        }
        catch(Exception ex)
        {
            lblMsg.Text = ex.Message;

        }
    }
}



It is working !!!!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900