I’m trying to create a website in IIS using this code (from another site on the same server)
using Microsoft.Web.Administration;
//...
ServerManager manager = new ServerManager();
manager.Sites.Add(Host, "http", ":80:" + Host, Path);
//...
But I get this exception:
MESSAGE: Filename: redirection.config
Error: Cannot read configuration file due to insufficient permissions
I’ve tried impersonation like this: http://www.codeproject.com/KB/cs/zetaimpersonator.aspx
but then I get an ugly COM error.
What is the best way to impersonate the correct user, or specify a user to run the manager as.
In order to use the
ServerManagerAPI you need to be a local administrator on the machine that is being managed. As your code runs in the context of an IIS app it’s this app’s application pool identity which you need to change to an administrative user (the default identity on IIS 7.x isApplicationPoolIdentitywhich is a non-privileged user).Alternatively, you could enable ASP.NET impersonation by adding these elements to the
<system.web>section in the Web.config:…and by enabling
Windows Authenticationfor the IIS application. But it’s generally not recommended to do that (see: http://www.hanselman.com/blog/AvoidUsingImpersonationInASPNET.aspx).