// https://msdn.microsoft.com/en-us/library/tkwek5a4%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396 // Example Configuration Code for an HTTP Module: public static void AddModule(Configuration config, string moduleName, string moduleClass) { HttpModulesSection section = (HttpModulesSection)config.GetSection("system.web/httpModules"); // Create a new module action object. HttpModuleAction moduleAction = new HttpModuleAction( moduleName, moduleClass); // "RequestTimeIntervalModule", "Samples.Aspnet.HttpModuleExamples.RequestTimeIntervalModule"); // Look for an existing configuration for this module. int indexOfModule = section.Modules.IndexOf(moduleAction); if (-1 != indexOfModule) { // Console.WriteLine("RequestTimeIntervalModule module is already configured at index {0}", indexOfModule); } else { section.Modules.Add(moduleAction); if (!section.SectionInformation.IsLocked) { config.Save(); // Console.WriteLine("RequestTimeIntervalModule module configured."); } } }
public static bool CheckUrlAccessForPrincipal(string virtualPath, IPrincipal user, string verb) { if (virtualPath == null) { throw new ArgumentNullException("virtualPath"); } if (user == null) { throw new ArgumentNullException("user"); } if (verb == null) { throw new ArgumentNullException("verb"); } verb = verb.Trim(); VirtualPath path = VirtualPath.Create(virtualPath); if (!path.IsWithinAppRoot) { throw new ArgumentException(System.Web.SR.GetString("Virtual_path_outside_application_not_supported"), "virtualPath"); } if (!s_EnabledDetermined) { if (!HttpRuntime.UseIntegratedPipeline) { HttpModulesSection httpModules = RuntimeConfig.GetConfig().HttpModules; int count = httpModules.Modules.Count; for (int i = 0; i < count; i++) { HttpModuleAction action = httpModules.Modules[i]; if (Type.GetType(action.Type, false) == typeof(UrlAuthorizationModule)) { s_Enabled = true; break; } } } else { foreach (ModuleConfigurationInfo info in HttpApplication.IntegratedModuleList) { if (Type.GetType(info.Type, false) == typeof(UrlAuthorizationModule)) { s_Enabled = true; break; } } } s_EnabledDetermined = true; } if (s_Enabled) { AuthorizationSection authorization = RuntimeConfig.GetConfig(path).Authorization; if (!authorization.EveryoneAllowed) { return(authorization.IsUserAllowed(user, verb)); } } return(true); }
private static void AddWebModules(System.Configuration.Configuration config, string name, string type) { // Get the <httpModules> section. HttpModulesSection sectionSystemWeb = (HttpModulesSection)config.GetSection("system.web/httpModules"); // Create a new module action object. HttpModuleAction myHttpModuleAction = new HttpModuleAction(name, type); int indexOfHttpModule = sectionSystemWeb.Modules.IndexOf(myHttpModuleAction); if (indexOfHttpModule == -1) { sectionSystemWeb.Modules.Add(myHttpModuleAction); if (!sectionSystemWeb.SectionInformation.IsLocked) { config.Save(); } } }
public static void Main() { // <Snippet1> // Get the Web application configuration. System.Configuration.Configuration configuration = WebConfigurationManager.OpenWebConfiguration("/aspnetTest"); // Get the section. HttpModulesSection httpModulesSection = (HttpModulesSection)configuration.GetSection( "system.web/httpModules"); // </Snippet1> // <Snippet2> // Create a new section object. HttpModuleAction newModuleAction = new HttpModuleAction("MyModule", "MyModuleType"); // </Snippet2> // <Snippet3> // Initialize the module name and type properties. newModuleAction.Name = "ModuleName"; newModuleAction.Type = "ModuleType"; // </Snippet3> // <Snippet4> // Get the modules collection. HttpModuleActionCollection httpModules = httpModulesSection.Modules; string moduleFound = "moduleName not found."; // Find the module with the specified name. foreach (HttpModuleAction currentModule in httpModules) { if (currentModule.Name == "moduleName") { moduleFound = "moduleName found."; } } // </Snippet4> // <Snippet5> // Get the modules collection. HttpModuleActionCollection httpModules2 = httpModulesSection.Modules; string typeFound = "typeName not found."; // Find the module with the specified type. foreach (HttpModuleAction currentModule in httpModules2) { if (currentModule.Type == "typeName") { typeFound = "typeName found."; } } // </Snippet5> }
internal static void CheckConfiguration(ISite site) { if (site == null) { return; } IWebApplication app = (IWebApplication)site.GetService(typeof(IWebApplication)); if (app == null) { return; } Configuration config = app.OpenWebConfiguration(false); HttpHandlersSection handlers = (HttpHandlersSection)config.GetSection("system.web/httpHandlers"); // Does the httpHandlers Secton already exist? if (handlers == null) { // If not, add it... handlers = new HttpHandlersSection(); ConfigurationSectionGroup group = config.GetSectionGroup("system.web"); // Does the system.web Section already exist? if (group == null) { // If not, add it... config.SectionGroups.Add("system.web", new ConfigurationSectionGroup()); group = config.GetSectionGroup("system.web"); } if (group != null) { group.Sections.Add("httpHandlers", handlers); } } HttpHandlerAction action = new HttpHandlerAction("*/ext.axd", "Ext.Net.ResourceHandler", "*", false); // Does the ResourceHandler already exist? if (handlers.Handlers.IndexOf(action) < 0) { // If not, add it... handlers.Handlers.Add(action); config.Save(); } HttpModulesSection modules = (HttpModulesSection)config.GetSection("system.web/httpModules"); // Does the httpModules Secton already exist? if (modules == null) { // If not, add it... modules = new HttpModulesSection(); ConfigurationSectionGroup group = config.GetSectionGroup("system.web"); // Does the system.web Section already exist? if (group == null) { // If not, add it... config.SectionGroups.Add("system.web", new ConfigurationSectionGroup()); group = config.GetSectionGroup("system.web"); } if (group != null) { group.Sections.Add("httpModules", modules); } } //<add name="DirectRequestModule" type="Ext.Net.DirectRequestModule, Ext.Net" /> HttpModuleAction action2 = new HttpModuleAction("DirectRequestModule", "Ext.Net.DirectRequestModule, Ext.Net"); // Does the ResourceHandler already exist? if (modules.Modules.IndexOf(action2) < 0) { // If not, add it... modules.Modules.Add(action2); config.Save(); } }
public void WebConfigUtil_UpdateConfiguration() { string tempConfigFile = Path.GetTempFileName(); try { File.WriteAllText(tempConfigFile, EmptyConfig); System.Configuration.Configuration cfg = ConfigurationManager.OpenExeConfiguration(tempConfigFile); WebConfigUtil webConfigUtil = new WebConfigUtil(cfg); // Verify that none of the sections we wat to set are present Assert.IsFalse(webConfigUtil.IsAspNetCompatibilityEnabled(), "Blank config should not have AspNetCompatibility"); Assert.IsFalse(webConfigUtil.IsMultipleSiteBindingsEnabled(), "Blank config should not have MultiSiteBinding"); Assert.IsFalse(webConfigUtil.IsEndpointDeclared(BusinessLogicClassConstants.ODataEndpointName), "Blank config should not have OData endpoint"); Assert.IsTrue(webConfigUtil.DoWeNeedToValidateIntegratedModeToWebServer(), "Blank config should not have validate integrated mode"); Assert.IsTrue(webConfigUtil.DoWeNeedToAddHttpModule(), "Blank config should not have http module"); Assert.IsTrue(webConfigUtil.DoWeNeedToAddModuleToWebServer(), "Blank config should not have http module to web server"); string domainServiceFactoryName = WebConfigUtil.GetDomainServiceModuleTypeName(); Assert.IsFalse(string.IsNullOrEmpty(domainServiceFactoryName), "Could not find domain service factory name"); // ------------------------------------ // Set everything we set from the wizard // ------------------------------------ webConfigUtil.SetAspNetCompatibilityEnabled(true); webConfigUtil.SetMultipleSiteBindingsEnabled(true); webConfigUtil.AddValidateIntegratedModeToWebServer(); webConfigUtil.AddHttpModule(domainServiceFactoryName); webConfigUtil.AddModuleToWebServer(domainServiceFactoryName); // ------------------------------------ // Verify API's see the changes // ------------------------------------ Assert.IsTrue(webConfigUtil.IsAspNetCompatibilityEnabled(), "Failed to set AspNetCompatibility"); Assert.IsTrue(webConfigUtil.IsMultipleSiteBindingsEnabled(), "Failed to set MultiSiteBinding"); Assert.IsFalse(webConfigUtil.DoWeNeedToValidateIntegratedModeToWebServer(), "Failed to set validate integrated mode"); Assert.IsFalse(webConfigUtil.DoWeNeedToAddHttpModule(), "Failed to set http module"); Assert.IsFalse(webConfigUtil.DoWeNeedToAddModuleToWebServer(), "Failed to set http module to web server"); // ------------------------------------ // Independently verify those changes // ------------------------------------ // AspNetCompat ServiceHostingEnvironmentSection section = cfg.GetSection("system.serviceModel/serviceHostingEnvironment") as ServiceHostingEnvironmentSection; Assert.IsTrue(section != null && section.AspNetCompatibilityEnabled, "AspNetCompat did not set correct section"); // MultisiteBindings Assert.IsTrue(section != null && section.MultipleSiteBindingsEnabled, "MultisiteBinding did not set correct section"); // Http modules System.Web.Configuration.HttpModulesSection httpModulesSection = cfg.GetSection("system.web/httpModules") as System.Web.Configuration.HttpModulesSection; HttpModuleAction module = (httpModulesSection == null) ? null : httpModulesSection.Modules.OfType <HttpModuleAction>() .FirstOrDefault(a => String.Equals(a.Name, BusinessLogicClassConstants.DomainServiceModuleName, StringComparison.OrdinalIgnoreCase)); Assert.IsNotNull(module, "Did not find httpModule"); // ------------------------------------ // Set and verify OData endpoint // ------------------------------------ webConfigUtil.AddEndpointDeclaration(BusinessLogicClassConstants.ODataEndpointName, WebConfigUtil.GetODataEndpointFactoryTypeName()); Assert.IsTrue(webConfigUtil.IsEndpointDeclared(BusinessLogicClassConstants.ODataEndpointName), "Failed to set OData endpoint"); DomainServicesSection domainServicesSection = cfg.GetSection("system.serviceModel/domainServices") as DomainServicesSection; Assert.IsNotNull(domainServicesSection, "system.serviceModel/domainServices section not found"); Assert.AreEqual(ConfigurationAllowDefinition.MachineToApplication, domainServicesSection.SectionInformation.AllowDefinition, "AllowDefinition s/b MachineToApplication"); Assert.IsFalse(domainServicesSection.SectionInformation.RequirePermission, "RequirePermission s/b false"); ProviderSettings setting = (domainServicesSection == null) ? null : domainServicesSection.Endpoints.OfType <ProviderSettings>().FirstOrDefault(p => string.Equals(p.Name, BusinessLogicClassConstants.ODataEndpointName, StringComparison.OrdinalIgnoreCase)); Assert.IsNotNull(setting, "Did not find OData endpoint in config"); // ValidateIntegratedMode this.CheckValidateIntegratedMode(cfg); // WebServer module this.CheckWebServerModule(cfg, WebConfigUtil.GetDomainServiceModuleTypeName()); } catch (Exception ex) { Assert.Fail("Did not expect exception " + ex); } finally { File.Delete(tempConfigFile); } }
// Methods public int IndexOf(HttpModuleAction action) { }
public void Remove(HttpModuleAction action) { }
public void Add(HttpModuleAction httpModule) { }
public static bool CheckUrlAccessForPrincipal(String virtualPath, IPrincipal user, string verb) { if (virtualPath == null) { throw new ArgumentNullException("virtualPath"); } if (user == null) { throw new ArgumentNullException("user"); } if (verb == null) { throw new ArgumentNullException("verb"); } verb = verb.Trim(); VirtualPath vPath = VirtualPath.Create(virtualPath); if (!vPath.IsWithinAppRoot) { throw new ArgumentException(SR.GetString(SR.Virtual_path_outside_application_not_supported), "virtualPath"); } if (!s_EnabledDetermined) { if (!HttpRuntime.UseIntegratedPipeline) { HttpModulesSection modulesSection = RuntimeConfig.GetConfig().HttpModules; int len = modulesSection.Modules.Count; for (int iter = 0; iter < len; iter++) { HttpModuleAction module = modulesSection.Modules[iter]; if (Type.GetType(module.Type, false) == typeof(UrlAuthorizationModule)) { s_Enabled = true; break; } } } else { List <ModuleConfigurationInfo> modules = HttpApplication.IntegratedModuleList; foreach (ModuleConfigurationInfo mod in modules) { if (Type.GetType(mod.Type, false) == typeof(UrlAuthorizationModule)) { s_Enabled = true; break; } } } s_EnabledDetermined = true; } if (!s_Enabled) { return(true); } AuthorizationSection settings = RuntimeConfig.GetConfig(vPath).Authorization; // Check if the user is allowed, or the request is for the login page return(settings.EveryoneAllowed || settings.IsUserAllowed(user, verb)); }
public static void Main() { // <Snippet1> // Get the Web application configuration. System.Configuration.Configuration configuration = WebConfigurationManager.OpenWebConfiguration( "/aspnetTest"); // Get the section. HttpModulesSection httpModulesSection = (HttpModulesSection)configuration.GetSection( "system.web/httpModules"); // Get the collection. HttpModuleActionCollection modulesCollection = httpModulesSection.Modules; // </Snippet1> // <Snippet2> // Create a new HttpModuleActionCollection object. HttpModuleActionCollection newModuleActionCollection = new HttpModuleActionCollection(); // </Snippet2> //<Snippet3> // Create a new module object. HttpModuleAction ModuleAction = new HttpModuleAction("MyModuleName", "MyModuleType"); // Add the module to the collection. if (!httpModulesSection.SectionInformation.IsLocked) { modulesCollection.Add(ModuleAction); configuration.Save(); } //</Snippet3> //<Snippet4> // Set the module object. HttpModuleAction ModuleAction1 = new HttpModuleAction("MyModule1Name", "MyModule1Type"); // Get the module collection index. int moduleIndex = modulesCollection.IndexOf(ModuleAction1); //</Snippet4> //<Snippet5> // Set the module object. HttpModuleAction ModuleAction2 = new HttpModuleAction("MyModule2Name", "MyModule2Type"); // Remove the module from the collection. if (!httpModulesSection.SectionInformation.IsLocked) { modulesCollection.Remove(ModuleAction2); configuration.Save(); } //</Snippet5> //<Snippet6> // Remove the module from the collection. if (!httpModulesSection.SectionInformation.IsLocked) { modulesCollection.Remove("TimerModule"); configuration.Save(); } //</Snippet6> //<Snippet7> // Remove the module from the collection. if (!httpModulesSection.SectionInformation.IsLocked) { modulesCollection.RemoveAt(0); configuration.Save(); } //</Snippet7> //<Snippet8> // Clear the collection. if (!httpModulesSection.SectionInformation.IsLocked) { modulesCollection.Clear(); configuration.Save(); } //</Snippet8> }
public void Remove(HttpModuleAction action) {}
public void Add(HttpModuleAction httpModule) {}
// Methods public int IndexOf(HttpModuleAction action) {}
public static bool CheckFileAccessForUser(String virtualPath, IntPtr token, string verb) { if (virtualPath == null) { throw new ArgumentNullException("virtualPath"); } if (token == IntPtr.Zero) { throw new ArgumentNullException("token"); } if (verb == null) { throw new ArgumentNullException("verb"); } VirtualPath vPath = VirtualPath.Create(virtualPath); if (!vPath.IsWithinAppRoot) { throw new ArgumentException(SR.GetString(SR.Virtual_path_outside_application_not_supported), "virtualPath"); } if (!s_EnabledDetermined) { if (HttpRuntime.UseIntegratedPipeline) { s_Enabled = true; // always enabled in Integrated Mode } else { HttpModulesSection modulesSection = RuntimeConfig.GetConfig().HttpModules; int len = modulesSection.Modules.Count; for (int iter = 0; iter < len; iter++) { HttpModuleAction module = modulesSection.Modules[iter]; if (Type.GetType(module.Type, false) == typeof(FileAuthorizationModule)) { s_Enabled = true; break; } } } s_EnabledDetermined = true; } if (!s_Enabled) { return(true); } //////////////////////////////////////////////////////////// // Step 3: Check the cache for the file-security-descriptor // for the requested file bool freeDescriptor; FileSecurityDescriptorWrapper oSecDesc = GetFileSecurityDescriptorWrapper(vPath.MapPath(), out freeDescriptor); //////////////////////////////////////////////////////////// // Step 4: Check if access is allowed int iAccess = 3; if (verb == "GET" || verb == "POST" || verb == "HEAD" || verb == "OPTIONS") { iAccess = 1; } bool fAllowed = oSecDesc.IsAccessAllowed(token, iAccess); //////////////////////////////////////////////////////////// // Step 5: Free the security descriptor if adding to cache failed if (freeDescriptor) { oSecDesc.FreeSecurityDescriptor(); } return(fAllowed); }
public static bool CheckFileAccessForUser(string virtualPath, IntPtr token, string verb) { bool flag; if (virtualPath == null) { throw new ArgumentNullException("virtualPath"); } if (token == IntPtr.Zero) { throw new ArgumentNullException("token"); } if (verb == null) { throw new ArgumentNullException("verb"); } VirtualPath path = VirtualPath.Create(virtualPath); if (!path.IsWithinAppRoot) { throw new ArgumentException(System.Web.SR.GetString("Virtual_path_outside_application_not_supported"), "virtualPath"); } if (!s_EnabledDetermined) { if (HttpRuntime.UseIntegratedPipeline) { s_Enabled = true; } else { HttpModulesSection httpModules = RuntimeConfig.GetConfig().HttpModules; int count = httpModules.Modules.Count; for (int i = 0; i < count; i++) { HttpModuleAction action = httpModules.Modules[i]; if (Type.GetType(action.Type, false) == typeof(FileAuthorizationModule)) { s_Enabled = true; break; } } } s_EnabledDetermined = true; } if (!s_Enabled) { return(true); } FileSecurityDescriptorWrapper fileSecurityDescriptorWrapper = GetFileSecurityDescriptorWrapper(path.MapPath(), out flag); int iAccess = 3; if (((verb == "GET") || (verb == "POST")) || ((verb == "HEAD") || (verb == "OPTIONS"))) { iAccess = 1; } bool flag2 = fileSecurityDescriptorWrapper.IsAccessAllowed(token, iAccess); if (flag) { fileSecurityDescriptorWrapper.FreeSecurityDescriptor(); } return(flag2); }