我有一個應用程序需要綁定到遠程客戶的Active Directory來執行身份驗證任務。UserPrincipal.FindByIdentity拋出PrincipalServerDownException
using (var ctx = new PrincipalContext(ContextType.Domain, "customer.org", "ou=people,dc=customer,dc=org", ContextOptions.SecureSocketLayer | ContextOptions.SimpleBind, "[email protected]", "password"))
{
var user = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, username); // after several seconds, throws PrincipalServerDownException
if (user == null) return null; // user doesn't exist
// check if the account is locked out, etc. (omitted)
// quickly validate credentials
if (!ctx.ValidateCredentials(username, password, ContextOptions.SecureSocketLayer | ContextOptions.SimpleBind)) return null; // bad credentials
return user;
}
例外情況是:
PrincipalServerDownException:服務器不可操作。
at System.DirectoryServices.AccountManagement.ADStoreCtx.GetAsPrincipal(Object storeObject, Object discriminant)
at System.DirectoryServices.AccountManagement.ADStoreCtx.FindPrincipalByIdentRefHelper(Type principalType, String urnScheme, String urnValue, DateTime referenceDate, Boolean useSidHistory)
at System.DirectoryServices.AccountManagement.ADStoreCtx.FindPrincipalByIdentRef(Type principalType, String urnScheme, String urnValue, DateTime referenceDate)
at System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithTypeHelper(PrincipalContext context, Type principalType, Nullable`1 identityType, String identityValue, DateTime refDate)
at System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithType(PrincipalContext context, Type principalType, IdentityType identityType, String identityValue)
at System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(PrincipalContext context, IdentityType identityType, String identityValue)
直到今天,事情做工精細。一個變化是運行此代碼的應用程序從4升級到4.5。我無法確定在升級後是否立即發生問題,或者這只是一個巧合。
我一直在使用AdFind來測試客戶的AD綁定,它似乎工作正常。
另一個有趣的事情是,PrincipalContext
初始化就好了(因此驗證其對遠程存儲連接),如果我註釋掉FindByIdentity
調用,這樣只是ctx.ValidateCredentials
被調用時,工作正常了。
看到http://stackoverflow.com/questions/12608971/net-4-5- bug-in-userprincipal-findbyidentity-system-directoryservices-accountma?rq = 1 – 2013-04-23 15:23:55