Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a website where there is a login page. Whenever a valid user makes login, it is redirected to application pages. Now I want it to have a sign out option.I have done this using redirecting to login page and disabling the back button and also by erasing browsing history of the browser. Now I want this to be managed using session.

Please help with code snippet.


Thanks....
Posted

Try this for login....:)

C#
protected void btnLogin_Click(object sender, EventArgs e)
    {
        if (txtUsername.Text != "" && txtPassword.Text != "")
        {
            RegistrationUser Dataservice = new RegistrationUser();
            DataTable dtGetlogDetail = Dataservice.GetLoginDetail(txtUsername.Text, txtPassword.Text);
            if (dtGetlogDetail.Rows.Count > 0)
            {
                if (dtGetlogDetail.Rows[0]["confirmed"].ToString() == "1")
                {
                    Session["User"] = Convert.ToInt32(dtGetlogDetail.Rows[0]["id"]);
                    Response.Redirect("~/Customer/ClientIndex.aspx");
                }
            }
            else
            {
                Response.Write("<script>alert('Please enter valid Password or Username.')</script>");
            }
        }
    }

Try this for Loguot....:)
C#
protected void btnLogOut_Click(object sender, EventArgs e)
{
 if (Session["User"] != null)
        {
            Session.Clear();//clear session
            Session.Abandon();//Abandon session
            Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetNoStore();          
        }
}
 
Share this answer
 
v2
 
Share this answer
 
Well,implementing login functionality using session is not recommended as there are pros. and cons. with session such as

-Session will be handled at server side.
-If the worker process or application domain is recycled, all session data will be lost.
-Though it is the fastest, more session data and more users can affect performance, because of memory usage.
-Overhead involved in serializing and De-Serializing session Data. because In case of StateServer and SQLServer session mode we need to serialize the object before store.
-Sessions have expiration behavior. If expired app would be crashed.

It is strongly recommended to use below approaches.It would be better in terms of reliability as well as maintainance.

How To: Use Forms Authentication with SQL Server[^]

Form authentication and authorization in ASP.NET[^]

How to: Implement Simple Forms Authentication[^]

[Additional]:

Authenticate User by Roles in ASP.NET[^]

Regards.. :laugh:
 
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