gpt4 book ai didi

active-directory - 无法获取 Novell.Directory.Ldap.NETStandard 库进行查询

转载 作者:行者123 更新时间:2023-12-03 23:56:51 34 4
gpt4 key购买 nike

我需要让用户在 Active Directory 中查询 .Net Core 中的名称。
所以我正在构建一个 Active Directory 搜索 Web API 服务。

我能够连接到 bind 语句。
但是,尽管没有错误,但我无法通过查询获得任何结果。

另一位程序员给我发了一些他在其他应用程序中使用的代码。但它使用了 .Net Core 中不可用的 DirectoryEntry 对象。

所以我试图使用 Novell.Directory.Ldap.NetStandard 库。

这是其他开发人员发送给我的代码:

public static List<UserProfileModel> GetADUsers(string alias)
{
List<UserProfileModel> users = new List<UserProfileModel>();

if (alias == null || alias.Trim().Equals(""))
{
return users;
}

try
{
// Ad path LDAP://ourOrg.gov/CN=Users,DC=ourOrg,DC=gov
DirectoryEntry de2 = new DirectoryEntry(ConfigurationManager.AppSettings["AD_Path"], ConfigurationManager.AppSettings["AD_User"], ConfigurationManager.AppSettings["AD_Password"]);
de2.Path = ConfigurationManager.AppSettings["AD_Path"];

de2.AuthenticationType = AuthenticationTypes.Secure;

DirectorySearcher deSearch = new DirectorySearcher();

deSearch.SearchRoot = de2;
deSearch.Filter = "(samaccountname=*" + alias + "*)";

LOGGER.Debug(String.Format("Active Directory Search Filter {0}", deSearch.Filter));

SearchResultCollection results = deSearch.FindAll();
String raw = "";

LOGGER.Debug(String.Format("Active Directory Search Result Counts {0}", results.Count));

if (results.Count > 0)
{
foreach (SearchResult item in results)
{
UserProfileModel userProfileModel = new UserProfileModel();

userProfileModel.Name = GetADProperty("name", item);
userProfileModel.email = GetADProperty("mail", item);
userProfileModel.identity = GetADProperty("userPrincipalName", item);
userProfileModel.first_name = GetADProperty("givenName", item);
userProfileModel.last_name = GetADProperty("sn", item);
users.Add(userProfileModel);
raw = String.Format("{0}/n{1}", raw, userProfileModel.ToString());
}
LOGGER.Debug(String.Format("Active Directory Search Resuts ToString: {0}", raw));
}
}
catch (Exception e)
{
LOGGER.Error("Unable to Query Active Directory", e);
}

return users;
}

我需要将其转换为 Novell 的 LDAP 库。

这是我的尝试:
    [HttpGet]
public async Task<List<UserProfileModel>> GetByName(string alias)
{

int ldapPort = LdapConnection.DEFAULT_PORT;
string ldapHost = "ourOrg.gov";
string loginDn = @"ourOrg\myName";
string password = "myPass";

List<UserProfileModel> users = new List<UserProfileModel>();

if (alias == null || alias.Trim().Equals(""))
{
return users;
}

try
{
using (var con = new LdapConnection())
{
con.Connect(ldapHost, ldapPort);
con.Bind(loginDn, password);

LdapSearchResults results = con.Search(
"cn=users,dc=ourOrg,dc=gov",
LdapConnection.SCOPE_ONE,
"samaccountname=*",
null,
false);

// NO RESULTS:(
}

return users;
}
catch(Exception ex)
{
throw ex;
}

}

我没有收到错误。
但是有 0 个结果。

我最初有这部分:

"samaccountname=*",

喜欢:

"samaccountname={别名}",

但我只是想在这一点上取回结果。

最佳答案

我得到了这个工作:

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Hrsa.Core.Web.App.Models.ViewModels;
using Novell.Directory.Ldap;

// For more information on enabling Web API for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860

namespace Hrsa.Core.Web.App.Controllers.Api
{
[Route("api/[controller]")]
public class ActiveDirectoryController : Controller
{
private readonly AppSettings _appSettings;

public ActiveDirectoryController(IOptions<AppSettings> appSettings)
{
_appSettings = appSettings.Value;
}

[HttpGet]
public async Task<List<UserProfileModel>> GetByName(string alias)
{
int ldapPort = LdapConnection.DEFAULT_PORT;
string ldapHost = _appSettings.HrsaLdapHost; // ourOrgName.gov
string loginDn = _appSettings.AdUser;
string password = _appSettings.AdPassword;

string searchBase = _appSettings.HrsaAdSearchBase;
string searchFilter = $"(samaccountname=*{alias}*)";
string[] attributes = new string[] { "cn", "userPrincipalName", "st", "givenname", "samaccountname",
"description", "telephonenumber", "department", "displayname", "name", "mail", "givenName", "sn" };

List<UserProfileModel> users = new List<UserProfileModel>();

if (alias == null || alias.Trim().Equals(""))
{
return users;
}

try
{
using (var con = new LdapConnection())
{
con.Connect(ldapHost, ldapPort);
con.Bind(loginDn, password);

LdapSearchQueue queue = con.Search(
searchBase,
LdapConnection.SCOPE_SUB,
searchFilter,
attributes,
false,
(LdapSearchQueue)null,
(LdapSearchConstraints)null);

LdapMessage message;

while ((message = queue.getResponse()) != null)
{
if (message is LdapSearchResult)
{
LdapEntry entry = ((LdapSearchResult)message).Entry;

LdapAttributeSet attributeSet = entry.getAttributeSet();

users.Add(new UserProfileModel
{

Cn = attributeSet.getAttribute("cn")?.StringValue,
UserPrincipalName = attributeSet.getAttribute("userPrincipalName")?.StringValue,
St = attributeSet.getAttribute("st")?.StringValue,
Givenname = attributeSet.getAttribute("givenname")?.StringValue,
Samaccountname = attributeSet.getAttribute("samaccountname")?.StringValue,
Description = attributeSet.getAttribute("description")?.StringValue,
Telephonenumber = attributeSet.getAttribute("telephonenumber")?.StringValue,
Department = attributeSet.getAttribute("department")?.StringValue,
Displayname = attributeSet.getAttribute("displayname")?.StringValue,
Name = attributeSet.getAttribute("name")?.StringValue,
Mail = attributeSet.getAttribute("mail")?.StringValue,
GivenName = attributeSet.getAttribute("givenName")?.StringValue,
Sn = attributeSet.getAttribute("sn")?.StringValue
});
}
}
}

return users;
}
catch(Exception ex)
{
throw ex;
}

}
}
}

关于active-directory - 无法获取 Novell.Directory.Ldap.NETStandard 库进行查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44333776/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com