Using session variables is very convenient and usually that is all you will need to maintain state from page to page. However, if you want to save the session state variable in a database for some reason to make sure something is visible only while a session is active (for instance if you want discussion posts to appear to the person posting as long as they are in their session but not to anyone else until you as the administrator approves the post). Here is the code to get the current SessionID.
public static string TheSessionId() {
HttpSessionState ss = HttpContext.Current.Session;
HttpContext.Current.Session["test"] = "test";
HttpContext.Current.Response.Write(ss.SessionID);
return "ok";
}
Comments