如何对不属于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.ValideCredentials((方法,但没有成功。

创建期间:

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:DOMAINusername cmd

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

  1. userPrincipal.SetPassword(password)移动到userPrincipal.Save()之前,或者
  2. 使用接受密码的构造函数,以便在创建帐户时设置密码:
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();

看看这是否有什么不同。

相关内容

最新更新