
February 21st, 2013, 05:35 AM
|
|
Registered User
|
|
Join Date: Sep 2012
Location: USA
Posts: 45
Time spent in forums: 11 h 16 m 13 sec
Reputation Power: 1
|
|
|
Session Issue
HI ! i give you one example with coding i hope it will help you.
public class BasePage : System.Web.UI.Page
{
public SecurityApplicationPageBase()
{
this.Load += new System.EventHandler(this.Page_Load);
}
private void Page_Load(object sender, System.EventArgs e)
{
if(Session["Session_name"]==null)
{
Response.Redirect("Login.aspx");
}
InjectSessionExpireScript();
}
// For demo purpose the timeout is set to a smaller value.
//Remember The Javascript setTimeout works in milliseconds.
protected void InjectSessionExpireScript( )
{
string script = "<script> \n" +
"function expireSession(){ \n"+
" window.location = '"+"Logout.aspx"+"'}\n"+
"setTimeout('expireSession()', " +this.Session.Timeout * 1000 +" ); \n"+
"</script>"
this.Page.RegisterClientScriptBlock("expirescript",script);
}
}
Logout Page: This page calls Session.Abandon() and redirects to the login.aspx page.
public class LogOut : BasePage
{
private void Page_Load(object sender, System.EventArgs e)
{
Session.Abandon();
Response.Redirect("Login.aspx",true);
}
}
Login Page: This page facilitates login. On a successful login a Session variable is created.
public class LogIn : System.Web.UI.Page
{
private void btnLogin_Click(object sender, System.EventArgs e)
{
//when username and pasword is correct
Session.Add("Session_name","loggedinsuccessfully");
}
}
|