简体   繁体   English

IIS 目录服务

[英]IIS Directory Services

I am trying to get the website id from IIS on Windows Server 2012 R2.我正在尝试从 Windows Server 2012 R2 上的 IIS 获取网站 ID。 I want to fetch the Id, then add a website pointer for the website in IIS.我想获取 Id,然后在 IIS 中为该网站添加一个网站指针。 I'm not sure where to grant the permissions for the code to work and at the same time, not leaving my web server vulnerable.我不确定在哪里授予代码工作的权限,同时又不会让我的网络服务器容易受到攻击。

Here is my code for fetching Id这是我获取 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;

This is the error that is thrown:这是抛出的错误:

 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:

Line 22: DirectoryEntry w3svc = new DirectoryEntry(string.Format("IIS://{0}/w3svc", serverName));第 22 行:DirectoryEntry w3svc = new DirectoryEntry(string.Format("IIS://{0}/w3svc", serverName)); Line 23: Line 24: foreach (DirectoryEntry site in w3svc.Children) Line 25: { Line 26: if (site.Properties["ServerComment"] != null)第 23 行:第 24 行:foreach(w3svc.Children 中的 DirectoryEntry 站点)第 25 行:{第 26 行:if (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

It was a simple thing to sort out.整理是一件很简单的事情。

So I just changed the Identity of the ApplicationPool to LocalSystem and it worked 100%.所以我只是将 ApplicationPool 的标识更改为 LocalSystem 并且它 100% 工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM