如何验证不属于 dotnetcore 中域用户的用户?

How can I authenticate a user that does not belong to Domain Users in dotnetcore?

出于安全和管理原因,我们在单独的容器中创建用户条目,将它们添加到单独的组,使该组成为主要组,然后将它们从 Domain Users 组中删除。我已经完成了所有这些步骤,但现在我找不到对它们进行身份验证的方法。我不知道这是我使用的 类 还是我的论点,还是什么。

假设:

AD_server = fully.qualified.domain
container = OU=My Users,OU=Accounts,DC=fully,DC=qualified,DC=domain
group = CN=App Users,OU=Software,DC=fully,DC=qualified,DC=domain
user = CN=Example,OU=Software,DC=fully,DC=qualified,DC=domain
sAMAccountName = Example
userPrincipalName = Example@MyUsers.fully.qualified.domain

我试过了

new LdapConnection(
  new LdapDirectoryIdentifier(AD_server)
).Bind(
  new NetworkCredential(userPrincipalName,password)
)

new LdapConnection(
  new LdapDirectoryIdentifier(AD_server)
).Bind(
  new NetworkCredential(sAMAccountName,password)
)

new DirectoryEntry(
  "LDAP://"+AD_server+"/"+container,
  sAMAccountName,
  password
)

new DirectoryEntry(
  "LDAP://"+AD_server+"/"+container,
  userPrincipalName ,
  password
)

new DirectoryEntry(
  "LDAP://"+AD_server+"/"+group,
  sAMAccountName,
  password
)

new DirectoryEntry(
  "LDAP://"+AD_server+"/"+group,
  userPrincipalName ,
  password
)

new DirectoryEntry(
  "LDAP://"+AD_server+"/"+group,
  user,
  password
)

我也尝试过使用 PrincipalContext.ValidateCredentials() 方法但没有成功。

创建期间:

this.principalContext = new PrincipalContext(ContextType.Domain,
  AD_server, // serves as domain and AD server
  container,
  adminUser,
  adminPassword);

管理员凭据有权对域进行更改。 创建用户(从传输对象开始):

UserPrincipal userPrincipal =
    UserPrincipal.FindByIdentity(principalContext, user.UserId + upnSuffix);
if (null == userPrincipal)
{
    userPrincipal = new UserPrincipal(principalContext)
    {
        SamAccountName = user.UserId,
        DisplayName = user.FullName,
        GivenName = user.FirstName,
        Surname = user.LastName,
        EmailAddress = user.Email,
        UserPrincipalName = user.UserId + upnSuffix,
        PasswordNeverExpires = true
    };
    //create user
    userPrincipal.Save();
}
return userPrincipal;

设置密码:userPrincipal.SetPassword(password);

如果您想进一步确定您的身份验证码不是问题所在(我怀疑不是),您可以尝试直接在 Windows 中进行身份验证 - 使用该帐户登录 Windows , 或者在命令行中使用 runas:

runas /user:DOMAIN\username cmd

我怀疑您也会遇到同样的问题,这意味着帐户的创建方式存在问题。我没有使用 UserPrincipal 创建帐户(我使用 DirectoryEntry 创建帐户),但我注意到您正在做的事情(创建帐户和 然后 之间存在差异设置密码)和其他在线示例。您可以试试:

  1. userPrincipal.SetPassword(password)移动到userPrincipal.Save()之前,或者
  2. 使用 constructor that accepts the password 以便在创建帐户时设置密码:
userPrincipal = new UserPrincipal(principalContext, user.UserId, password, true)
{
    DisplayName = user.FullName,
    GivenName = user.FirstName,
    Surname = user.LastName,
    EmailAddress = user.Email,
    UserPrincipalName = user.UserId + upnSuffix,
    PasswordNeverExpires = true
};
//create user
userPrincipal.Save();

看看有什么不同。