IIS7以后application pool都支持两种模式:经典模式和集成模式。

所谓经典模式就是与IIS6的application pool运行模式相同,对于asp.net的页面请求处理由单独的asp.net filter完成。

集成模式是IIS7及以上的默认模式,对于各种请求的处理均在一条流水线上由不同的模块完成。

由于IIS7与.NET的完美结合使很多原本在IIS6中比较麻烦的事情变得简单。比如我们可以通过C#编写托管模块处理网站的所有请求,这在IIS6中需要通过非托管代码写ISAPI filter来完成。

最近就遇到一个小问题,网站因为某种原因需要同时启用匿名和windows集成认证,但是在特定情况下需要将匿名请求变成windows认证过的请求,于是就通过以下几行代码编译成一个IIS扩展dll模块,放在网站相应的bin目录下,然后到IIS Manager - Modules - Add Managed Module添加托管模块。在匿名请求进来的时候判断条件然后返回401 Authentication Challenge, 客户端就弹出认证框开始认证用户了。

  1. using System;
  2. using System.Web;
  3. namespace IISModules
  4. {
  5. public class CustomAuthenticationModule : IHttpModule
  6. {
  7. public const string HttpNtlmSchemeName = "NTLM";
  8. public const int HttpNotAuthorizedStatusCode = 401;
  9. public const string HttpWWWAuthenticateHeader = "WWW-Authenticate";
  10. public const string HttpAuthUserVariable = "AUTH_USER";
  11. public void IssueAuthenticationChallenge(object source, EventArgs e)
  12. {
  13. HttpApplication application = (HttpApplication)source;
  14. HttpContext context = application.Context;
  15. //省略其他判断条件
  16. if (string.IsNullOrEmpty(context.Request.ServerVariables[HttpAuthUserVariable]))
  17. {
  18. context.Response.StatusCode = HttpNotAuthorizedStatusCode;
  19. context.Response.AddHeader(HttpWWWAuthenticateHeader, HttpNtlmSchemeName);
  20. context.Response.AddHeader("Powered By", "CustomAuthenticationModule");
  21. }
  22. }
  23. public void Init(HttpApplication context)
  24. {
  25. context.EndRequest += new EventHandler(this.IssueAuthenticationChallenge);
  26. }
  27. public void Dispose()
  28. {
  29. }
  30. }
  31. }

这种极端的情况只是作为一个事例,但是这也说明了IIS提供了非常方便的扩展功能,在处理一些特殊情况上仍可以做到游刃有余。

C#实现的自定义IIS认证模块 转载的更多相关文章

  1. CMDB资产管理系统开发【day25】:Django 自定义用户认证

    官方文档:https://docs.djangoproject.com/en/1.10/topics/auth/customizing/#substituting-a-custom-user-mode ...

  2. django CBV装饰器 自定义django中间件 csrf跨站请求伪造 auth认证模块

    CBV加装饰器 第一种 @method_decorator(装饰器) 加在get上 第二种 @method_decorator(login_auth,name='get') 加在类上 第三种 @met ...

  3. iis重写模块实现程序自动二级域名,微软提供的URL重写2.0版本适用IIS以上

    在iis7以后微软提供了url重写2.0版本,可以通过安装重写组件来实现.适用于iis7以上版本. 安装有两种方式可以选择,一是下载安装文件,二是通过“web平台安装程序”安装 1.下载安装文件 下载 ...

  4. spring Security的自定义用户认证

    首先我需要在xml文件中声明.我要进行自定义用户的认证类,也就是我要自己从数据库中进行查询 <http pattern="/*.html" security="no ...

  5. author认证模块

    author认证模块 用auth模块 你就用全套 不是自己写一部分 用别人一部分 ​ 创建超级管理员,用于登录DJango admin的后台管理 ​ 命令:createsuperuser,输入顺序用户 ...

  6. Django--csrf跨站请求伪造、Auth认证模块

    form表单中使用跨站请求伪造 { % csrf_token % } 会动态生成一个input框,内部的value是随机刷新的 如果不想校验csrf from django.views.decorat ...

  7. drf JWT认证模块与自定制

    JWT模块 在djangorestframework中,有一款扩展模块可用于做JWT认证,使用如下命令进行安装: pip install djangorestframework-jwt 现在,就让我们 ...

  8. 【Linux】常用的Linux可插拔认证模块(PAM)应用举例:pam_limits.so、pam_rootok.so和pam_userdb.so模块

    常用的Linux可插拔认证模块(PAM)应用举例:pam_limits.so.pam_rootok.so和pam_userdb.so模块 pam_limits.so模块: pam_limits.so模 ...

  9. Django之Auth认证模块

    一.Auth模块是什么 Auth模块是Django自带的用户认证模块: 我们在开发网站的时候,无可避免的需要设计实现网站的用户系统,此时我们需要实现包括用户注册,用户登陆,用户认证,注销修改密码等功能 ...

随机推荐

  1. String.format("%0"+length+"d", arr)中的%0和"d"分别代表什么

    public static void main(String[] args) { int a = 8; String s = String.format("%04d", a); S ...

  2. unity quaternion vector

    做脚印呢 做了曲面细分和decal两种 先用正交camera生成 高度图 采样uv由pos 从world到camera space生成 unity对tessellation的支持限制还是比较大的 只能 ...

  3. Vue-router路由基础总结(一)

    一.安装 npm下载:npm install vue-router 如果在一个模块化工程中使用它,必须要通过 Vue.use() 明确地安装路由功能:在你的文件夹下的 src 文件夹下的 main.j ...

  4. solr 高亮显示

    官网:https://lucene.apache.org/solr/guide/6_6/highlighting.html#Highlighting-TheUnifiedHighlighter 前言 ...

  5. hdu-Coins

    http://acm.hdu.edu.cn/showproblem.php?pid=2844 Problem Description Whuacmers use coins.They have coi ...

  6. urllib库在python2和python3环境下的使用区别

    好东西啊!!! Python 2 name Python 3 name urllib.urlretrieve() urllib.request.urlretrieve() urllib.urlclea ...

  7. centos关于vsftpd的配置、配置说明及常见问题

    一.安装vsftpd 安装yum install -y vsftpd 开机启动 chkconfig vsftpd on 启动 service vsftpd start 加入防火墙 vi /etc/sy ...

  8. ElasticSearch reindex报错:the final mapping would have more than 1 type

    ElasticSearch reindex报错:the final mapping would have more than 1 type 学习了:https://blog.csdn.net/qq_2 ...

  9. C# 继承实现父类方法、重写、重载

    继承是派生类(子类)去实现(重写<override>.重构<new>)基类(父类)的方法或属性.从而获取在派生类中要实现的功能. 子类调用父类构造方法,在父类中有个实现姓名和年 ...

  10. MySQL SELECT 语句

    SELECT语句: products表例如以下: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGl1eWluZ18xMDAx/font/5a6L5L2T ...