Code for Validating Session With Valid User

Code for Validating Session With Valid User

protected void Page_Init(object sender, EventArgs e)

{

if (Context.Session != null)

{

if (Session.IsNewSession)

{

HttpCookie newSessionIdCookie = Request.Cookies[“ASP.NET_SessionId”];

if (newSessionIdCookie != null)

{

string newSessionIdCookieValue = newSessionIdCookie.Value;

if (newSessionIdCookieValue != string.Empty)

{

// This means Session was timed Out and New Session was started

Response.Redirect(“StudentLogin.aspx”);

}

}

}

}

}

 

 

 

 

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

if (Session[“UserName”] != null)

{

string UserName = (string)Session[“UserName”];

lblSessionName.Text = “Hello   ” + UserName.ToUpper();

Response.ClearHeaders();

Response.AddHeader(“Cache-Control”, “no-cache, no-store, max-age=0, must-revalidate”);

Response.AddHeader(“Pragma”, “no-cache”);

}

else

{

HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);

HttpContext.Current.Response.Cache.SetNoServerCaching();

HttpContext.Current.Response.Cache.SetNoStore();

Session.Abandon();

Response.Write(“Session has timed out or not been initialized”);

Response.Redirect(“StudentLogin.aspx”);

}

}

 

 

public static string TheSessionId()

{

 

HttpSessionState ss = HttpContext.Current.Session;

HttpContext.Current.Session[“UserName”] = “UserName”;

HttpContext.Current.Response.Write(ss.SessionID);

return “ok”;

}

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s