简体   繁体   中英

Get domain chosen by PrincipalContext

I'm creating a custom login functionality for asp.net which validates against active directory. The user must be able to login with only his username or with his username and a domain (and a password in both cases).

Code:

AuthUser user = Authentication.getDomainAndUserName(givenUsername);

bool validAccount = false;

PrincipalContext network = null;
if (user.domain != "") network = new PrincipalContext(ContextType.Domain, user.domain);
else network = new PrincipalContext(ContextType.Domain);

if (UserPrincipal.FindByIdentity(network, IdentityType.SamAccountName, user.username) != null) {
      validAccount = network.ValidateCredentials(givenUsername, givenPassword, ContextOptions.Negotiate);
}

The "AuthUser" contains the username and, if given, the domain. Now if a user didn't explicitly specify the domain the above still works fine.

So if you call

new PrincipalContext(ContextType.Domain);

It seems that the domain is set automatically.

In that case, how can i find out the domain it used?

您总是可以从UserPrincipal.FindByIdentity()返回的用户主体中获得使用的域。

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