Wednesday, March 23, 2011

Logout Code for ASP.Net

On the Default Page on Login Button

protected void btnLogin_Click(object sender, EventArgs e)
{
string username = txtUserName.Text;
string password = txtPassword.Text;
UserLogin login = new UserLogin();



if (login.VerifyLogin(username, password))
{
Session["LoginUser"] = username;
Response.Redirect("~/home.aspx");
}
else
{
lblMsg.Text = "User ID OR Password is invalid";
}

In MasterPage CS file on Load Event

protected void Page_Load(object sender, EventArgs e)
{
if (HttpContext.Current.Session["LoginUser"] == null)
{
Response.Redirect("~/Default.aspx");
}
}

In Logout Page on Load event

protected void Page_Load(object sender, EventArgs e)
{
Session["LoginUser"] = null;
Session.Abandon();
Session.Clear();
Session.Contents.RemoveAll();

Response.Redirect("~/Default.aspx");


}

No comments:

Post a Comment