使用HttpModule实现基于角色的身份验证

    技术2022-05-11  72

           最近使用HttpModule实现了一个基于角色的身份验证。我将我的代码贴出,欢迎大家批评指正。程序是以用asp.net 2.0实现的。关于HttpModule的介绍网上有很多的资料,我这里就不介绍了:)

    首先,建立一个cs文件:AuthenticateModule.cs。在里面建一个类:AuthenticateModule,这个类实现IHttpModule接口。VS会自动为你添加IHttpModule接口的两个方法:(虽然同样的功能也可以在Globe.asax中实现,但是放到Globe.asax文件中以后,它会和网站程序一起编译。以后如果这个身份验证的模块需要维护,也会使网站程序重新编译。而把身份验证模块单独编译成一个dll文件,则不存在这个问题。而且这个dll文件以后可以在不同的程序中使用)

    public void Dispose()public void Init(HttpApplication context)

    Init方法中添加以下代码:

    context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);

    为什么要在PreRequestHandlerExecute事件中进行处理呢,因为我需要将用户身份的信息存放在Session中,比如:用户名,用户的角色(使用字符串标明,比如:AdministratorOperator等)。而Session只有在AcquireRequestState事件之后才可以使用。而PreRequestHandlerExecute事件是在AcquireRequestState事件之后发生的。参见:

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconhandlingpublicevents.asp

    以下是验证的方法:

      void  context_PreRequestHandlerExecute( object  sender, EventArgs e)         {                        HttpApplication application = (HttpApplication)sender;                        HttpContext context = application.Context;            /*取得一个实现了IMemeberManage接口的实例IMemeberManage是一个自定义的接口,主要实现的功能是判断用户请求的页面是否是他所在的角色所允许访问的。            */            IMemeberManage im = MemberManagementLoader.GetInstance();                        string relativeUrl = context.Request.Url.AbsolutePath.Replace("/PacificAdmin""~");            //仅对aspx类型的请求进行判断            if (context.Request.Url.AbsolutePath.EndsWith(".aspx"))            {                /*Session[“userInfo”]为空,表明用户尚未登录或是登录后Seesion失效,将页面导航到登录页面;同时将用户请求的页面记录到名为redirectPath的QueryString中,在用户进行身份验证后,会自动地导航到该页面*/                if (context.Session["userInfo"== null)                {                    /*                        判断请求的页面是否为“不受保护的页面”                        “不受保护的页面”是无须身份验证即可访问的页面。比如:                        登录页面,错误页面等                    */                    if (!im.IsNonprotectedPage(relativeUrl))                        context.Response.Redirect("~/Login.aspx?redirectPath=" + context.Request.Url.AbsolutePath);                }                else                {                    /*从Session中取出用户的信息,这个信息记录在一个名为UserInfo的结构体中,在这里取出                    */                    string role = ((UserInfo)context.Session["userInfo"]).Role;                    //判断这个角色是否存在                    if (!im.IsRoleExist(role))                    {                        //如果这个角色已不存在,销毁Session,并要求用户重新登录                        HttpContext.Current.Session.Abandon();             HttpContext.Current.Response.Redirect(ConfigurationManager.AppSettings["loginPage"]);                    }                    else                    {                        /*                            这个角色存在的情况下                        */                        if (!im.IsNonprotectedPage(relativeUrl))                        {                            /*                                判断用户是否有权访问                            */                            if (!im.IsAuthorized(role, relativeUrl))                            {                                //无权访问,将用户导航到登录页面                                context.Response.Redirect(ConfigurationManager.AppSettings["RefuseAccessPage"] + "?redirectPath=" + context.Request.UrlReferrer);                            }                        }                    }                }            }        }    

    最新回复(0)