【问题标题】:Get domain chosen by PrincipalContext获取 PrincipalContext 选择的域
【发布时间】:2013-08-18 14:20:09
【问题描述】:

我正在为 asp.net 创建一个自定义登录功能,该功能针对活动目录进行验证。用户必须能够仅使用他的用户名或使用他的用户名和域(以及两种情况下的密码)登录。

代码:

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);
}

“AuthUser”包含用户名和域(如果给定)。现在,如果用户没有明确指定域,上述仍然可以正常工作。

所以如果你打电话

new PrincipalContext(ContextType.Domain);

域好像是自动设置的。

在这种情况下,我怎样才能找到它使用的域?

【问题讨论】:

    标签: c# asp.net login active-directory


    【解决方案1】:

    您始终可以从 UserPrincipal.FindByIdentity() 返回的用户主体中获取使用的域

    【讨论】:

    • 现在由UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(network, IdentityType.SamAccountName, user.username); string distinguishedName = userPrincipal.DistinguishedName 获得。然后提取并加入该字符串的所有“DC”部分以获得完整的域。
    • 如果您仍然需要答案:它在我原来的问题中定义为“network = new PrincipalContext(ContextType.Domain, user.domain);”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-01
    • 1970-01-01
    相关资源
    最近更新 更多