Я пытаюсь получить идентификатор сайта из IIS на Windows Server 2012 R2. Я хочу получить идентификатор, а затем добавить указатель на веб-сайт в IIS. Я не уверен, где предоставить разрешения для работы кода и в то же время, не оставляя мой веб-сервер уязвимым.Службы IIS Directory
Вот мой код для извлечения Id
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: Еогеасп (DirectoryEntry сайт w3svc.Children) Line 25: { Line 26: если (! Site.Properties [ "ServerComment"] = нуль)
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