使用用戶控件wheLogined.ascx,代碼如下:
namespace HEZjs.WebForm.modules.ascx
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
/// <summary>
/// modWheLogined 的摘要說明。
/// </summary>
public class modWheLogined : System.Web.UI.UserControl
{
private void Page_Load(object sender, System.EventArgs e)
{
// 在此處放置用戶代碼以初始化頁面
string strPath = HEZjs.WebForm.wfFunction.SiteUrl(this.Parent.Page) + "modules/aspx/modLogin.aspx";
if (this.Session["轉入頁"] == null)
this.Session.Add("轉入頁", this.Parent.Page.Request.RawUrl);
else
this.Session["轉入頁"] = this.Parent.Page.Request.RawUrl;
if (this.Session["用戶"] != null)
{
if (((HEZjs.Sys.sysUser)this.Session["用戶"]).Logined)
{
//判斷權限
//指定樣式
}
else
{
this.Response.Redirect(strPath);
}
}
else
this.Response.Redirect(strPath);
}
#region Web 窗體設計器生成的代碼
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 該調用是 ASP.NET Web 窗體設計器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器
/// 修改此方法的內容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
strPath用來指定系統登錄界面,HEZjs.WebForm.wfFunction.SiteUrl()方法用來返回網站地址
Session["轉入頁"]中存放用戶請求的頁面的地址
Session["用戶"]中存放一個HEZjs.Sys.sysUser對象,該對像包括一個Logined成員用來返回用戶是否已經登錄
把這個用戶控件直接拖放到每一個需要登錄的頁面上,當用戶請求該頁面時,如果用戶未登錄,將轉到登錄頁面,如果已經登錄,則正常瀏覽。
是不是很方便呢?
還可以在上面代碼標注的地方進行用戶對請求頁面的權限的判斷和指定頁面的通用樣式(如果事先沒有指定樣式的話)。
|
|