Edit config custom section in IIS - asp.net

I am working on big ASP.NET project(we using ASP.NET 3.5) which comprised of 5 different WebSites and some shared assemblies. Recently I added custom section into web.config files for each site. When I deploy all these applications, each site is deployed separately under same app pool.
Is there any way to make this section editable in IIS on site level, just like you can edit ConnectionString section for each site?
Sections I added all look like this:
<sectionGroup name="RegistriesCustomSettings">
<section name="RegistriesSettings"
type="Registries.Business.Utilities.RegistriesConfigurations"/>
</sectionGroup >
<RegistriesCustomSettings>
<RegistriesSettings ContextCommandTimeout="30"
logLinq="true" DisplayUser="true" BaseReportPath="/DDD/"
ReportingServer="http://patriot-regdev:8000/ReportServer"
TopInstitution="1000001" />
</RegistriesCustomSettings>
We using are IIS 7.0, 2008 RC 2.

Yes there is a way to do this by extending the IIS configuration schema.
Create a file called RegistriesSchema.xml and copy and paste the following XML:
<configSchema>
<sectionSchema name="RegistriesCustomSettings">
<element name="RegistriesSettings">
<attribute name="ContextCommandTimeout"
type="int"
validationType="integerRange"
validationParameter="1,600"
allowInfinite="true"
defaultValue="30" />
<attribute name="logLinq"
type="bool"
defaultValue="True" />
<attribute name="DisplayUser"
type="bool"
defaultValue="True" />
<attribute name="BaseReportPath"
type="string"
validationType="nonEmptyString" />
<attribute name="ReportingServer"
type="string"
validationType="nonEmptyString" />
<attribute name="TopInstitution"
type="string"
validationType="nonEmptyString" />
</element>
</sectionSchema>
</configSchema>
Grab a copy of a tool called IisSchema.exe from here:
IISSCHEMA.EXE - A tool to register IIS7 configuration sections
Unzip and make sure both the exe and the xml schema file are in the same folder.
From an administrator command line (i.e. open cmd.exe using "Run As Administrator"):
IISSCHEMA.EXE /install RegistriesSchema.xml
This will do two things:
drops the schema file into %systemroot%\system32\inetsrv\config\schema
adds the following XML to applicationHost.config:
<section name="RegistriesCustomSettings"
overrideModeDefault="Allow"
allowDefinition="Everywhere" />
4 . Launch IIS Manager and open the feature settings for your website and open the Configuration Editor:
5 . Select the Section drop down list:
If all is good you should see "RegistriesCustomSettings", select this item.
6 . You can now edit these settings and they'll be added to your site's web.config file:
This is just a demonstration so the schema settings may not be quite right and will probably need some fine tuning.
What To Do With <sectionGroup name="RegistriesCustomSettings">?:
You will still need to add the configSection/sectionGroup xml to your web.config file for each site or you could add it to the root machine.config file for whatever version of ASP.NET you're using, i.e.:
For .NET Framework 2.0 (which also applies to .NET3.0 and 3.5):
%systemroot%\Microsoft.NET\Framework\v2.050727\CONFIG\machine.config
%systemroot%\Microsoft.NET\Framework64\v2.050727\CONFIG\machine.config
For .NET Framework 4.0:
%systemroot%\Microsoft.NET\Framework\v4.0.30319\CONFIG\machine.config
%systemroot%\Microsoft.NET\Framework64\v4.0.30319\CONFIG\machine.config
If you put your assembly's configSection/sectionGroup in your machine.config file(s) then you don't need to declare it in every site's web.config. If quite a few sites are going to be using this assembly then this may be good timesaver.
Update:
There seems to be a bug or limitation in the IIS7.5 Configuration Editor. It appears that if you have your own custom configSections <sectionGroup> or <section> declarations in your site's web.config file this breaks the IIS7.5 Configuration Editor. I'm trying to get to the bottom of this:
ASP.NET custom configuration section declaration breaks IIS Manager Configuration Editor
Update 2:
I think the MS docs on this are a bit bogus particularly where your custom config section needs to be consumable by ASP.NET and editable in the IIS Manager Configuration Editor. The trick seems to be to declare the schema as follows in the RegistriesSchema.xml file:
<configSchema>
<sectionSchema name="RegistriesCustomSettings/RegistriesSettings">
<attribute name="ContextCommandTimeout"
type="int"
validationType="integerRange"
validationParameter="1,600"
allowInfinite="true"
defaultValue="30" />
<attribute name="logLinq"
type="bool"
defaultValue="True" />
<attribute name="DisplayUser"
type="bool"
defaultValue="True" />
<attribute name="BaseReportPath"
type="string"
validationType="nonEmptyString" />
<attribute name="ReportingServer"
type="string"
validationType="nonEmptyString" />
<attribute name="TopInstitution"
type="string"
validationType="nonEmptyString" />
</sectionSchema>
</configSchema>
Also, and importantly, remove the section reference from applicationHost.config:
<section name="RegistriesCustomSettings"
overrideModeDefault="Allow"
allowDefinition="Everywhere" />
This is not required.
Additionally, you don't actually need to use the iisschema.exe tool, just grab a copy of NotePad2 (it's a 64bit editor, you need this to edit anything in inetsrv\config) and create the RegistriesSchema.xml file directly in inetsrv\config\schema.
You can find out more about extending the IIS7 schema here:
Extending IIS 7.0 Schema and Accessing the Custom Sections Using MWA
You can poke about the existing schema files to learn more about how to construct these settings. They can be found in:
%systemroot%\system32\inetsrv\config\schema
Caveat: The example above was tested on IIS7.5 x64 RTM on Windows 7 x64 Ultimate. You mention that you're running a release candidate so your mileage may vary because of that.

Related

disable 8.3 name creation in web.config file

i use asp .net core version 3.1.
i know that for disabling 8.3 name creation i can set NtfsDisable8dot3NameCreation vaule to 1 on below path.
"HKLM\SYSTEM\CurrentControleSet\Control\FileSystem"
but my problem is that i dont know how i can set this configuration in web.config file.
actualy i want to use web.config XMl code for solving this issue.
and i try below setting in webconfig but get error when run project.
<configuration>
<HKLM>
<SYSTEM>
<CurrentControleSet>
<Control>
<FileSystem>
<add key="NtfsDisable8dot3NameCreation" value="1"/>
</FileSystem>
</Control>
</CurrentControleSet>
</SYSTEM>
</HKLM>
</configuration>

Framework64 and Framework

There are 2 applications pointing to 2 different paths.
1. C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\web.config
2. C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config\web.config
I want to keep my connection strings in 1 single file so that both the applications should be able to read the same.
Any idea?
You can try to store your connection strings in external .config file and include it in both web.config files. Here is how to export profile configuration into a dedicated configuration file.
This is web.config:
...
<system.web>
...
<profile configSource="profile.config" />
...
</system.web>
...
This is profile.config:
<profile>
<properties>
<add name="Name" type="String" />
<add name="Age" type="Int32" />
</properties>
</profile>
Make sure you use .config as the extension of your files so they cannot be served to the browser.
See this blog post for details
To use one .config file in multiple applications create a symbolic link to that file in each application folder and reference that link in web config. Use mklink command in elevated command prompt:
cd "c:\YourApplicationDirectoryWhereWebConfigIs"
mklink profile.config "c:\YourSharedConfigFilesDirectory\profile.config"

Adding custom section to web.config in ASP.NET 4

I've spent half of a day trying to understand why the following fails.
I can add section anywhere but never got it working like that ():
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<mysection />
<system.web>
<compilation debug="false" batch="false" targetFramework="4.0" />
</system.web>
<system.webServer>
<handlers>
</handlers>
</system.webServer>
</configuration>
I think the error related to .NET 4, because when you put section without pre-configuration in applicationHost.config it shows error with gray border saying that config is incorrect. That is what I expect. Then I add section definition and everything seems to work I can edit config from console - this means it is parsed correctly now.
But when I try to reach Application, it gives:
Parser Error Message: Unrecognized configuration section mysection
with a piece of config on yellow background.
Or do I need to write a module to consume that settings ? At the moment I do not have any, just a text in config.
following links will help you understand for this.
http://www.codeproject.com/Articles/32628/ASP-NET-Custom-Web-Configuration-Section
https://web.archive.org/web/20211020133931/https://www.4guysfromrolla.com/articles/032807-1.aspx
Regards,
Old topic but these links are very helpfull:
http://www.iis.net/learn/develop/extending-iis-configuration/configuration-extensibility
http://www.iis.net/learn/develop/extending-iis-configuration/extending-iis-schema-and-accessing-the-custom-sections-using-mwa
Edit (05/25/2016) :
The Details of how to store custom information in applicationHost.config file ... I hope this helps !
Note : These settings wont be visible on IIS Manager. There is a way to do that but thats beyond the scope of this response.
Requirement:
Need to extend the system.applicationHost/sites section of applicationHost.config file to allow a siteowner attribute at the site level. (IIS Does not allow us to do this by default). Nor can you manually edit the applicationHost.config file and add custom tags/attributes.
Steps:
Create a custom schema ( xml ) file under %windir%\system32\inetsrv\config\schema\ . File name: siteExtension_schema.xml
Include the custom elements that you want to eventually save in the applicationHost.config in that xml and save it with a appropriate name. The crucial thing to keep in mind is the sectionSchema tag.So when extending the schema of an existing section, simply create a element and set the name attribute to be the same as an existing section. In the schema file (see below), we have defined a with a name of "system.applicationHost/sites" - this is the same as the sectionSchema name in the default IIS_Schema.xml file in the Schema directory. So in essence you are instructing IIS to add these
<!-- Contents of %windir%\system32\inetsrv\config\schema\siteExtension_schema.xml -->
<configSchema>
<sectionSchema name="system.applicationHost/sites">
<collection addElement="site">
<attribute name="owner" type="string" />
<attribute name="ownerEmail" type="string" />
</collection>
</sectionSchema>
</configSchema>
Test the modifications by adding values for the "owner" and "ownerEmail" attributes that we included in step 2 above and then check the configuration file (applicationHost.config) to see the changes. Simply run the following command (must be elevated as Administrator) from the command line (uses appcmd ) to do so:
C:\> %windir%\system32\inetsrv\appcmd set site "Default Web Site" /owner:"John Contoso" /ownerEmail:"john#contoso.com"
To see if the configuration was applied, run the following command and check the output:
C:\> %windir%\system32\inetsrv\appcmd list site "Default Web Site" /config
<system.applicationHost>
<sites>
...
<site name="Default Web Site" id="1" siteOwner="John Contoso" siteOwnerEmail="john#contoso.com">
...
...
</site>
</sites>
</system.applicationHost>
To Read and Write your settings programmatically thru C# :
//this Will work with the ServerManager.OpenRemote("MyRemoteHostname") method also
using(var mgr = new ServerManager())
{
//Read
Console.WriteLine(mgr.Sites["Default Web Site"].Attributes["owner"].Value ); //Prints "John Contoso"
//Write
mgr.Sites["Default Web Site"].Attributes["owner"].Value = "New Owner";// Sets new value
mgr.CommitChanges(); // commits the changes to applicationHost.Config
}

Impersonating different users during install in WiX setup

I've just created a WiX v3.5 installer to install my Web application to IIS7. I have custom actions to allow the user to choose which website and app pool they want and to name the Virtual directory via the dialogs.
But now I've come to authentication and I'm stumped. I am trying to enable impersonation and allow the user to enter their impersonation login and password. I had this working fine in my Visual Studion 2010 setup projects so now I need to replicate the same in WiX.
Aparently this can be done via an appcmd as per this question: Is setting "ASP.NET Impersonation" possible using WiX 3.x with IISExtension? but I can't seem to get this working. Can i add this in my product.wxs and wrap it in a custom action? Any ideas anyone? Any help would be appreciated?
appcmd set config /commit:WEBROOT/section:identity /impersonate:true
Hi I managed to get this working myself , so if anyone else is having the same issue , i fixed this by modifying my web.config during my install:
To do this i added the following code to my product.wsx to edit my web.config , using properties which i assigned to text boxes in a new dialog to allow the user to enter the impersonation username and password on install :
<Component Id="Web.config" Guid="2ED81B77-F153-4003-9006-4770D789D4B6">
<File Id="Web.config" Name="Web.config" Source="$(var.SolutionDir)MyWebApp\Web.config" DiskId="1" KeyPath="yes" />
<util:XmlFile Id="system.webidentity" File="[INSTALLLOCATION]Web.config" Action="createElement" ElementPath="/configuration/system.web" Name="identity" Sequence="1" />
<util:XmlFile Id="system.webIdentityAttribute" Action="setValue" File="[INSTALLLOCATION]Web.config" ElementPath="/configuration/system.web/identity" Name="impersonate" Value="true" Sequence="2" />
<util:XmlFile Id="system.webIdentityAttribute2" Action="setValue" File="[INSTALLLOCATION]Web.config" ElementPath="/configuration/system.web/identity" Name="password" Value="[IMPERSONATIONUSERPASSWORD]" Sequence="3" />
<util:XmlFile Id="system.webIdentityAttribute3" Action="setValue" File="[INSTALLLOCATION]Web.config" ElementPath="/configuration/system.web/identity" Name="userName" Value="[IMPERSONATIONUSER]" Sequence="4" />
Note if you are adding your files automatically to your Wix project using msbuild and heat , you'll have to ensure you arent copying your web.config here , or if you are , remove my web.config your Target settings. Otherwise you'll get duplication errors .
<Target Name="BeforeBuild">
<MSBuild Projects="%(ProjectReference.FullPath)" Targets="Package" Properties="Configuration=$(Configuration);Platform=AnyCPU" Condition="'%(ProjectReference.PackageThisProject)'=='True'" />
<Delete Files="%(ProjectReference.RootDir)%(ProjectReference.Directory)obj\$(Configuration)\Package\PackageTmp\web.config">
</Delete>
<PropertyGroup>
<LinkerBaseInputPaths>%(ProjectReference.RootDir)%(ProjectReference.Directory)obj\$(Configuration)\Package\PackageTmp\</LinkerBaseInputPaths>
</PropertyGroup>
<HeatDirectory OutputFile="%(ProjectReference.Filename).wxs" Directory="%(ProjectReference.RootDir)%(ProjectReference.Directory)obj\$(Configuration)\Package\PackageTmp\" DirectoryRefId="INSTALLLOCATION" ComponentGroupName="%(ProjectReference.Filename)_Project" SuppressCom="true" SuppressFragments="true" SuppressRegistry="true" SuppressRootDirectory="true" AutoGenerateGuids="false" GenerateGuidsNow="true" ToolPath="$(WixToolPath)" Condition="'%(ProjectReference.PackageThisProject)'=='True'" /> </Target>

How do I enable Directory Browsing for an virtual web directory using wix?

I want to enable "Directory Browsing" for the for the following virtual web directory using WIX.
<iis:WebVirtualDir Id="LogsVirDir" Alias="Logs" Directory="ESGLOGFILES" />
How do I accomplish this using WIX?
Wouldn't a simpler solution be to use the web.config system.webserver property like :
<directoryBrowse enabled="true"/>
Based on my research Wix currently does not have any capability to enable Directory Browsing using the standard set of actions. The one way I have found to do this is using a combination of Wix Custom Actions and IIS's Appcmd.exe. Note this command will create a web.config file if one does not exist.
<CustomAction Id="EnableDirectoryBrowsing"
Execute="deferred"
ExeCommand='[WindowsFolder]system32\inetsrv\APPCMD.EXE set config "ESG Website/logs" /section:directoryBrowse /enabled:true'
Directory="TARGETDIR"
Return="check"
Impersonate="no"/>
<InstallExecuteSequence>
<Custom Action="EnableDirectoryBrowsing" Before="InstallFinalize">Not Installed</Custom>
</InstallExecuteSequence>
Im using wix v3.8
try adding ConfigurableDirectory in your Feature
ex: <Feature Id='TestName' Title='Test Web' ConfigurableDirectory='INSTALLDIR' Level='1'>
Use the following code
<Control Id="Browse" Type="PushButton" X="304" Y="210" Width="56" Height="17" Text="!(loc.CustomizeDlgBrowse)">
<Publish Event="SelectionBrowse" Value="BrowseDlg">1</Publish>
</Control>
Take the value of this in the variable you want and use it.

Resources