How to read/write administration.config
class Program
{
static void Main(string[] args)
Configuration administrationConfig = sm.GetAdministrationConfiguration();
ConfigurationSection moduleProvidersSection = administrationConfig.GetSection("moduleProviders");
ConfigurationElementCollection moduleProvidersCollection = moduleProvidersSection.GetCollection();
foreach (ConfigurationElement moduleProviderElement in moduleProvidersCollection)
{
Console.WriteLine(moduleProviderElement.GetAttribute("name").Value);
}
}
MWA achieves this by setting a pathMapper to map MACHINE/WEBROOT configuration path to administration.config instead of root web.config (it uses a different AdminManager for root web.config). You can write a pathMapper yourself and use AppHostAdminLibrary directly to read or write administration.config. Program below uses a simple pathMapper to map MACHINE/WEBROOT to administration.config and then prints UI module count.
using AppHostAdminLibrary;
IAppHostElement modulesElement = configManager.GetAdminSection(
Console.WriteLine(modulesElement.Collection.Count);
public class MyPathMapper : IAppHostPathMapper
if (bstrConfigPath.Equals("MACHINE/WEBROOT", StringComparison.OrdinalIgnoreCase))
return physicalPath;
In windows server 2008 you can also implement IAppHostPathMapper2 and then set “pathMapper2” metadata on IAppHostAdminManager. IAppHostPathMapper2 allows you to return the impersonation token with the physical path mapping which is then used by the configuration system to read the configuration file. Also, native configuration system has an in-built pathMapper which maps MACHINE/WEBROOT to administration.config. Sample program below sets this in-built pathMapper and then creates an IIS manager user.
using System;
IAppHostElement authenticationSection = ahadmin.GetAdminSection(
IAppHostElement credentialsElement = authenticationSection.GetElementByName("credentials");
newElement.Properties["name"].Value = "newuser";
//
// This is required by UI
// Plain text passwords won't work for IIS Manager users
byte f1 = 0xf0;
hexString = hexString + ((first4 > 9) ? (char)('A' + (first4 - 10)) : (char)('0' + first4));
newElement.Properties["password"].Value = hexString;
credentialsCollection.AddElement(newElement, -1);
You can also use Microsoft.Web.Managerment.Server.ManagementAuthentication.CreateUser("user", "password") to create an IIS Manager user. This will also make sure that user is saved to the right provider which might or might not be administration.config.
-Kanwal