c# - 如何在 dotnetcore 中对不属于域用户的用户进行身份验证?
问题描述
出于安全和管理原因,我们在单独的容器中创建用户条目,将它们添加到单独的组中,使该组成为主要组,然后将它们从域用户组中删除。我的这些步骤都在工作,但现在我找不到验证它们的方法。我不知道是我正在使用的课程还是我的论点,还是什么。
假设:
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
)创建帐户,但我注意到您正在做的事情(创建帐户然后设置密码)与其他在线示例之间存在差异。您可以尝试:
- 移到
userPrincipal.SetPassword(password)
之前userPrincipal.Save()
,或 - 使用接受密码的构造函数,以便在创建帐户时设置密码:
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();
看看这是否有什么不同。
推荐阅读
- angular - 在订阅中返回布尔值的替代方法
- sql - sql中的2列在另一列之下
- javascript - 有没有办法从 json 中获取一个值作为 react native 中的变量
- python-3.x - 如何将两个列表中的所有条目分成一个
- javascript - 是否应该在生产包中删除 javascript 注释?
- c++ - std::atomic 的内存排序
在某些 CPU 上 - kubernetes - 我们如何限制对 Istio 服务的内部请求?
- r - 如何在不使用 R 循环的情况下编写此指标矩阵
- windows - 如何使用 Go 在 Windows 中查找文件创建日期
- sql - Oracle SQL 在多个表中的多个子查询上查找匹配记录