zoukankan      html  css  js  c++  java
  • 利用HttpModule 检查用户是否有权限打开指定的网址

    1. 建立一个网站项目
    2. 在app_code 里面 新建立一个类
       /// <summary>

    /// 说明:检查用户是否有权使用模块的Module

    /// 作者:郑文亮

    /// 联系:http://www.cnblogs.com/zhwl

    /// </summary>

    using System;

    using System.Collections.Generic;

    using System.Text;

    using System.Web;


    public class SystemModuleAuthorizationModule : IHttpModule
    {

        #region IHttpModule 成员

        public void Dispose()
        {

        }

        public void Init(HttpApplication context)
        {

            context.AcquireRequestState += new EventHandler(context_AcquireRequestState);

        }

        void context_AcquireRequestState(object sender, EventArgs e)
        {

            HttpApplication application = (HttpApplication)sender;

            string d =  application.Request.Url.ToString();

            // 如果用户未登录,则无需检查模块授权,因为请求会被用户登录Module重定向到登录页面。

            if (application.Session["UserName"] == null)
            {

                string requestUrl = application.Request.Url.ToString();

                string requestPage = requestUrl.Substring(requestUrl.LastIndexOf('/') + 1);

                // 如果请求的页面不是登录页面,刚重定向到登录页面。

                if (requestPage != "Login.aspx")

                    application.Server.Transfer("Login.aspx");

                return;
            }


            // 获取用户名和Url

            string userName = application.Session["UserName"].ToString();

            string url = application.Request.Url.ToString();

            // 如果用户没有被授权,请求被终止,并打印提示信息。

            if (!Validator.CanUseModule(userName, url))
            {

                application.CompleteRequest();
                application.Response.Clear();
                application.Response.Write(string.Format("对不起!{0},您无权访问此模块!", userName));

            }
            else
            {
                application.Response.Write(string.Format("欢迎您!{0}!", userName));
            }

        }

        #endregion

    }

    public class Validator
    {

        /// <summary>

        /// 检查用户是否被授权使用模块。

        /// aaa可以使用模块 a.aspx, 其他的情况返回false

        /// </summary>

        /// <param name="userName"></param>

        /// <param name="url"></param>

        /// <returns></returns>

        public static bool CanUseModule(string userName, string url)
        {
            if (url.Contains("login.aspx") == true)
            {
                return true;
            }
            if (userName == "aaa" && url.Contains("a.aspx"))
            {

                return true;

            }
            else
            {
                return false;
            }

        }

    }


    3. web.config 文件 配置如下

       <httpModules>
        
          <add name="SystemModuleAuthorizationModule" type="SystemModuleAuthorizationModule" />
        </httpModules>

         备注: 因为类文件放在app_Code 下面 所以没有加命名空间的名称(不需要)

        如果是一个wen应用程序

       <system.web>

         <httpModules>
          <add name="SystemModuleAuthorizationModule" type="WebApplication3.SystemModuleAuthorizationModule" />
        </httpModules>
      </system.web>

       注解: WebApplication3 是这个项目的命名空间

    4. 建立login.aspx 里面有一个按钮

        后台文件:
        protected void Button1_Click(object sender, EventArgs e)
        {
            Session["UserName"] = TextBox1.Text.Trim();
        }
        前台页面 :
         <div>
        <a href="a.aspx">页面a</a>
        </div>
        <div>
        <a href="b.aspx">页面b</a>
        </div>
    5. 建立一个空的a.aspx 和 b.aspx (测试用)

  • 相关阅读:
    孔曰成仁,孟曰取义
    mysql索引&实现原理
    MySQL存储引擎
    HashMap原理
    反射
    list对象属性排序
    mysql数据表操作&库操作
    mysql字段类型
    java线程池初步理解
    java四种内部类
  • 原文地址:https://www.cnblogs.com/zhwl/p/1959904.html
Copyright © 2011-2022 走看看