[英]How to set a specific user on IIS Application
I have inherited some code that creates a couple of applications under IIS 7.0 using code similar to the following (tidied up for brevity) 我继承了一些代码,使用类似于以下的代码在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();
This all works correctly but now I have a need to specify that the Application runs under a specific account. 这一切都正常,但现在我需要指定应用程序在特定帐户下运行。 I can change this manually in IIS in the Connect As... dialog and specify the required domain username and password.
我可以在IIS中的Connect As ...对话框中手动更改此设置,并指定所需的域用户名和密码。
Unfortunately, I cannot for the life of me work out how to set this in the code. 不幸的是,我不能为我的生活弄清楚如何在代码中设置它。 I have tried setting the Username property in various ways but the Invoke("AppCreate") call always throws an Exception of some type dependent on what I've tried.
我已尝试以各种方式设置Username属性,但Invoke(“AppCreate”)调用总是抛出一些取决于我尝试过的类型的异常。 The following have all failed to work:
以下都没有奏效:
newVirDir.Username = "MYDOMAIN\\MyUser";
newVirDir.Properties["Username"][0] = "MYDOMAIN\\MyUser";
newVirDir.Properties["Username"].Insert(0, "MYDOMAIN\\MyUser");
It must be easy - if only I knew how to do it! 这一定很容易 - 只要我知道怎么做!
Thanks in advance, Chris 谢谢,克里斯
I haven't done this in awhile, but I think you need to also create an ApplicationPool, and set the credentials at the Application Pool level, then when you create your virtual directory, make sure it is using the Application Pool that you created. 我暂时没有这样做,但我认为您还需要创建一个ApplicationPool,并在应用程序池级别设置凭据,然后在创建虚拟目录时,确保它使用您创建的应用程序池。
This Link should get you started: 这个链接应该让你开始:
http://martinnormark.com/adding-an-application-pool-to-iis7-programmatically http://martinnormark.com/adding-an-application-pool-to-iis7-programmatically
Then, you'll need to change the code where it specifies use the Network Service, to the following, and you should be in business. 然后,您需要将其指定使用网络服务的代码更改为以下内容,您应该开展业务。 (this will set the credentials on the app pool)
(这将在应用程序池上设置凭据)
myAppPool.ProcessModel.IdentityType = ProcessModelIdentityType.SpecificUser;
myAppPool.ProcessModel.UserName = "username";
myAppPool.ProcessModel.Password = "password";
Make sure you create the Application Pool first, then set the AppPool on the Virtual Directory like this... 确保首先创建应用程序池,然后像这样在虚拟目录上设置AppPool ......
newVirDir.Properties["AppPoolId"][0] = "newAppPoolName";
Virtual directory Credential 虚拟目录凭据
newVirDir.Properties["AuthFlags"].Value = 5;
newVirDir.Properties["UNCUsername"].Value = "MYDOMAIN\\MyUser";
newVirDir.Properties["UNCPassword"].Value = "password";
Well, I've found a way of doing it - whether it's the recommended way I don't know. 好吧,我找到了一种方法 - 不管它是我不知道的推荐方式。
Using this link as a start: http://www.iis.net/ConfigReference/system.applicationHost/sites/site/application/virtualDirectory 使用此链接作为开头: http : //www.iis.net/ConfigReference/system.applicationHost/sites/site/application/virtualDirectory
I've now got this, which seems to do exactly what I want: 我现在有了这个,这似乎完全符合我的要求:
using (ServerManager serverManager = new ServerManager())
{
Configuration config = serverManager.GetApplicationHostConfiguration();
ConfigurationSection sitesSection = config.GetSection("system.applicationHost/sites");
ConfigurationElementCollection sitesCollection = sitesSection.GetCollection();
ConfigurationElement siteElement = FindElement(sitesCollection, "site", "name", @"Default Web Site");
ConfigurationElementCollection applicationCollection = siteElement.GetCollection();
ConfigurationElement applicationElement = FindElement(applicationCollection, "application", "path", @"/MyNewVirtualDir");
ConfigurationElementCollection virtualDirCollection = applicationElement.GetCollection();
ConfigurationElement virtualDirElement = FindElement(virtualDirCollection, "virtualDirectory", "path", @"/");
virtualDirElement.Attributes["userName"].Value = "MYDOMAIN\\MyUser";
virtualDirElement.Attributes["password"].Value = "MyPassword";
serverManager.CommitChanges();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.