1、登錄頁login.aspx代碼:
using?System;using?System.Collections.Generic;
using?System.Linq;
using?System.Web;
using?System.Web.UI;
using?System.Web.UI.WebControls;
namespace?wgxt.Controllers
{
public?partial?class?login?:?System.Web.UI.Page {protected?void?Page_Load(object?sender,?EventArgs?e)
{
}
protected?void?Button1_Click(object?sender,?EventArgs?e)
{
string?username?=?TextBox1.Text;//獲取輸入的用戶名稱 string?password?=?TextBox2.Text;//獲取輸入的用戶密碼 if?(username=="admin"?&&?password=="123456") {Session["islogin"]?=?"login";//記錄登錄成功
Response.Redirect("WebForm1.aspx");//登錄成功後跳轉到webform1.aspx頁
} else {Response.Write("<script>alert('用戶名和密碼輸入不匹配,請重輸!');</script>");
}}
}}
2、WebForm1.aspx頁代碼
using?System;using?System.Collections.Generic;
using?System.Linq;
using?System.Web;
using?System.Web.UI;
using?System.Web.UI.WebControls;
namespace?wgxt.Controllers
{
public?partial?class?WebForm1?:?System.Web.UI.Page {protected?void?Page_Load(object?sender,?EventArgs?e)
{
if?(Session["islogin"]?==null?||?Session["islogin"].ToString()?==?"")//判斷是否登錄 {Response.Redirect("login.aspx");//沒有登錄驗證跳轉回重新登錄
}}
}}