Automating IIS7 Website creation using impersonation (with Microsoft.Web.Administration)
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开发者_Go百科.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 ServerManager
API 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 is ApplicationPoolIdentity
which 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:
<authentication mode="Windows" />
<identity impersonate="true" />
...and by enabling Windows Authentication
for the IIS application. But it's generally not recommended to do that (see: http://www.hanselman.com/blog/AvoidUsingImpersonationInASPNET.aspx).
It could be that your IISExpress folder is encripted with your user and if you open Visual as administrator, will not have permission to access the file...
C:\Users::your user::\Documents\IISExpress\config
- Go to that folder
- select all files
- right click over the selected files
- click in "Properties" context menu
- click in "Advanced..."
- Remove the check in "Encrypt contents to secure data"
- click OK
and then try again... it worked for me.
精彩评论