zoukankan      html  css  js  c++  java
  • 关于使用HttpModule模块处理登录验证示例

    Http模块(HttpModule)和Http处理程序(HttpHandler)都是用来响应用户请求事件的程序,但二都作用不一.
    HTTP 模块是一个在每次针对应用程序发出请求时调用的程序集。
    ASP.NET HTTP 处理程序是响应对 ASP.NET Web 应用程序的请求而运行的过程(通常称为“终结点”)。
    若程序中调用了用户自定的HttpHandler程序,将会覆盖程序内置的HttpHandler;但在程序中可以使用多个HttpModule;
    以下就HttpModule写一个验证用户是否登录的例子:
    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;
    /// <summary>
    /// ValidateCookie 的摘要说明
    /// </summary>
    public class ValidateCookie:IHttpModule
    {
    public ValidateCookie()
    {}
        #region IHttpModule 成员
        public void Dispose()
        {  }
        public void Init(HttpApplication context)
        {
            context.AcquireRequestState += new EventHandler(context_AcquireRequestState);
        }
        public void context_AcquireRequestState(object sender, EventArgs e)
        {
            // 获取应用程序
            HttpApplication application = (HttpApplication)sender;
            // 检查用户是否已经登录
            if (application.Context.Session == null)
                return;
            if (application.Context.Session["UserName"] == null )
            {
                // 获取Url
                string requestUrl = application.Request.Url.ToString();
                string requestPage = requestUrl.Substring(requestUrl.LastIndexOf('/') + 1);
                // 如果请求的页面不是登录页面,刚重定向到登录页面。
                if (requestPage != "UserLogin.aspx")
                    application.Server.Transfer("UserLogin.aspx");
            }
            else
            {
                // 已经登录,向每个请求的页面打印欢迎词。
                application.Response.Write(string.Format("欢迎您!{0}!", application.Context.Session["UserName"]));         
            }
        }
        #endregion
    }

    登录页面代码与主页代码与普通页面相同.

  • 相关阅读:
    Linux C下的正则表达式
    中英文i18 vue参数传递
    es map的用法
    webpack中publicPath问题
    prop中如何获取vue data中的数据 中英文方案
    利用map reduce方法将对象转成数组
    WPF 踩坑笔记2
    WPF 踩坑笔记1
    RabbitMQ 高阶用法 之 x-expire 过期时间设置
    JANUSEC应用网关1.0发布,提供一站式安全交付能力
  • 原文地址:https://www.cnblogs.com/armyfai/p/2779384.html
Copyright © 2011-2022 走看看