IIS目录服务
问题描述:
我想从Windows Server 2012 R2上的IIS获取网站ID。我想获取Id,然后在IIS中为该网站添加一个网站指针。我不确定在哪里授予代码工作的权限,同时也不会让我的web服务器变得脆弱。IIS目录服务
这里是我取标识
string result = "-1";
DirectoryEntry w3svc = new DirectoryEntry(string.Format("IIS://{0}/w3svc", "localhost"));
foreach (DirectoryEntry site in w3svc.Children)
{
if (site.Properties["ServerComment"] != null)
{
if (site.Properties["ServerComment"].Value != null)
{
if (string.Compare(site.Properties["ServerComment"].Value.ToString(),
"website.com", true) == 0)
{
result = site.Name;
break;
}
}
}
}
return result;
码这是抛出的错误:
Access is denied. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Runtime.InteropServices.COMException: Access is denied. Source Error:
第22行:的DirectoryEntry W3SVC =新 的DirectoryEntry(的String.format (“IIS:// {0}/w3svc”,serverName)); 第23行: 第24行:的foreach(在w3svc.Children的DirectoryEntry站点) 第25行:{ 第26行:如果(!site.Properties [ “ServerComment”] = NULL)
Source File: c:\inetpub\wwwroot\DNS_IIS\DNS.aspx.cs Line: 24 Stack Trace: [COMException (0x80070005): Access is denied. ] System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) +597561 System.DirectoryServices.DirectoryEntry.Bind() +44 System.DirectoryServices.DirectoryEntry.get_IsContainer() +42 System.DirectoryServices.DirectoryEntries.GetEnumerator() +49 DNS.GetWebSiteId(String serverName, String websiteName) in c:\inetpub \wwwroot\DNS_IIS\DNS.aspx.cs:24 DNS.btnDNS_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot \DNS_IIS\DNS.aspx.cs:85 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +155 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3804
答
它是一件很简单的事情。
因此,我只是将ApplicationPool的标识更改为LocalSystem,并且它工作得100%。