IIS Handler and Module探索
2016-01-12 17:41
260 查看
Create Handler & Module
Run the Visual StudioCreate a Class Library “HMHandler” --> Change the class to HMHandler.cs
Create a Class Library “HMModule” --> Change the class to HMModule.cs
Create an ASP.NET Web Application “HMDemo” --> Add a Default.aspx page, to test the handler and module, remember the physical path of the site. (e.g. D:\HMDemo)
The structure of solution as below
1. Add code in HMHandler.cs as below
using System; using System.Web; namespace HMHandler { public class HMHandler : IHttpHandler { public bool IsReusable { get { return true; } } public void ProcessRequest(HttpContext context) { DateTime dt; String useUtc = context.Request.QueryString["utc"]; if (!String.IsNullOrEmpty(useUtc) && useUtc.Equals("true")) { dt = DateTime.UtcNow; } else { dt = DateTime.Now; } context.Response.Write( String.Format("<h1>{0}</h1>", dt.ToLongTimeString() )); } } }
2. Update the code in HMModule
using System; using System.Text; using System.Web; namespace HMModule { public class HMModule : IHttpModule { public void Dispose() { //throw new NotImplementedException(); } public void Init(HttpApplication context) { context.AuthenticateRequest += Context_AuthenticateRequest; } private void Context_AuthenticateRequest(object sender, EventArgs e) { try { //Get http application HttpApplication app = sender as HttpApplication; //Get authorization var authorization = app.Request.Headers["Authorization"]; //Remove the "Basic " var authChar = authorization.Remove(0, 6); var authBytes = Convert.FromBase64String(authChar); var authString = Encoding.UTF8.GetString(authBytes); //Reset the authorization app.Request.Headers.Set("Authorization", "Basic YWR2ZW50XHNmZW5nOk1lbmcxMjMkJV4="); app.Response.Write("<h1>Module_Authentication is passed</h1>"); } catch (Exception) { } } } }
3. Update the Defualt.aspx.cs
using System; namespace HMDemo { public partial class Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Response.Write("hello from default page</br>"); } } }
4. Update the web.config as below
<?xml version="1.0"?> <!-- For more information on how to configure your ASP.NET application, please visit http://go.microsoft.com/fwlink/?LinkId=169433 --> <configuration> <system.web> <compilation debug="true" targetFramework="4.6"/> </system.web> <system.webServer> <handlers accessPolicy="Read, Execute, Script"> <add name="HMHandler" path="*.dll" verb="*" type="HMHandler.HMHandler" resourceType="File" requireAccess="Execute" preCondition="integratedMode"/> </handlers> <modules> <add name="HMModule" type="HMModule.HMModule" /> </modules> </system.webServer> </configuration>
Build the solution
Run the Internet Information Services (IIS) Manager
Add a website which Site name is Default Web Site
Physical path is the HMDemo path (eg. D:\HMDemo)
Make sure the managed Pipeline Mode of Default Web Site application pool is Integrated
Copy the scripts folder to D:\HMDemo
Copy the HMModule.dll and HMHandler.dll to D:\HMDemo\bin\
Add IIS_USRS full control to Default Web Site
Run ConfigModules.ps1
Default Web Site --> Handler Mappings Edit Feature Permissions --> Check “Excute”
Default Web Site
Run Windows PowerShell as Administrator
Input Set-ExecutionPolicy RemoteSigned and press <Enter>
Press <Y>
Close Windows PowerShell
Run Windows PowerShell Modules as Administrator
Build IIS
Unlock Web Global Modules
In Windows Server 2008 r2
$modules = Get-WebGlobalModule $modules $count = $modules.Count for($i=1;$i -le $count; $i++) { C:\Windows\System32\inetsrv\appcmd.exe set module $modules[$i].Name /lockItem:true }
In Windows Server 2012 r2
Run WindowsPowerShell as Administrator
Run the ConfigModules.ps1"
Get Web Global Modules"
$modules = Get-WebGlobalModule $modules $count = $modules.Count for($i=1;$i -le $count; $i++) { C:\Windows\System32\inetsrv\appcmd.exe set module $modules[$i].Name /lockItem:true }
Test
Logon and the planned results as below相关文章推荐
- ubuntu14.04无法连接有线连接问题
- 1.3线程停止
- AlertDialog的create和show
- 动态添加综合布局---动态添加控件及将某XML动态加入到Activity显示(续)
- JS函数集合大全
- Unity3D中的Coroutine详解
- [转帖]fstream的使用方法介绍
- 新春赏花看梯田-罗平元阳东川红土红摄影
- ssh通道技术
- ajax处理返回的json格式数据
- 故障案例:一个子查询导致服务崩溃
- 动态添加控件及将某XML动态加入到Activity显示
- web日志分析
- ECharts3.0 强大的统计图
- Iphone的私有API
- OPENCV常见错误(一)
- Android启动另一个APP时,注意disable与enable的问题
- 使用Git上传本地项目代码到github
- R 绘图 填充颜色
- unity3d热更新插件uLua学习整理