rsv
rsv

Reputation: 3

How to logout from a particular project using asp.net

I am trying to logout from only one project but the problem is when i am running two web project in browser at a time. After login in both, i click logout button of 1st project but both project's Sessions ends automatically on logout button click of any one.

For log out i am using Session.Abandon();.

I am using visual studio 2012, can i logout from only one project?

Here is my login code

protected void btnLogin_Click(object sender, EventArgs e)
{
    con.Open();
    qry = "select * from UserTable where Uname='" + txtUname.Text + "' and Pwd='" + txtPwd.Text + "' ";
    com = new SqlCommand(qry, con);
    dr = com.ExecuteReader();
    if (dr.Read())
    {
        Session["UName"] = txtUname.Text;
        Session["Pwd"] = txtPwd.Text;
        Session["Name"] = dr["Name"].ToString();
        Session["Mail"] = dr["Mail"].ToString();
        if (chkStayLogin.Checked)
        {
            Response.Cookies["rsstl"]["stul_"] = crypt.Encrypt(uname);
            Response.Cookies["rsstl"]["stpl_"] = crypt.Encrypt(pwd);
            Response.Cookies["rsstl"].Expires = DateTime.Now.AddMonths(12);
        }
        else
        {
            Response.Cookies["rsstl"].Expires = DateTime.Now.AddDays(-1);
        }
        con.Close();
        Response.Redirect("Home.aspx");
    }
    else
    {
        lblLogin.Visible = true;
        lblLogin.Text = "Enter Valid User Name And Password";
    }
    con.Close();
}

and my logout code

protected void btnLogout_Click(object sender, EventArgs e)
{
    HttpCookie cki = new HttpCookie("rsstl");
    cki.Expires = DateTime.Now.AddDays(-1);
    Response.Cookies.Add(cki);
    Session.Abandon();
    Response.Redirect("Home.aspx");
}

I am using same code in other project.

Upvotes: 0

Views: 279

Answers (2)

Win
Win

Reputation: 62300

i am trying this on my localhost. 1st project is my summer training project (Online Railway Ticket Booking) and 2nd one is my final year project (Online Examination System) and is there any problem when i will host these two on same domain?

According to the above comment, both sites' cookies are same and stored under same domain name which is localhost.

As the result, when a user logs out from site 1, s/he will be automatically logout from site 2 since they are sharing the same cookie.

In order to avoid that, you'll need to store two different cookie names. For example

Response.Cookies["rsstl" + site1]["stul_"] = crypt.Encrypt(uname);
Response.Cookies["rsstl" + site1]["stpl_"] = crypt.Encrypt(pwd);

Other thoughts

  1. Use parameterized query to avoid SQL Injection.
  2. FormsAuthentication could be a lot easier than what you are currently doing. However, it is out of the scope of your original question.

Upvotes: 0

kiamnemr
kiamnemr

Reputation: 124

try to run in IE in new session

File -> new session

Upvotes: 1

Related Questions