ASP. NET prevents users from skipping the login interface

Source: Internet
Author: User

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 ,,,

Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.