Chr*_*len 1 .net c# iis virtual-directory
我继承了一些代码,这些代码在 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();
Run Code Online (Sandbox Code Playgroud)
这一切正常,但现在我需要指定应用程序在特定帐户下运行。我可以在 IIS 中的 Connect As... 对话框中手动更改此设置,并指定所需的域用户名和密码。
不幸的是,我一生都无法弄清楚如何在代码中设置它。我曾尝试以各种方式设置 Username 属性,但 Invoke("AppCreate") 调用总是根据我的尝试抛出某种类型的异常。以下都无法正常工作:
newVirDir.Username = "MYDOMAIN\\MyUser";
newVirDir.Properties["Username"][0] = "MYDOMAIN\\MyUser";
newVirDir.Properties["Username"].Insert(0, "MYDOMAIN\\MyUser");
Run Code Online (Sandbox Code Playgroud)
这一定很容易——要是我知道怎么做就好了!
提前致谢,克里斯
我有一段时间没有这样做了,但我认为您还需要创建一个应用程序池,并在应用程序池级别设置凭据,然后在创建虚拟目录时,确保它使用的是您创建的应用程序池。
这个链接应该让你开始:
http://martinnormark.com/adding-an-application-pool-to-iis7-programmatically
然后,您需要将指定使用网络服务的代码更改为以下代码,您应该可以开始工作了。(这将在应用程序池上设置凭据)
myAppPool.ProcessModel.IdentityType = ProcessModelIdentityType.SpecificUser;
myAppPool.ProcessModel.UserName = "username";
myAppPool.ProcessModel.Password = "password";
Run Code Online (Sandbox Code Playgroud)
确保首先创建应用程序池,然后像这样在虚拟目录上设置应用程序池...
newVirDir.Properties["AppPoolId"][0] = "newAppPoolName";
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4322 次 |
| 最近记录: |