![]() |
#2
accpfriend2009-11-06 09:59
using System;
using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Collections; using HtgtBossDataAccess; /// <summary> ///ModuleDemo 的摘要说明 /// </summary> namespace HtgtBossWeb { /// <summary> /// 检查用户是否登陆Module /// </summary> public class ModuleDemo : IHttpModule { HttpApplication application; string url = ""; string Vitual = ""; ArrayList arr; public ModuleDemo() { arr = new ArrayList(); //在集合中添加公共页面 string path = HttpContext.Current.Request.ApplicationPath; arr.Add(path + "/Pact/AddCustPactInfo.aspx"); arr.Add(path + "/Cust/AddCustInfo.aspx"); arr.Add(path + "/Cust/EditCustInfo.aspx"); arr.Add(path + "/UserRole/AddOrgInfo.aspx"); arr.Add(path + "/UserRole/EditOrgInfo.aspx"); arr.Add(path + "/UserRole/AddUserInfo.aspx"); arr.Add(path + "/UserRole/EditUserInfo.aspx"); arr.Add(path + "/UserRole/ApplicationInfo.aspx"); arr.Add(path + "/UserRole/CustInfo.aspx"); arr.Add(path + "/UserRole/AddRoleInfo.aspx"); arr.Add(path + "/UserRole/EditRoleInfo.aspx"); arr.Add(path + "/UserRole/AddFunctionItemInfo.aspx"); arr.Add(path + "/UserRole/EditFunctionItemInfo.aspx"); //政策管理 arr.Add(path + "/Policy/AddSpecialCode.aspx"); arr.Add(path + "/Policy/AddSpecialRequire.aspx"); arr.Add(path + "/Policy/AppSpecialCode.aspx"); arr.Add(path + "/Policy/AppSpecialRequire.aspx"); arr.Add(path + "/Policy/AddSpecialPact.aspx"); arr.Add(path + "/Policy/AppSpecialPact.aspx"); arr.Add(path + "/Policy/AddSpecialPolicy.aspx"); arr.Add(path + "/Policy/AppSpecialPolicy.aspx"); arr.Add(path + "/Policy/AddAgentPolicy.aspx"); arr.Add(path + "/Policy/AddFlow.aspx"); arr.Add(path + "/ErrorPage.aspx"); arr.Add(path + "/login.aspx"); arr.Add(path + "/Main.aspx"); } public void Init(HttpApplication context) { context.AcquireRequestState += new EventHandler(context_AcquireRequestState); } void context_AcquireRequestState(object sender, EventArgs e) { #region 获取 每个访问请求的URL的路径,以便于和数据库比较取出相应的功能点ID application = (HttpApplication)sender; url = application.Context.Request.FilePath; Vitual = application.Context.Request.ApplicationPath; url = url.Replace(Vitual, ""); string requestUrl = application.Request.Url.ToString(); string requestPage = requestUrl.Substring(requestUrl.LastIndexOf('/') + 1); #endregion //下面几个页面不需要走权限验证 if (arr.IndexOf(application.Context.Request.Path) >= 0 || application.Context.Request.Path.IndexOf(".ashx") >= 0 || application.Context.Request.Path.IndexOf(".html") >= 0) { if (application.Context.Session["User"] == null) { if (requestPage != "Login.aspx") application.Server.Transfer(Vitual + "/Login.aspx"); } else { return; } } else { if (url.ToLower().EndsWith(".aspx") || url.ToLower().EndsWith(".htm")) { //Session if (application.Context.Session["User"] == null) { if (requestPage != "Login.aspx") application.Server.Transfer(Vitual + "/Login.aspx"); } // 将所有页面在客户端不缓存,以实现数据的实时呈现性. //也就是说,如果没有这个设置,当一些依靠session判断的是否能访问的页面,当session 超时,应该 不能访问了,但是由于IE缓存的存在,还是可以看到的. else { string userName = application.Context.Session["User"].ToString(); //查询当前请求的页面能否访问 LoginIn user = new LoginIn(); if (!user.CanUseModule(userName, url)) { (); application.Response.Write(string.Format("对不起!{0},您无权访问此模块!", userName)); } } application.Context.Response.Expires = 0; } } } public void Dispose() { } } } |
场景
一个网站,有一个首页(Default.aspx),一个登录页面(Login.aspx),两个模块(模块1和模块2)。
一、当用户在未登录的情况下访问网站的任一个页面都会跳转到登录页面要求用户登录,登录完成后跳转到网站首页并在每个页面上显示欢迎词。
二、假设有两个用户,一个“文野”,一个“stwyhm”,文野可以访问模块1,stwyhm可以访问模块2,当他们访问各自有权访问的模块时,显示模块给出的欢迎词,如果访问的模块没有访问权限,给出错误提示。其它用户只能访问指定模块以后的页面。
要求:有页面和类库所有代码。急!急!急!谢谢了!(第一次尝试,希望成功获得满意结果)