我继承了一些代码,这些代码使用类似于以下的代码(为简洁起见整理)在 IIS 7.0 下创建了几个应用程序
DirectoryEntry iisServer = new DirectoryEntry("IIS://localhost/W3SVC/1");
DirectoryEntry folderRoot = iisServer.Children.Find("Root", "IIsWebVirtualDir");
DirectoryEntry newVirDir = folderRoot.Children.Add("MyNewVirtualDir", "IIsWebVirtualDir");
newVirDir.Properties["AccessRead"][0] = true;
newVirDir.Properties["AccessScript"][0] = true;
newVirDir.Properties["Path"].Insert(0, "C:\\MyVirtualDirStuff");
newVirDir.Properties["AccessExecute"][0] = true;
newVirDir.Properties["AppIsolated"].Value = 2; //"Medium (Pooled)"
newVirDir.Properties["DefaultDoc"][0] = "index.htm";
newVirDir.Invoke("AppCreate", true);
newVirDir.CommitChanges();
folderRoot.CommitChanges();
iisServer.CommitChanges();
一切正常,但现在我需要指定应用程序在特定帐户下运行。我可以在 IIS 中的“连接为...”对话框中手动更改它并指定所需的域用户名和密码。
不幸的是,我终生无法弄清楚如何在代码中设置它。 我尝试过以各种方式设置 Username 属性,但 Invoke("AppCreate") 调用总是抛出某种类型的异常,具体取决于我尝试过的内容。以下均无效:
newVirDir.Username = "MYDOMAIN\\MyUser";
newVirDir.Properties["Username"][0] = "MYDOMAIN\\MyUser";
newVirDir.Properties["Username"].Insert(0, "MYDOMAIN\\MyUser");
这一定很容易 - 要是我知道怎么做就好了!
提前致谢, 克里斯
请您参考如下方法:
我有一段时间没有这样做了,但我认为您还需要创建一个应用程序池,并在应用程序池级别设置凭据,然后在创建虚拟目录时,确保它使用的是应用程序池你创造了。
这个链接应该让你开始:
http://martinnormark.com/adding-an-application-pool-to-iis7-programmatically
然后,您需要将指定使用网络服务的代码更改为以下内容,您应该开始工作了。 (这将在应用程序池上设置凭据)
myAppPool.ProcessModel.IdentityType = ProcessModelIdentityType.SpecificUser;
myAppPool.ProcessModel.UserName = "username";
myAppPool.ProcessModel.Password = "password";
确保先创建应用程序池,然后像这样在虚拟目录上设置 AppPool...
newVirDir.Properties["AppPoolId"][0] = "newAppPoolName";
