Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I want to create a Login form which redirects the user login to the two different form according to the roles of users. I have two forms 1. UserPanelFrm and 2.FrmUserRole and two user role . 1. Admin and 2.User . I want to redirect Admin to UserPanelFrm and User to form FrmUserRole. I researched for this process but only could found useful resources for ASP.NET.

tbl_Staff :

SQL
CREATE TABLE [dbo].[tbl_Staff](
[StaffID] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](100) NOT NULL,
[Address] [nvarchar](500) NULL,
[Phone] [nvarchar](100) NULL,
[Email] [nvarchar](100) NULL,
[JoinedDate] [date] NULL,
[Username] [nvarchar](50) NULL,
[Password] [nvarchar](max) NULL,
[CreatedDate] [date] NULL,
[Roles] [nvarchar](200) NULL,
[Status] [int] NULL
}

tbl_StaffRoles :
SQL
CREATE TABLE [dbo].[tbl_StaffRoles](
[id] [int] NULL,
[RoleDescription] [nvarchar](50) NULL
)

tbl_StaffRoles data :
id RoleDescription
1 Admin
2 User

Hi , I am currently using following code for normal login.

LoginForm btnLogin :

C#
private void btnLogin_Click(object sender, EventArgs e)
{
try
{
int result = uc.Login(txtUserName.Text, txtPassword.Text);
if (result == 1)
{
this.Hide();
UserPanelFrm frm = new UserPanelFrm();
frm.ShowDialog();
this.Close();

}
else
{
MessageBox.Show("INVALID USERNAME OR PASSWORD");
MakeFieldBlank();
}

}

catch (Exception ex)
{

MessageBox.Show(ex.Message);

}

}

UserClass.cs Login class :
public int Login(String Username, String Password)
{

try
{
int result = 0;
SqlCommand cmd = new SqlCommand("Select * from tbl_Staff where Username=@Username and Password=@Password", conn);
cmd.Parameters.AddWithValue("@Username", Username);
cmd.Parameters.AddWithValue("@Password", Password);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
conn.Close();
if (dt.Rows.Count > 0)
result = 1;
else
result = 0;
return result;
}
catch (Exception ex)
{

throw ex;
}
}
Posted
Updated 16-Dec-14 5:38am
v2
Comments
BillWoodruff 16-Dec-14 11:46am    
So, this is a WinForm Application ? And, you have an encrypted database which you use to verify user-login ?

Right now looks like your login code only returns zero or one; so where do you determine the two types of possible valid login ?

Oh, dear...
Never store passwords in clear text - it is a major security risk. There is some information on how to do it here: Password Storage: How to do it.[^]

But better (particularly for a role-based login system) don't "brew it yourself" at all.
Instead, look at Membership: MSDN - Introduction to Membership[^] it provides a role based, secure membership system that is designed for what you are trying to do.


OK, my fault: I missed the WinForms bit: ignore membership, just implement passwords properly.

Then all you have to do when they log in is check the role and open the appropriate form:
C#
string role = ...
Form frm = null;
switch(role.ToLower())
   {
   case "user":
      frm = new FrmUserRole();
      break;
   case "admin":
      frm = new UserPanelFrm();
      break;
   }
if (frm != null)
   {
   Hide();
   frm.ShowDialog();
   Show();
   }


BTW: Please, rename your forms - try to be consistent, it makes life a lot easier next time you have to look at it...
 
Share this answer
 
v2
Comments
BillWoodruff 16-Dec-14 12:01pm    
Just curious; isn't it possible to use the MS Membership stuff outside of a web-based app ?
OriginalGriff 16-Dec-14 14:01pm    
I've never tried - and all the documentation I've seen is clearly peppered with "ASP.NET" references. Since it's integrated with IIS, I suspect it might be a little difficult to use - it's going to want to use Session and suchlike I suspect.
when you authenticate you can get the StaffID by changing the select statement

C#
"Select StaffID  from tbl_Staff where Username=@Username and Password=@Password"

if your datatable having rows means authentication success, you already done that. if you get the value of dt.Rows[0].ItemArray[0] value gives you StaffID .
next execute below statement by givn above value as parameter
C#
"select RoleDescription from tbl_StaffRoles where [id]= @id"

then you can read the role assigned to given user, based on that value you can decide which form to open.
 
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