Asp.net 服务在注册到IIS的时候,会把每个扩展可以处理的文件扩展名注册到IIS里面(如:*.ascx、*.aspx等)。扩展启动后,就根据定义好的方式来处理IIS所不能处理的文件,然后把控制权跳转到专门处理代码的进程中。让这个进程开始处理代码,生成标准的HTML代码,生成后把这些代码加入到原有的 Html中,最后把完整的Html返回给IIS,IIS再把内容发送到客户端。
//------------------------------------------------------------------------------ // <copyright company="Telligent Systems"> // Copyright (c) Telligent Systems Corporation. All rights reserved. // </copyright> //------------------------------------------------------------------------------
using System; using System.IO; using System.Web; using CommunityServer.Components; using CommunityServer.Configuration;
namespace CommunityServer {
// ********************************************************************* // CSHttpModule // /**//// <summary> /// This HttpModule encapsulates all the forums related events that occur /// during ASP.NET application start-up, errors, and end request. /// </summary> // ***********************************************************************/ public class CSHttpModule : IHttpModule { Member variables and inherited properties / methods#region Member variables and inherited properties / methods
public String ModuleName { get { return "CSHttpModule"; } }
// ********************************************************************* // ForumsHttpModule // /**//// <summary> /// Initializes the HttpModule and performs the wireup of all application /// events. /// </summary> /// <param name="application">Application the module is being run for</param> public void Init(HttpApplication application) { // Wire-up application events // application.BeginRequest += new EventHandler(this.Application_BeginRequest); application.AuthenticateRequest += new EventHandler(Application_AuthenticateRequest); application.Error += new EventHandler(this.Application_OnError); application.AuthorizeRequest += new EventHandler(this.Application_AuthorizeRequest);
//settingsID = SiteSettingsManager.GetSiteSettings(application.Context).SettingsID; Jobs.Instance().Start(); //CSException ex = new CSException(CSExceptionType.ApplicationStart, "Appication Started " + AppDomain.CurrentDomain.FriendlyName); //ex.Log(); }
//int settingsID; public void Dispose() { //CSException ex = new CSException(CSExceptionType.ApplicationStop, "Application Stopping " + AppDomain.CurrentDomain.FriendlyName); //ex.Log(settingsID); Jobs.Instance().Stop(); }
CSException csException = context.Server.GetLastError() as CSException;
if(csException == null) csException = context.Server.GetLastError().GetBaseException() as CSException;
try { if (csException != null) { switch (csException.ExceptionType) { case CSExceptionType.UserInvalidCredentials: case CSExceptionType.AccessDenied: case CSExceptionType.AdministrationAccessDenied: case CSExceptionType.ModerateAccessDenied: case CSExceptionType.PostDeleteAccessDenied: case CSExceptionType.PostProblem: case CSExceptionType.UserAccountBanned: case CSExceptionType.ResourceNotFound: case CSExceptionType.UserUnknownLoginError: case CSExceptionType.SectionNotFound: csException.Log(); break; } } else { Exception ex = context.Server.GetLastError(); if(ex.InnerException != null) ex = ex.InnerException;
csException = new CSException(CSExceptionType.UnknownError, ex.Message, context.Server.GetLastError());
System.Data.SqlClient.SqlException sqlEx = ex as System.Data.SqlClient.SqlException; if(sqlEx == null || sqlEx.Number != -2) //don't log time outs csException.Log(); } } catch{} //not much to do here, but we want to prevent infinite looping with our error handles
// If the installer is making the request terminate early if (CSConfiguration.GetConfig().AppLocation.CurrentApplicationType == ApplicationType.Installer) { return; }
// Only continue if we have a valid context // if ((context == null) || (context.User == null)) return;
try { // Logic to handle various authentication types // switch(context.User.Identity.GetType().Name.ToLower()) {
// Microsoft passport case "passportidentity": p = (Provider) CSConfiguration.GetConfig().Extensions["PassportAuthentication"]; module = ExtensionModule.Instance(p); if(module != null) module.ProcessRequest(); else goto default; break;
// Windows case "windowsidentity": p = (Provider) CSConfiguration.GetConfig().Extensions["WindowsAuthentication"]; module = ExtensionModule.Instance(p); if(module != null) module.ProcessRequest(); else goto default; break;
// Forms case "formsidentity": p = (Provider) CSConfiguration.GetConfig().Extensions["FormsAuthentication"]; module = ExtensionModule.Instance(p); if(module != null) module.ProcessRequest(); else goto default; break;
// Custom case "customidentity": p = (Provider) CSConfiguration.GetConfig().Extensions["CustomAuthentication"]; module = ExtensionModule.Instance(p); if(module != null) module.ProcessRequest(); else goto default; break;
// If the installer is making the request terminate early if (config.AppLocation.CurrentApplicationType == ApplicationType.Installer) { //CSContext.Create(context); return; }
// we're now allowing each individual application to be turned on and off individually. So before we allow // a request to go through we need to check if this product is disabled and the path is for the disabled product, // if so we display the disabled product page. // // I'm also allowing the page request to go through if the page request is for an admin page. In the past if you // disabled the forums you were locked out, now with this check, even if you're not on the same machine but you're accessing // an admin path the request will be allowed to proceed, where the rest of the checks will ensure that the user has the // permission to access the specific url.
//very wachky. The first call into ReWritePath always fails with a 404. //calling ReWritePath twice actually fixes the probelm as well. Instead, //we use the second ReWritePath overload and it seems to work 100% //of the time. if(isReWritten && newPath != null) { string qs = null; int index = newPath.IndexOf('?'); if (index >= 0) { qs = (index < (newPath.Length - 1)) ? newPath.Substring(index + 1) : string.Empty; newPath = newPath.Substring(0, index); } context.RewritePath(newPath,null,qs); }
// If the installer is making the request terminate early if (config.AppLocation.CurrentApplicationType == ApplicationType.Installer) { //CSContext.Create(context); return; }
// we're now allowing each individual application to be turned on and off individually. So before we allow // a request to go through we need to check if this product is disabled and the path is for the disabled product, // if so we display the disabled product page. // // I'm also allowing the page request to go through if the page request is for an admin page. In the past if you // disabled the forums you were locked out, now with this check, even if you're not on the same machine but you're accessing // an admin path the request will be allowed to proceed, where the rest of the checks will ensure that the user has the // permission to access the specific url.
//very wachky. The first call into ReWritePath always fails with a 404. //calling ReWritePath twice actually fixes the probelm as well. Instead, //we use the second ReWritePath overload and it seems to work 100% //of the time. if(isReWritten && newPath != null) { string qs = null; int index = newPath.IndexOf('?'); if (index >= 0) { qs = (index < (newPath.Length - 1)) ? newPath.Substring(index + 1) : string.Empty; newPath = newPath.Substring(0, index); } context.RewritePath(newPath,null,qs); }
// If the installer is making the request terminate early if (CSConfiguration.GetConfig().AppLocation.CurrentApplicationType == ApplicationType.Installer) { return; }
// Only continue if we have a valid context // if ((context == null) || (context.User == null)) return;
try { // Logic to handle various authentication types // switch(context.User.Identity.GetType().Name.ToLower()) {
// Microsoft passport case "passportidentity": p = (Provider) CSConfiguration.GetConfig().Extensions["PassportAuthentication"]; module = ExtensionModule.Instance(p); if(module != null) module.ProcessRequest(); else goto default; break;
// Windows case "windowsidentity": p = (Provider) CSConfiguration.GetConfig().Extensions["WindowsAuthentication"]; module = ExtensionModule.Instance(p); if(module != null) module.ProcessRequest(); else goto default; break;
// Forms case "formsidentity": p = (Provider) CSConfiguration.GetConfig().Extensions["FormsAuthentication"]; module = ExtensionModule.Instance(p); if(module != null) module.ProcessRequest(); else goto default; break;
// Custom case "customidentity": p = (Provider) CSConfiguration.GetConfig().Extensions["CustomAuthentication"]; module = ExtensionModule.Instance(p); if(module != null) module.ProcessRequest(); else goto default; break;
CSException csException = context.Server.GetLastError() as CSException;
if(csException == null) csException = context.Server.GetLastError().GetBaseException() as CSException;
try { if (csException != null) { switch (csException.ExceptionType) { case CSExceptionType.UserInvalidCredentials: case CSExceptionType.AccessDenied: case CSExceptionType.AdministrationAccessDenied: case CSExceptionType.ModerateAccessDenied: case CSExceptionType.PostDeleteAccessDenied: case CSExceptionType.PostProblem: case CSExceptionType.UserAccountBanned: case CSExceptionType.ResourceNotFound: case CSExceptionType.UserUnknownLoginError: case CSExceptionType.SectionNotFound: csException.Log(); break; } } else { Exception ex = context.Server.GetLastError(); if(ex.InnerException != null) ex = ex.InnerException;
csException = new CSException(CSExceptionType.UnknownError, ex.Message, context.Server.GetLastError());
System.Data.SqlClient.SqlException sqlEx = ex as System.Data.SqlClient.SqlException; if(sqlEx == null || sqlEx.Number != -2) //don't log time outs csException.Log(); } } catch{} //not much to do here, but we want to prevent infinite looping with our error handles