C# 如何对不属于dotnetcore中域用户的用户进行身份验证?
出于安全和管理原因,我们正在一个单独的容器中创建用户条目,将它们添加到一个单独的组中,使该组成为主组,然后将它们从域用户组中删除。我已经完成了所有这些步骤,但是现在我找不到一种方法来验证它们。我不知道是我使用的类还是我的参数,还是什么 假设:C# 如何对不属于dotnetcore中域用户的用户进行身份验证?,c#,active-directory,ldap,C#,Active Directory,Ldap,出于安全和管理原因,我们正在一个单独的容器中创建用户条目,将它们添加到一个单独的组中,使该组成为主组,然后将它们从域用户组中删除。我已经完成了所有这些步骤,但是现在我找不到一种方法来验证它们。我不知道是我使用的类还是我的参数,还是什么 假设: AD_server = fully.qualified.domain container = OU=My Users,OU=Accounts,DC=fully,DC=qualified,DC=domain group = CN=App Users,OU=S
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.SetPasswordpassword 如果您想进一步确保您的身份验证代码不是我怀疑的问题,您可以尝试直接在Windows中进行身份验证-使用该帐户登录Windows,或在命令行中使用:
runas /user:DOMAIN\username cmd
我怀疑您也会遇到同样的问题,这意味着帐户的创建方式存在问题。我还没有用UserPrincipal创建帐户,我用DirectoryEntry创建了帐户,但我注意到创建帐户与设置密码和其他联机示例之间的区别。您可以尝试以下任一方法:
将userPrincipal.SetPasswordpassword移到userPrincipal.Save之前,或
使用,以便在创建帐户时设置密码:
看看这是否有什么不同。你能定义而不成功吗?发生了什么事?一切都以失败告终;DirectoryEntry的方法最直接,8009030C:LDAPPER:DSID-0C09079,注释:AcceptSecurityContext错误,数据52e,V3839错误代码52e:用户名或密码不正确。你是如何创建帐户和设置密码的?是的,我知道-这就是问题所在。我无法让它进行身份验证。因为我在测试中运行这个,所以我有刚刚使用的密码,并且可以通过LDAP浏览器查看条目。我正在尝试理解正确的参数/类/方法,以扭转和验证帐户。好的,这不是答案,但请引导我找到它。我很确定SetPassword不需要在之后保存。然而,实际的问题是我的用户创建没有将帐户设置为Enabled,错误消息故意含糊不清。有趣。我确实想到了这一点,因为如果您创建一个没有密码的帐户,我知道它会自动禁用,您必须专门启用它。但我认为情况并非如此,因为有错误信息。我希望看到错误代码533,这意味着该帐户被禁用。很高兴知道!
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();