【问题标题】:Get AD info for user in Windows Authentication for ASP .NET Core在 ASP .NET Core 的 Windows 身份验证中获取用户的 AD 信息
【发布时间】:2018-11-20 00:29:02
【问题描述】:

在 .NET Core 中处理 Intranet 应用程序,我想检索与 AD 用户相关的信息。目前,所有身份验证都由 Windows 处理并且效果很好。有没有办法从 AD 中提取数据?我想获取名字和姓氏、电子邮件、ID 等信息。

【问题讨论】:

  • 我认为目前这是您唯一的选择。 github.com/dsbenghe/Novell.Directory.Ldap.NETStandard
  • 如果您通过 project.json 获得 4.6.1 框架,您可以使用 vikutech.blogspot.co.uk/2016/07/…
  • @K7Buoy,这是针对 .NET Core 的,它与旧的 .NET 框架包不兼容。不过谢谢!
  • 您仍然可以导入较旧的框架,但也许这是您要避免的。 “框架”:{“netcoreapp1.1”:{“依赖项”:{“Microsoft.NETCore.App”:{“版本”:“1.1.0”}},“进口”:[“dotnet5.6”,“便携-net45+win8" ] }, "net461": { }
  • @K7Buoy - 我之前已经尝试过它的变体,但我得到的只是指出 netcoreapp1.1 无法与 net461 比较的错误。

标签: asp.net-core asp.net-core-mvc


【解决方案1】:

使用.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];

【讨论】:

  • 这对我有用。只需要在过滤器中进行更改,因为我的公司使用属性“samaccountname”。
  • DirectoryEntry de = new DirectoryEntry("LDAP://bla.bli", ldapuser, ldappw); DirectorySearcher ds = new DirectorySearcher(de);
【解决方案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 文件,用作部署中的调试器。

希望这对其他人有帮助!

【讨论】:

  • 你把这段代码放在哪里了?在控制器内部?感谢您的提示!
  • @Diego - 是的,我将它放在主控制器中的 IActionResult 下,该 IActionResult 调用列出了 AD 数据的页面。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
相关资源
最近更新 更多