Click here to Skip to main content
15,917,859 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
by this code i am using to login but this is not efficient apporach.i want to use session for my login.how would i do it
please help me with that...

C#
try
            {
                SqlConnection con = new SqlConnection(@"Data Source=LENOVO-74FE9906\SQLEXPRESS;AttachDbFilename=D:\Farrukh\Orignal Project\Inventory Management System\IMS.mdf;Integrated Security=True");
                string query = "SELECT [user].* FROM [user] where u_name='" + txtusername.Text + "' and pwd='" + txtpassword.Text + "'";
                SqlCommand com = new SqlCommand(query, con);
                con.Open();
                if (txtusername.Text == "admin")
                {

                    if (com.ExecuteReader().HasRows)
                    {


                        Response.Redirect("index.html");
                        Label3.Text = "successfully";



                    }
                    else
                    {
                        Label3.Text = "not successfully";
                        txtusername.Text = "";
                        txtpassword.Text = "";
                    }
                }
                else if (txtusername.Text == "user")
                {
                    if (com.ExecuteReader().HasRows)
                    {


                        Response.Redirect("userinventory.aspx");
                        Label3.Text = "successfully";



                    }
                    else
                    {
                        Label3.Text = "not successfully";
                        txtusername.Text = "";
                        txtpassword.Text = "";
                    }
                }

                con.Close();
            }
            catch (Exception ex)
            {
                Console.Write("" + ex.Message);
            }
Posted
Updated 27-May-13 2:48am
v2

Add this

Session["username"]


to where the user in succesfully logiin ....
 
Share this answer
 
Comments
fak_farrukh 27-May-13 8:52am    
add to where
First of all,if you use session it will be stored in server memory,so thats bad idea and as a good software expert you should avoid using that.

Further,refer to the below links.

Managing Web Site Users with Roles[^] [This link will explain how to redirect users based on their role(admin,user etc.)]

Implement Simple Forms Authentication[^] [This link will explain how to redirect unauthenticated user to login page]

I strongly suggest you to use role based authentication because,think of the system which have 20 user types(admin,user,teacher,hod,ass prof. prof. etc) then redirecting each and every user to different pages will be so tedious.

So try using the above approach,that will help you in this as well as in future issues also.

Best Regards.. :)
 
Share this answer
 
v3
Hi...
Use sessions like this.

session["username"]=textbox1.text;
session["password"]=textbox2.text;

if u want any where,create obj to login page and use sessions.
thank u.
 
Share this answer
 
First of all this is a real bad piece of code in terms of security. Your site can be easily hacked using SQL Injection. Always use stored procedure is the golden rule number 1.

Second, you are passing plain text as password. That means you are storing password in you table as plain text. Anyone with access to that table can enjoy freebies!

Third, if a user logs in successfuly and is redirected to "userinventory.aspx", do you check if he change the URL to "Index.aspx" manually, he will not be granted access?

When you have taken care of these issues very well, try using
C#
if (com.ExecuteReader().HasRows)
{
Session["loggedinuser"] = txtusername.Text; 
}


And wherever you want to use the username try:
C#
if (com.ExecuteReader().HasRows)
{
string username = Session["loggedinuser"];
}
 
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