2017-02-28 656 views
1

在.NET Core中处理Intranet应用程序,我想检索连接到AD用户的信息。目前,所有的身份验证都由Windows处理,并且效果很好。有什么方法可以从AD中提取数据?我想获取诸如姓名,电子邮件,身份证等信息。在ASP .NET Core的Windows身份验证中为用户获取AD信息

+0

我认为现在这是您唯一的选择。 https://github.com/dsbenghe/Novell.Directory.Ldap.NETStandard –

+0

如果你有通过project.json提供的4.6.1框架,你可以使用这个https://vikutech.blogspot.co.uk/2016/07 /ldap-with-aspnet-core-identity-in-mvc-core.html – K7Buoy

+0

@ K7Buoy,这是针对.NET Core的,它与旧的.NET框架包不兼容。虽然谢谢! –

回答

0

经过一周的尝试,我最终使用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文件,充当部署中的调试器。

希望这可以帮助别人!

相关问题