When login verification or other global variables of the session need to be used, there are three convenient implementation methods. (I prefer the first implementation method)
1. Create a public method in a public class and call this method on all pages to be verified.
// In this example, the CheckLogin () method is called in the entry function; [html]
Public static string SeUserID
{
Get
{
Return HttpContext. Current. Session ["SeUserID"]. ToString ();
}
Set
{
HttpContext. Current. Session ["SeUserID"] = value;
}
}
/// <Summary>
/// Check whether the user is logged on. If the user is not logged on, go to the logon page.
/// </Summary>
Public static void CheckLogin ()
{
If (SeUserID = "" | SeUserID = "0 ")
{
HttpContext. Current. Response. Redirect ("ForeignFirms. aspx ");
// HttpContext. current. response. write ("<script> window. open (''); alert ('login failed, please log on again '); </script> ");
}
}
Public static string SeUserID
{
Get
{
Return HttpContext. Current. Session ["SeUserID"]. ToString ();
}
Set
{
HttpContext. Current. Session ["SeUserID"] = value;
}
}
/// <Summary>
/// Check whether the user is logged on. If the user is not logged on, go to the logon page.
/// </Summary>
Public static void CheckLogin ()
{
If (SeUserID = "" | SeUserID = "0 ")
{
HttpContext. Current. Response. Redirect ("ForeignFirms. aspx ");
// HttpContext. current. response. write ("<script> window. open (''); alert ('login failed, please log on again '); </script> ");
}
}
[Html]
// Call the verification method on the cs page
Protected void Page_Load (object sender, EventArgs e)
{
Commom. CommonFunction. CheckLogin (); // verify the login information
If (! IsPostBack)
{
GetData ();
Value = Request. QueryString ["id"]. ToString ();
If (value! = "0 ")
{
GetEdit ();
}
}
}
// Call the verification method on the cs page
Protected void Page_Load (object sender, EventArgs e)
{
Commom. CommonFunction. CheckLogin (); // verify the login information
If (! IsPostBack)
{
GetData ();
Value = Request. QueryString ["id"]. ToString ();
If (value! = "0 ")
{
GetEdit ();
}
}
} 2. Use the Global file to control
[Html]
Protected void Session_Start (Object sender, EventArgs e)
{
Session ["sqlConnectionString"] = "uid = Username; pwd = password; database = MyTest; server = Localhost; Connect Timeout = 300 ";
}
Protected void Session_Start (Object sender, EventArgs e)
{
Session ["sqlConnectionString"] = "uid = Username; pwd = password; database = MyTest; server = Localhost; Connect Timeout = 300 ";
} -- Read method: Apply [html] view plaincopyprint in the code?
String strConnection = Session ["sqlConnectionString"]. ToString ();
SqlConnection_1 = new SqlConnection (strConnection );
String strConnection = Session ["sqlConnectionString"]. ToString ();
SqlConnection_1 = new SqlConnection (strConnection); 3. configure it through the Web. Config file
// Configure the Web. Config file as follows:
Add the following code to the <system. Web> </system. web> node of the web. Config file and set the Session lifecycle to 10 minutes.
[Html]
<SessionState mode = "InProc" timeout = "10"> </sessionState>
<SessionState mode = "InProc" timeout = "10"> </sessionState> when setting a Session in the web. config file, you can set the following parameters: [html] view plaincopyprint?
Mode // this parameter is used to set the storage session status. The statuses include Off, Inproc, StateServer, and SqlServer.
Off // indicates that the session is disabled.
Inproc // indicates the Working Process's own storage session Status
StateServer // indicates that session information will be stored in a separate ASP. NET status service.
SqlServe // r indicates that session information is stored in the SQL Server database.
StateConnecitonString // this parameter is used to set the name of the server in which the ASP. NET application stores the remote session status. The default name is local.
Cookieless // when the value of this parameter is set to True, it indicates that no Cookie is used. If the value is set to False, it indicates that the Cookie Session is started.
SqlConnectionString // this parameter is used to set the SQL Server database connection.
Timeout // this parameter is used to set the session time. If the time limit is exceeded, the session is automatically interrupted. The default value is 20.