1

我们是一个团队,我们每个人都会遇到这种有点随机的错误。该错误在下面列出并出现在以下行: UserPrincipal.FindByIdentity(principalContext, windowsPrincipal.Identity.Name);

它工作得很好几天/几周/几个月,然后我们中的一个人得到这个错误。

在我们的测试服务器上,我们不会像本地机器那样频繁地部署更改,它在我们得到这个错误之前工作了好几个月。

如果我们将应用程序池从 ApplicationPoolIdentity 更改为 NetworkService,则可以。但是,切换回 ApplicationPoolIdentity 后,会出现相同的错误。

IISreset 没有帮助。

重新启动计算机总是可以解决问题,因此 ApplicationPoolIdentity 每天都可以对我们进行身份验证。

这是我们使用的代码(稍作修改):

var windowsPrincipal = principal as WindowsPrincipal;
if (windowsPrincipal == null)
    return null;
try
{
    var principalContext = new PrincipalContext(ContextType.Domain);
    var userPrincipal = UserPrincipal.FindByIdentity(principalContext, windowsPrincipal.Identity.Name);
    if (userPrincipal == null) return null;
    return userPrincipal.Surname;
}

这是错误消息:

An operations error occurred.
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.DirectoryServices.DirectoryServicesCOMException: An operations error occurred.

Source Error:
var principalContext = new PrincipalContext(ContextType.Domain);
var userPrincipal = UserPrincipal.FindByIdentity(principalContext, windowsPrincipal.Identity.Name);

Stack Trace:


[DirectoryServicesCOMException (0x80072020): An operations error occurred.
]
   System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) +628309
   System.DirectoryServices.DirectoryEntry.Bind() +44
   System.DirectoryServices.DirectoryEntry.get_AdsObject() +42
   System.DirectoryServices.PropertyValueCollection.PopulateList() +29
   System.DirectoryServices.PropertyValueCollection..ctor(DirectoryEntry entry, String propertyName) +63
   System.DirectoryServices.PropertyCollection.get_Item(String propertyName) +163
   System.DirectoryServices.AccountManagement.PrincipalContext.DoLDAPDirectoryInitNoContainer() +521413
   System.DirectoryServices.AccountManagement.PrincipalContext.DoDomainInit() +51
   System.DirectoryServices.AccountManagement.PrincipalContext.Initialize() +161
   System.DirectoryServices.AccountManagement.PrincipalContext.get_QueryCtx() +42
   System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithTypeHelper(PrincipalContext context, Type principalType, Nullable`1 identityType, String identityValue, DateTime refDate) +29
   System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(PrincipalContext context, String identityValue) +81
4

1 回答 1

0

如果您不将其丢弃在 finaly 块中,您最终将耗尽资源......

Using (var principalContext = new PrincipalContext(ContextType.Domain)) 
{
var userPrincipal = UserPrincipal.FindByIdentity(principalContext, 
  windowsPrincipal.Identity.Name);
if (userPrincipal == null) return null;
  return userPrincipal.Surname;
}

应该有帮助

于 2013-04-06T14:39:05.477 回答