1. Add the following code after checking the successful login code on the login page
Session ["UserID"] = txtUserID. Text. Trim ();
Or
Session ["UserID"] = "OK ";
For example, in the response event of my Login. aspx. cs Login button, the Code is as follows:
If (BaseClass. CheckUser (txtUserID. Text. Trim (), txtPwd. Text. Trim ()))
{
Session ["UserID"] = txtUserID. Text. Trim ();
Response. Redirect ("Main. aspx ");
}
Else
{
Response. Write ("<script> alert ('user name or password error'); location = 'login. aspx '</script> ");
}
2. Add the following code at the beginning of Page_Load on each page:
If (Session ["UserID"] = null | Session ["UserID"]. ToString () = "")
{
Response. Write ("<Script language = 'javascript '> window. top. location. href = 'login. aspx'; </Script> ");
}
For example, the Page_Load code in my Main. aspx. cs contains the following content:
Protected void Page_Load (object sender, EventArgs e)
{
If (Session ["UserID"] = null | Session ["UserID"]. ToString () = "")
{
Response. Write ("<Script language = 'javascript '> window. top. location. href = 'login. aspx'; </Script> ");
}
If (! IsPostBack)
{
}
}
3. Try it ,,,