HttpModule实现简单权限限制访问

"权限限制访问"几乎每个程序员在做系统时都会用到。就我而言会用一下三种方法来实现:

写一个权限管理函数,在每个代码隐藏文件中的Page_Load函数中调用。这种方法很低级,代码量很大,如果系统很大的化,那简直就是一场灾难。
撰写一个继承System.Web.UI.Page 基类的BasePage类,然后再 BasePage类继承的OnInit 方法中填写权限管理的代码。然后在系统的每个代码隐藏文件中的Page类继承BasePage类就OK了。使用这种方法虽然能够有效的解决代码重用问题,但是想想如果忘了让Page累继承BasePage类,那将会变成系统安全的一个漏洞......
用HttpModule来实现"权限限制访问"相对简单多了,也可以有效解决以上问题,以下是自己写的代码示例(VS2005),请路过的朋友不要见笑,并提出宝贵意见。
文件->添加新项目->Visual C#->类库 
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.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DataControl;

namespace HM
{
    public class UserControl : IHttpModule
    {

        #region IHttpModule

        public void Dispose()
        {

        }

        public void Init(HttpApplication context)
        {
              context.BeginRequest += new System.EventHandler(httpApplication_BeginRequest);
              
        }

        #endregion

        #region Registered event handlers
        public void httpApplication_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication httpApplication = (HttpApplication)sender;


            HttpCookie UserCookie = httpApplication.Context.Request.Cookies["UserCookie"];


            //如果用户未登录就跳转到登录界面
            if (UserCookie == null)
            {

                httpApplication.Context.Response.Redirect("/Portal/ProtalLogin.aspx");

            }

            //根据当前路径取得用户权限
            DataTable dt = Authority.GetAuthorityByUserAndPath(UserCookie["UserID"], httpApplication.Context.Request.RawUrl);
            if (dt.Rows.Count == 1)
            {
                UserCookie["ISVIEW"] = dt.Rows[0]["ISVIEW"].ToString();
                UserCookie["ISADD"] = dt.Rows[0]["ISADD"].ToString();
                UserCookie["ISUPDATE"] = dt.Rows[0]["ISUPDATE"].ToString();
                UserCookie["ISDELETE"] = dt.Rows[0]["ISDELETE"].ToString();
                UserCookie["ISCONFIRM"] = dt.Rows[0]["ISCONFIRM"].ToString();

            }
            else
            {
                UserCookie["ISVIEW"] = "0";
                UserCookie["ISADD"] = "0";
                UserCookie["ISUPDATE"] = "0";
                UserCookie["ISDELETE"] = "0";
                UserCookie["ISCONFIRM"] = "0";

            }
        }


        #endregion
      
       if (UserCookie["ISVIEW"].ToString().Equals("0"))
            {

                httpApplication.Context.Response.Redirect("/Portal/ProtalLogin.aspx");

            }

    }
}


文件->添加新项目->Visual C#->类库:(将控件修改为能够自动感知权限的控件)
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DataControl;

namespace DataControl
{
      public class UpdateButton : System.Web.UI.WebControls.Button
    {
               protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            HttpCookie UserCookie = this.Context.Request.Cookies["UserCookie"];
            if (UserCookie != null)
            {
                string IsSave = UserCookie["ISUPDATE"];
                if (IsSave.Equals("1"))
                {
                    this.Enabled = true;

                }
                else
                {
                    this.Enabled = false;

                }

            }
        }
      

    }
}


如何调用权限管控代码(文件->新建网站)
       在Web.Config文件中添加调用代码:
<system.web>
<httpModules>
<add name="UserControl" type="HM.UserControl,SQMSHttpModule"/>
    </httpModules>
</system.web>


在页面中调用自动感知权限的控件:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Defaule.aspx.cs" Inherits="Defaule" %>
<!--注册权限感知控件代码-->
<%@ Register TagPrefix="Authority" Namespace="DataControl" Assembly="DataControl" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div >
    <Authority:UpdateButton ID="btnChange" runat="server" Text="Change The System Description"  />

    </div>
    </form>
</body>
</html>


Notices: 请不要在有登录页的模块中调用HttpModule,否则会在登录页面造成跳转死循环。

转自:http://www.cnblogs.com/xiangxiang/archive/2008/12/26/1363034.html

你可能感兴趣的:(c,UI,Web,Security,项目管理)