How do I log out of my asp.net application?

I am using custom code for login and logout in my web application. when you click the login button, the following code is executed:

if (Membership.ValidateUser(txtUserEmail.Text, txtUserPass.Text))
{
    HttpContext.Current.Profile.Initialize(txtUserEmail.Text.Trim(), true);
}

      

Then I check the profile.Username on the pre-init of each page to check if the user is logged in or not. But now I don't know what to do to log out so that the profile is set to zero or something. I'm trying all this to click the exit button:

protected void lnkBtnLogout_Click(object sender, EventArgs e)
{
Session.Abandon();
Request.Cookies.Clear();
FormsAuthentication.SignOut();
var p = HttpContext.Current.Profile;
Response.Redirect("/Default.aspx");
}

      

I am using the p variable to check if the profile has been reset or not, but it still has all the values โ€‹โ€‹of the logged in user. So what should I do with resetting the profile and logging out of the user?

+3


source to share


1 answer


Immediately after SignOut () does the redirect, stopping anything else so that the page stops updating anything else.

So the code will be.



protected void lnkBtnLogout_Click(object sender, EventArgs e)
{
  Session.Abandon();
  Request.Cookies.Clear();
  FormsAuthentication.SignOut();
  Response.Redirect("/Default.aspx", true);
}

      

After checking the redirection if the use is still logged in. The user does not log out after calling SignOut, but after finishing and closing the last cookie and loading the next page.

+2


source







All Articles