简体   繁体   中英

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.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

  1. Go to that folder
  2. select all files
  3. right click over the selected files
  4. click in "Properties" context menu
  5. click in "Advanced..."
  6. Remove the check in "Encrypt contents to secure data"
  7. click OK

and then try again... it worked for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM