【源码】继承pagebase 权限判断
public partial class TopFrame : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Func.GetAllObjectList();
(new Func()).GetRolePermission();
}
}
PageBase的全局调用
override protected void OnInit(EventArgs e)
{
InitializeComponent();
Func.CheckThisPage(); //步骤3: 每次页面载入调用检查权限
base.OnInit(e);
}
全局检查session
public void CheckUserPurview()
{
if (System.Web.HttpContext.Current.Session["UserID"] == null)
{
this.RegisterStartupScript("relogin", "<script language=javascript>window.top.location.href='index.aspx'</script>");
}
}
鉴权逐出
public void DenyAccess()
{
System.Web.HttpContext.Current.Response.Write("<center><br><br><br><br><br><br>" + "您没有权限进入" + "<br><br>3 秒钟后返回......</center>");
System.Web.HttpContext.Current.Response.Write("<meta http-equiv=\"refresh\" content=\"3;URL=" + "Sysmain.aspx" + "\" />");
System.Web.HttpContext.Current.Response.Write("<body bgcolor='white'>");
System.Web.HttpContext.Current.Response.End();
}
框架顶页面 登录后调用一次,驻留
public partial class TopFrame : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Func.GetAllObjectList();
(new Func()).GetRolePermission();
}
}
鉴权的类
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using Maticsoft.DBUtility;
using System.Collections.Generic;
using System.Web.UI.MobileControls;
namespace GZstock
{
///<summary>
///Class1 的摘要说明
///</summary>
public partial class Func
{
//********* object里所有页面的集合 *************
public static List<string> ValueList = new List<string>();
//********* 角色拥有的,页面的集合 *************
private List<string> RoleDE_ValueList = new List<string>();
public Func()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
public static void GetAllObjectList()
{
// **************** 得到session,从数据库里提取所属的object范围 **************************************************************
if (System.Web.HttpContext.Current.Session["UserID"] != null || System.Web.HttpContext.Current.Session["UserID"].ToString() != "")
{
string LoginID = "", RoleID = "0", AgentID = System.Web.HttpContext.Current.Session["AgentID"].ToString().ToUpper();
//2.1 *************** 从模块object列表中找出所有页面集合 **************************************
GZstock.BLL.SMS_T_ObjectList SMS_T_ObjectList_bll = new GZstock.BLL.SMS_T_ObjectList();
DataSet ds = SMS_T_ObjectList_bll.GetAllList();
ValueList.Clear();
foreach (DataRow dr in ds.Tables[0].Rows)
{
string s = dr["ObjectValue"].ToString();
ValueList.Add(s);
}
}
}
//************* 此页面属于必须鉴权的,看登录用户对此页面是否有权限 ****************************
public bool IsAllowAccess()
{
bool bl = false;
string url = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
List<string> ListSession = new List<string>();
ListSession = (List<string>)(System.Web.HttpContext.Current.Session["UserDEPageList"]);
foreach (string s in ListSession)
{
//TODO 遍历用户权限的页面
string[] arry = s.Split(',');
string value = arry[1].ToString();
System.Web.HttpContext.Current.Response.Write(value + "" + arry[0].ToString() + "</br>");
if ("/" + value.ToLower() == url.ToLower()) return true;
}
return bl;
}
//************* 查看当前页面是否是属于需要验证的 ****************************
public static bool IsCheckThisPage()
{
bool bl = false;
string url = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
//url = url.Replace("/", "");
foreach (string s in ValueList)
{
if ( "/" + s.ToLower() == url.ToLower()) return true;
}
return bl;
}
public void CheckThisPage()
{
if (IsCheckThisPage()==true)
{
if (IsAllowAccess() == false)
{
PageBase pg = new PageBase();
pg.DenyAccess();
}
}
}
//********** 生成所需要验证的对象结合 *******************************************
public void GetRolePermission()
{
//1 *************** 得到当前页面的地址 ***************************************************************************************
//2 **************** 得到session,从数据库里提取所属的object范围 **************************************************************
if (System.Web.HttpContext.Current.Session["UserID"] != null || System.Web.HttpContext.Current.Session["UserID"].ToString() != "")
{
string LoginID = "", RoleID = "0", AgentID = System.Web.HttpContext.Current.Session["AgentID"].ToString().ToUpper();
//2.1 *************** 从 角色列表里选出 出所有页面集合 **************************************
string sql= string.Format("select RoleID from SMS_V_AgentSeleRole where AgentID = {0}",AgentID);
RoleID = DbHelperSQL.GetSingle(sql).ToString();;
GZstock.BLL.SMS_T_RoleList SMS_T_RoleList_bll = new GZstock.BLL.SMS_T_RoleList();
RoleDE_ValueList.Clear();
DataSet ds = DbHelperSQL.Query("select * from SMS_V_RoleSeleObject where RoleID=" + RoleID);
foreach (DataRow dr in ds.Tables[0].Rows)
{
string s = dr["ObjectText"].ToString()+ "," +dr["ObjectValue"].ToString();
RoleDE_ValueList.Add(s);
}
System.Web.HttpContext.Current.Session["UserDEPageList"] = RoleDE_ValueList;
}
//3 对比当前页是否在objectlist表的范围内,如果不在,则忽略,如果在,则根据看session的所有的object范围是否包含,是则进入,否则登出
}
}
}