使用.NET Core中的Intranet应用程序,我想检索连接到AD用户的信息。目前,所有身份验证都由Windows处理并且运行良好。有没有办法从AD中提取数据?我想获得姓名,电子邮件,身份证等信息。
答案 0 :(得分:5)
Using.net core 2.1.1
从NuGet安装“ System.DirectoryServices”
using System.DirectoryServices;
var name = User.Identity.Name.Split('\\')[1]; *@I was getting name as domain\\name @*
DirectorySearcher ds = new DirectorySearcher();
ds.Filter = "(&(objectClass=user)(objectcategory=person)(name=" + name + "))";
SearchResult userProperty = ds.FindOne();
var userEmail = userProperty.Properties["mail"][0];
var userName = userProperty.Properties["displayname"][0];
答案 1 :(得分:2)
经过一周的尝试,我终于使用Novell.Directory.Ldap软件包取得了进展。排除故障要容易得多,我不必担心运行双重框架。
首先,转到程序包管理器控制台并键入:
Install-Package Novell.Directory.Ldap
这会将包加载到项目中并将其添加到project.json中。
有一些例子,但在看了大部分之后,它们并不是我真正需要的。我最终得到了以下代码:
var logPath = System.IO.Path.GetTempFileName();
var logWriter = System.IO.File.CreateText(logPath);
var user = "cn="+User.Identity.Name.Split('\\')[1];
logWriter.WriteLine("Current Ldap results:");
LdapConnection ADconn = new LdapConnection();
ADconn.Connect("DC IP address", 389);
ADconn.Bind("DOMAIN\\username", "password");
logWriter.WriteLine(ADconn.GetSchemaDN());
LdapSearchResults lsc = ADconn.Search("ou=OrgUnit,dc=DOMAIN,dc=com",
LdapConnection.SCOPE_SUB,
user, attrs, false);
while (lsc.hasMore())
{
LdapEntry nextEntry = null;
try
{
nextEntry = lsc.next();
}
catch (LdapException e)
{
logWriter.WriteLine("Error: " + e.LdapErrorMessage);
//Exception is thrown, go for next entry
continue;
}
DisplayName = nextEntry.getAttribute("displayName").StringValue;
UserADId = new Guid((byte[])(Array)nextEntry.getAttribute("objectGuid").ByteValue).ToString();
EMail = nextEntry.getAttribute("mail").StringValue;
logWriter.WriteLine(DisplayName);
logWriter.WriteLine(UserADId);
logWriter.WriteLine(EMail);
}
logWriter.Dispose();
//Procced
//While all the entries are parsed, disconnect
ADconn.Disconnect();
使用Windows身份验证,可以从AD中提取用户的属性。拉出后,您可以将它们分配给变量并使用它们!它还在C:\ Windows \ Temp \文件夹中创建一个TMP文件,该文件在部署中充当调试器。
希望这有助于其他人!