我发现问题在于我在角色提供者中使用的 PrincipalSearchers 并不总是与设置中使用的代码联系同一个域控制器。由于域控制器之间的传播延迟,这将导致错误。为了解决这个问题,我使用构造函数注入将设置中使用的 PrincipalContext 提供给角色提供者。这允许角色提供者始终使用与测试代码相同的上下文。此外,我将 PrincipalSearcher 上的 SearchRoot 替换为基于通过构造函数注入提供的 PrincipalContext 的搜索根。相关代码如下。请注意,角色提供者实现 IDisposable 是为了在未从外部提供域上下文时处理域上下文。
private bool DisposeContext { get; set; }
private PrincipalContext DomainContext { get; set; }
public PrintAccountingRoleProvider() : this( null ) { }
public PrintAccountingRoleProvider( PrincipalContext domainContext )
{
this.DisposeContext = domainContext == null;
this.DomainContext = domainContext ?? new PrincipalContext( ContextType.Domain );
}
...
private UserPrincipal FindUser( string userName )
{
using (PrincipalSearcher userSearcher = new PrincipalSearcher())
{
UserPrincipal userFilter = new UserPrincipal( this.DomainContext );
userFilter.SamAccountName = userName;
userSearcher.QueryFilter = userFilter;
// Replace the searcher with one directly associated with the context to ensure that any changes
// made elsewhere will be reflected if we call the search immediately following the change. This
// is critical in the integration tests.
var searcher = userSearcher.GetUnderlyingSearcher() as DirectorySearcher;
searcher.SearchRoot = new DirectoryEntry( @"LDAP://" + this.DomainContext.ConnectedServer + @"/dc=iowa,dc=uiowa,dc=edu" );
return userSearcher.FindOne() as UserPrincipal;
}
}
...
private void Dispose( bool disposing )
{
if (!this.disposed)
{
if (disposing)
{
if (this.DisposeContext && this.DomainContext != null)
{
this.DomainContext.Dispose();
this.DomainContext = null;
}
}
this.disposed = true;
}
}