简体   繁体   中英

LDAP Bind through .NET DirectoryEntry always succeeds

I am looking up an arbitrary Windows account in a forms-based C# login scenario using the user's e-mail address as the key. Locating the user works fine and I get back my derived UserPrincipalEx fine.

However, when I try to validate a login using a Bind, it always succeeds:

// we can't use ValidateCredentials because it's too broken - multiple attempts each time,
// can't always negotiate properly, etc.
//if (!_principalContext.ValidateCredentials(userPrincipal.UserPrincipalName, password)) {
//    return LoginValidationResults.ValidationFailed;
//}
try {
    using (var directoryEntry = new DirectoryEntry("LDAP://" + _domain + "/" + _principalContext.Container,
        userPrincipal.UserPrincipalName, password, AuthenticationTypes.FastBind)) {
        var forceBind = directoryEntry.NativeObject;
        Log.DebugFormat("Validation successful ({0}).", forceBind);
        return LoginValidationResults.Valid;
    }                    
}
catch (COMException ex) {
    if (ex.ErrorCode != -2147023570) {
        Log.DebugFormat("Validation exception: {0}", ex.ToString());
        throw;
    }
    Log.Debug("Validation failed.");
    return LoginValidationResults.ValidationFailed;
}

In some cases - and I can't figure out what they are yet - the account always binds successfully, no matter what password I give.

Why could this be?

So I looked into this using some packet captures and saw that all of the succeeding binds were using a blank username.

Sure enough, the users in question had a blank (actually null - not set) UPN. This should never happen in our scenario but it did. Because it's a broken user case, I just detect that and throw an exception.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM