| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1160 人关注过本帖
标题:拜求:一个检查用户是否登录及模块授权的HttpModule示例
只看楼主 加入收藏
yangliping
Rank: 1
来 自:浙江
等 级:新手上路
帖 子:2
专家分:0
注 册:2009-9-15
结帖率:0
收藏
已结贴  问题点数:20 回复次数:1 
拜求:一个检查用户是否登录及模块授权的HttpModule示例
场景
一个网站,有一个首页(Default.aspx),一个登录页面(Login.aspx),两个模块(模块1和模块2)。
一、当用户在未登录的情况下访问网站的任一个页面都会跳转到登录页面要求用户登录,登录完成后跳转到网站首页并在每个页面上显示欢迎词。
二、假设有两个用户,一个“文野”,一个“stwyhm”,文野可以访问模块1,stwyhm可以访问模块2,当他们访问各自有权访问的模块时,显示模块给出的欢迎词,如果访问的模块没有访问权限,给出错误提示。其它用户只能访问指定模块以后的页面。

要求:有页面和类库所有代码。急!急!急!谢谢了!(第一次尝试,希望成功获得满意结果)
 

搜索更多相关主题的帖子: 检查 用户 HttpModule 模块 示例 
2009-11-01 21:25
accpfriend
Rank: 3Rank: 3
等 级:论坛游侠
威 望:5
帖 子:167
专家分:102
注 册:2006-12-31
收藏
得分:20 
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()
        {

        }
    }
}
2009-11-06 09:59
快速回复:拜求:一个检查用户是否登录及模块授权的HttpModule示例
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018762 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved