Click here to Skip to main content
15,921,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi tell me how to use session to control various user while login
Posted

C#
object o = Session["nameOfVariable"];

What are you trying to do with the session and login? Are you using a Membership Provider?
 
Share this answer
 
In the login page you can create a session after checking the user has provided the correct username and pasword

C#
Session["username"] = txt_username.Text;
Session["password"] = txt_password.Text;



There after in every page you need to check if the user has been logged in or not , you can do this by checking the session values. So write the following code in every page load event which only logged in users should see.

C#
if(Session["username"] == null || Session["password"]== null)
{
Response.Redirect("Login.aspx");
}
else
{
// You can write your desired code here

}




This is just a small example for lgin process. You should implement proper methods to avoid security breaches like SQL injection and others.

Hope this will help you out.

Let me know in case of any doubts.
 
Share this answer
 
v2
Will suggest you to Read this article first
Exploring Session in ASP.Net[^]. This will give you enough idea about ASP.NET Session. After that if you have any question do let us know.
 
Share this answer
 
C#
SqlDataAdapter da = new SqlDataAdapter("select * from strUserregistration where strusername='" + txtusername .Text + "' and strpassword= '" + txtpassword.Text + "'", (SqlConnection)Application.Get("con"));
        DataSet ds = new DataSet();
        da.Fill(ds);
        Session["UserName"] = txtusername.Text;
        if (ds.Tables[0].Rows.Count > 0)
        {
            string strname = ds.Tables[0].Rows[0][2].ToString();
            string strtype = ds.Tables[0].Rows[0][13].ToString();

            if (strtype == "Active")
            {
                Response.Redirect("somepage.aspx");
            }
            else
            {
                lblmessage.Text = "Incorrect UserName or Password.";In Inactive ";
            }
        }
Execute this code
 
Share this answer
 
v2

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