【问题标题】:.GetDirectoryEntry throws COM exception, code:0x800720720 when attempting to bind to object.GetDirectoryEntry 尝试绑定对象时抛出 COM 异常,代码:0x800720720
【发布时间】:2014-06-17 18:56:34
【问题描述】:

我的应用程序在 IIS 7.0 上运行,它应该模拟经过身份验证的用户并解锁或重置其他用户帐户。当我在我的工作站上开发它时它运行良好,但是当我将它上传到服务器时,模拟停止工作,它不会绑定到 AD 对象并继续抛出相同的异常。我之前在使用 PrincipalContext 时遇到了同样的问题,但我能够使用 using(HostingEnvironment.Impersonate()) 解决这个问题,因为我不需要经过身份验证的用户来执行该操作。但是现在我这样做了,我无法使用该解决方法。我需要对这个问题进行实际修复,我非常感谢一些输入。我一直在广泛寻找解决问题的方法,但到目前为止,它们都没有奏效。这是我正在使用的不断抛出异常的代码。

using (DirectorySearcher search = new DirectorySearcher(directoryEntries[counter]))
{
    //Sets the filter to find a user object with the target user username
    search.Filter = "(&(objectClass=user)(sAMAccountName=" + username + "))";
    //Sets the searchscope to search the whole AD
    search.SearchScope = SearchScope.Subtree;
    //Executes the search for one result and stores it in result.
    SearchResult result = search.FindOne();
    //Creates a directory entry from the result.

    using (DirectoryEntry targetUser = result.GetDirectoryEntry())
    {
        //This if-else statement checks if the user is locked, if it is then
        //the unlock is performed, and the unlockPerformed variable is set to
        //true, if it isn't then unlockPerformed is set to false.
        if (Convert.ToBoolean(targetUser.InvokeGet("IsAccountLocked")))
        {
            targetUser.InvokeSet("IsAccountLocked", false);
            targetUser.CommitChanges();
            unlockPerformed = true;
        }
        else
        {
            unlockPerformed = false;
        }
    }
}

此代码在我上传之前运行良好,非常感谢任何建议,我将对此进行监控,以便尽快修复。 提前致谢。

更新:修复了问题

根据这篇文章:http://msdn.microsoft.com/en-us/library/vstudio/ms730088(v=vs.100).aspx,显然从主机而不是远程机器运行的程序实际上是一个非常明显的症状。 根据那篇文章,问题在于模拟设置被设置为导致这种行为的模拟,我想要 DELEGATION。为了做到这一点,我使用this 页面来获取有关委派和模拟的不同方法的信息,我使用了“临时模拟原始呼叫者”部分。

在 web.config 文件中:

 <identity impersonate="false"/>

如果将其设置为 false,那么它会尝试在每个操作中模拟用户,这可能会导致我遇到的问题,而不是我想要实现的问题。

在代码中:

using System.Security.Principal;
...
// Obtain the authenticated user's Identity
WindowsIdentity winId = (WindowsIdentity)HttpContext.Current.User.Identity;
WindowsImpersonationContext ctx = null;
try
{
  // Start impersonating
  ctx = winId.Impersonate();
  // Now impersonating
  // Access resources using the identity of the authenticated user
}
// Prevent exceptions from propagating
catch
{
}
finally
{
  // Revert impersonation
  if (ctx != null)
    ctx.Undo();
}
// Back to running under the default ASP.NET process identity

这个修复相当简单,但是几乎不可能找到关于这个主题的清晰信息,我希望有一天有人会发现这很有用,我不能是唯一遇到这些问题的人。

【问题讨论】:

  • 有足够的代表发表答案吗?

标签: asp.net iis-7 active-directory comexception


【解决方案1】:

根据这篇文章:http://msdn.microsoft.com/en-us/library/vstudio/ms730088(v=vs.100).aspx,显然从主机而不是远程机器运行的程序实际上是一个非常明显的症状。根据那篇文章,问题在于模拟设置被设置为导致这种行为的模拟,我想要 DELEGATION。为了做到这一点,我使用此页面来获取有关委派和模拟的不同方法的信息,我使用了“临时模拟原始呼叫者”部分。

在 web.config 文件中:

&lt;identity impersonate="false"/&gt;

如果将其设置为 false,那么它会尝试在每个操作中模拟用户,这可能会导致我遇到的问题,而不是我想要实现的问题。

在代码中:

using System.Security.Principal;
...
// Obtain the authenticated user's Identity
WindowsIdentity winId = (WindowsIdentity)HttpContext.Current.User.Identity;
WindowsImpersonationContext ctx = null;
try
{
  // Start impersonating
  ctx = winId.Impersonate();
  // Now impersonating
  // Access resources using the identity of the authenticated user
}
// Prevent exceptions from propagating
catch
{
}
finally
{
  // Revert impersonation
  if (ctx != null)
    ctx.Undo();
}
// Back to running under the default ASP.NET process identity

这个修复相当简单,但是几乎不可能找到关于这个主题的清晰信息,我希望有一天有人会发现这很有用,我不能是唯一遇到这些问题的人。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-09
    • 2011-11-23
    • 2015-07-16
    • 2015-03-20
    • 2012-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多