zoukankan      html  css  js  c++  java
  • ASP.NET 登录验证 ihttpmoudle

    问题:

    1.iis版本不同(IIS7.0,应用程序池采用的是集成模式,换成经典模式才起作用.)

    在 IIS 7 以下的版本中,应用以下配置:

    <system.web>
      <httpModules>
        <add name="Cftea.MyHttpModule" type="CfteaHttpModule程序集" />
      </httpModules>
    </system.web>

    在 IIS 7 及以上的版本中,应用以下配置

    <system.webServer>
      <modules>
        <add name="Cftea.MyHttpModule" type="CfteaHttpModule程序集" />
      </modules>
    </system.webServer>

    <add name="命名空间.名字" type="程序集名字" />

    例如:

    <add name="web.common.MyModule" type="web" />

    2.具体实现

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace login.common
    {
    public class MyMoudle : IHttpModule
    {
    public void Dispose()
    {
    throw new NotImplementedException();
    }
    
    public void Init(HttpApplication context)
    {
    context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
    }
    
    private void context_PreRequestHandlerExecute(object sender, EventArgs e)
    {
    HttpApplication ha = (HttpApplication)sender;
    int length = ha.Context.Request.Url.Segments.Length;
    string a = ha.Context.Request.RawUrl;
    string path = ha.Context.Request.Url.Segments[length - 1].ToString(); //获得访问的页面
    bool IsLogingPage = path.ToLower().Equals("login.aspx"); //比较是否是Login.aspx 是就不进入
    if (!IsLogingPage)
    {
    //var CookiesDemo = ha.Context.Request.Cookies["UserID"];
    var CookiesDemo = ha.Session["UserID"];
    if (CookiesDemo == null ||CookiesDemo.Equals(""))
    {
    
    //ha.Context.Response.Redirect("/Login.aspx");
    ha.Context.Response.Write("<script>alert('login failed!');url='/login.aspx?url="+ a + "';if(window.parent!=null){window.parent.location=url;}else{this.location=url;};</script>");
    ha.Context.Response.End();
    }
    }else
    {
    }
    }
    }
    }
  • 相关阅读:
    在 Ubuntu 中运转 &micro;Torrent
    TestDisk & PhotoRec——两个数据规复软件
    Ext2 IFS For Windows
    Rainlendar-可定制的桌面日历
    Gimmix:一个新的 MPD 客户端播放器
    SuperSwitcher-桌面增强器械
    Audacity 1.2.6 & 1.3.2
    XChat 2.8.0
    Griffith:电影聚集筹划软件
    VLC media player 0.8.6a
  • 原文地址:https://www.cnblogs.com/uftwkb24/p/9984306.html
Copyright © 2011-2022 走看看