ASP. NET prevents users from skipping the login interface
Source: Internet
Author: User
1. Check that the logon is successful on the login page.CodeAdd the following code 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 ,,,
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service