Several ways to summarize of session for login authentication global control
- 2020-06-07 05:11:15
- OfStack
1. Create a public method in a public class and then call it on any page that needs to be validated
// In this case, call the CheckLogin () method in the entry function;
public static string SeUserID
{
get
{
return HttpContext.Current.Session["SeUserID"].ToString();
}
set
{
HttpContext.Current.Session["SeUserID"] = value;
}
}
/// <summary>
/// Check if the user is logged in, and if not, go to the login 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 login again ');</script>");
}
}
// in cs The page invokes the validation method
protected void Page_Load(object sender, EventArgs e)
{
Commom.CommonFunction.CheckLogin();// Verify login information
if (!IsPostBack)
{
GetData();
value = Request.QueryString["id"].ToString();
if (value != "0")
{
GetEdit();
}
}
}
2. Control via Global file
protected void Session_Start(Object sender, EventArgs e)
{
Session["sqlConnectionString"] = "uid=Username;pwd=password;database=MyTest;server=Localhost;Connect Timeout=300";
}
-- The method of reading, the application in the code
String strConnection=Session["sqlConnectionString"].ToString();
sqlConnection_1=new SqlConnection(strConnection);
3, via Web.Config file configuration
// The Web. Config file is configured as follows:
Web. Config file
<
system.web
>
<
/system.web
>
Add the following code to the node to set the life cycle of Session to 10 minutes.
<sessionState mode="InProc" timeout="10"></sessionState>
When setting Session in the web. config file, you can set the following parameters:
Mode// This parameter is used to set the state of the storage session. State including Off , Inproc , StateServer and SqlServer .
Off// Disables session state
Inproc// Represents the worker process itself stores session state
StateServer// Indicates that session information will be stored 1 A separate ASP.NET Status service
SqlServe//r Indicates that session information will be stored SQL Server In the database.
StateConnecitonString// This parameter is used for setting ASP.NET The name of the server where the application stores the remote session state. The default name is local.
Cookieless// When the parameter value is set to True when , Means not to use Cookie// Session identification guest , Otherwise set to False when , Logo launch Cookie Session state.
SqlConnectionString// This parameter is used for setting SQL Server Database connection.
Timeout// This parameter is used to set the session time beyond which the session will be automatically interrupted. The default is 20 .