這篇文章主要為大家詳細介紹了.net後台頁面統一驗證是否登入的方法,具有一定的參考價值,感興趣的小夥伴們可以參考一下
本文執行個體為大家分享了.net後台頁面統一驗證是否登入的具體代碼,供大家參考,具體內容如下
首先新寫一個PageBase類
using System;using System.Collections.Generic;using System.Web;namespace DepartmentMIS.Web.myclass{ public class PageBase : System.Web.UI.Page { public PageBase() { this.Load += new EventHandler(BasePage_Load); } private void BasePage_Load(object sender, EventArgs e) { if (Session["UserNo"] == null || Session["UserNo"].ToString() == "") { Response.Redirect("~/Login.aspx"); } } }}
Login頁面後台部分代碼
protected void btnLogin_Click(object sender, EventArgs e) { if (rblRole.SelectedValue == "1") { DataSet ds = AdminBLL.GetList("userName = '" + tbxUserName.Text.Trim() + "' and password = '" + tbxPassword.Text.Trim()+"' and isDeleted = 0"); if (ds.Tables[0].Rows.Count == 1) { int id = Convert.ToInt32(ds.Tables[0].Rows[0]["id"]); Session["UserNo"] = ds.Tables[0].Rows[0]["id"]; Session["UserName"] = ds.Tables[0].Rows[0]["userName"]; Response.Redirect("admin/adminIndex.aspx"); } else { Response.Write("<script>alert('使用者名稱或密碼錯誤!')</script>"); } } if (rblRole.SelectedValue == "2") { DataSet ds = StuBLL.GetList("stuNo = '" + tbxUserName.Text.Trim() + "' and password = '" + tbxPassword.Text.Trim() + "' and isDeleted = 0"); if (ds.Tables[0].Rows.Count == 1) { int id = Convert.ToInt32(ds.Tables[0].Rows[0]["id"]); Session["UserNo"] = ds.Tables[0].Rows[0]["id"]; Session["UserName"] = ds.Tables[0].Rows[0]["stuName"]; Response.Redirect("student/stusIndex.aspx"); } else { Response.Write("<script>alert('使用者名稱或密碼錯誤!')</script>"); } }
以stuWishChoices頁面為例,繼承PageBase類
using System.Web.UI.WebControls.WebParts;using System.Data.SqlClient;using System.Collections;namespace cbmis.ProDocumentMng{ public partial class DocumentList : BasePage //繼承 { protected void Page_Load(object sender, EventArgs e) { } } }}