Try this:
// Create the Server Manager Object:
ServerManager defaultManager = new ServerManager();
// Add the Application-Pool:
ApplicationPool defaultAppPool = defaultManager.ApplicationPools.Add("DefaultAppPool");
// Configure the Pool to Automatically Start.
defaultAppPool.AutoStart = true;
// If IIS Application-Pool Exceeds the CPU Limit Property:
defaultAppPool.Cpu.Action = ProcessorAction.KillW3wp;
// Pipeline:
defaultAppPool.ManagedPipelineMode = ManagedPipeLineMode.Integrated;
// Set Runtime:
defaultAppPool.ManagedRuntimeVersion = "v2.0";
// User Network Service Account:
defaultAppPool.ProcessModel.IdentityType = ProcessModelIdentityType.NetworkService;
// Idle:
defaultAppPool.ProcessModel.IdleTimeout = TimeSpan.FromMinutes(5);
// Max Number of IIS Worker Processes: (W3wp)
defaultAppPool.ProcessModel.MaxProcess = 1;
// Commit the Changes:
defaultManager.CommitChanges();
// Dispose:
defaultManager.Dispose();
It could happen because your not initiating the new ServerManager / Application-Pool. Then when it goes to create the user; it may not be an account that can actually create the user account. If You'd like to validate the application can indeed make those sort of changes also; you could use:
WindowsIdentity userIdentity = WindowsIdentity.GetCurrent();
// Test Operating System Version Vista or Greater for UAC
if (Environment.OSVersion.Platform != PlatformID.Win32NT || Environment.OSVersion.Version.Major < 6)
{
return false;
}
else
{
// If UserIdentity came back Null
if (userIdentity == null)
{
throw new InvalidOperationException("Unable to get current user");
}
else
{
// Set Security Principal to ensure user is in proper role.
WindowsPrincipal userPolicy = new WindowsPrincipal(userIdentity);
if (userPolicy.IsInRole(WindowsBuiltInRole.Administrator))
{
return true;
}
else
{
MessageBox.Show("Application isn't in proper administrative user role; please restart.");
return false;
}
}
}