1

我的应用程序在 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。为此,我使用页面获取有关委派和模拟的不同方法的信息,我使用了“临时模拟原始呼叫者”部分。

在 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

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

4

1 回答 1

1

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

在 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

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

于 2014-06-19T15:41:39.973 回答