【问题标题】:ASP.NET - Accessing Active Directory from code behind pageASP.NET - 从页面后面的代码访问 Active Directory
【发布时间】:2013-06-17 13:52:46
【问题描述】:

我制作了一个 Web 应用程序,它可以从 Active Directory 读取/写入数据。在我的 web.config 文件中有

<identity impersonate="true"/>

<authentication mode="Windows"/>

当我显示时

System.Web.HttpContext.Current.User.Identity.Name

在某些标签中,它显示mydomain\myusername,所以我认为模拟有效。

现在回答问题。当我访问运行 IIS Web 服务器的服务器上的应用程序时,一切正常。但是当我从远程 PC 访问 Web 应用程序时,我得到一个异常(标签仍然显示“mydomain\myusername”)。

我已经找到了问题所在。在我调用时的代码中

Forest currentForest = Forest.GetCurrentForest();

变量currentForest 知道它的currentForest.NamecurrentForest.RootDomaincurrentForest.ForestMode,但任何对currentForest.DomainscurrentForest.SitescurrentForest.GlobalCatalogs 的调用都会导致

System.DirectoryServices.ActiveDirectory.ActiveDirectoryOperationException。

现在我迷路了,不知道要进一步调试什么。我使用的帐户是 Enterprise Admins(多域林)的成员。我已经在两台具有不同 IIS 版本(IIS 7.5 和 IIS 6.0)的不同服务器上尝试过,但没有成功。

并且抛出的异常没有多大帮助:

异常详细信息:System.DirectoryServices.DirectoryServicesCOMException:发生操作错误。
源错误:在执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常起源和位置的信息。

堆栈跟踪:
DirectoryServicesCOMException (0x80072020):发生操作错误。

System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) +557
System.DirectoryServices.DirectoryEntry.Bind() +44
System.DirectoryServices.DirectoryEntry.get_AdsObject() +42
System.DirectoryServices.DirectorySearcher.FindAll(Boolean findMoreThanOne) +98
System.DirectoryServices.ActiveDirectory.ADSearcher.FindAll() +46
System.DirectoryServices.ActiveDirectory.Forest.GetDomains() +543

[ActiveDirectoryOperationException: 发生操作错误。]

System.DirectoryServices.ActiveDirectory.Forest.GetDomains() +512484
System.DirectoryServices.ActiveDirectory.Forest.get_Domains() +44
C:\Documents and Settings\myUser\documents\visual studio\Projects\MyWebApp\MyWebApp\ASPPage.aspx.cs:158 中的 myWebApp.ASPpage.Button_Click(Object sender, EventArgs e)
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +115
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +140
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +29
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2981

编辑:如果不明显,我想使用 currentForest.Domains 在整个林(所有域)中搜索由 UPN 名称指定的用户。

【问题讨论】:

    标签: asp.net active-directory


    【解决方案1】:

    我在本页的“相关问题”之一中找到了我的问题的答案。答案就在这个话题中:Why does DirectoryServicesCOMException occur querying Active Directory from a machine other than the web server?

    我发现,这正是我的情况。在阅读了建议的 Microsoft 文章后,我了解到,模拟仅适用于 IIS 服务器上的本地资源。要访问网络资源(SQL、Active Directory),我必须在 Active Directory 的计算机对象中设置“信任此计算机以进行委派”。

    【讨论】:

      【解决方案2】:

      这是因为 IIS 始终进行身份验证,因此 System.Web.HttpContext.Current.User.Identity.Name 显示mydomain\myusername; 以验证您可以使用的模拟是否有效: System.Security.Principal.WindowsIdentity.GetCurrent().NameSystem.Security.Principal.WindowsIdentity.GetCurrent(true) 在不模拟时也返回 null。

      【讨论】:

        猜你喜欢
        • 2012-03-18
        • 2021-09-30
        • 1970-01-01
        • 2011-04-16
        • 1970-01-01
        • 2012-08-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多