Active directory Can';t获取要查询的Novell.Directory.Ldap.NETStandard库
我需要让用户在Active Directory中查询.Net Core中的名称。 因此,我正在构建一个Active Directory搜索Web API服务 我能够连接bind语句。 但是我无法通过我的查询返回任何结果,尽管没有错误 另一个程序员给我发了一些他在其他应用程序中使用的代码。但它使用的是.NETCore中不可用的DirectoryEntry对象 因此,我尝试使用Novell.Directory.Ldap.NetStandard库 以下是其他开发人员发给我的代码:Active directory Can';t获取要查询的Novell.Directory.Ldap.NETStandard库,active-directory,asp.net-core-mvc,Active Directory,Asp.net Core Mvc,我需要让用户在Active Directory中查询.Net Core中的名称。 因此,我正在构建一个Active Directory搜索Web API服务 我能够连接bind语句。 但是我无法通过我的查询返回任何结果,尽管没有错误 另一个程序员给我发了一些他在其他应用程序中使用的代码。但它使用的是.NETCore中不可用的DirectoryEntry对象 因此,我尝试使用Novell.Directory.Ldap.NetStandard库 以下是其他开发人员发给我的代码: public sta
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;
}
公共静态列表GetADUsers(字符串别名)
{
列表用户=新列表();
if(alias==null | | alias.Trim().Equals(“”)
{
返回用户;
}
尝试
{
//广告路径LDAP://ourOrg.gov/CN=Users,DC=ourOrg,DC=gov
DirectoryEntry de2=新目录项(ConfigurationManager.AppSettings[“AD_路径”]、ConfigurationManager.AppSettings[“AD_用户”]、ConfigurationManager.AppSettings[“AD_密码”]);
de2.Path=ConfigurationManager.AppSettings[“AD_Path”];
de2.AuthenticationType=AuthenticationTypes.Secure;
DirectorySearcher deSearch=新的DirectorySearch();
deSearch.SearchRoot=de2;
deSearch.Filter=“(samaccountname=*”+别名+“*)”;
Debug(String.Format(“Active Directory搜索筛选器{0}”,deSearch.Filter));
SearchResultCollection results=deSearch.FindAll();
字符串raw=“”;
Debug(String.Format(“Active Directory搜索结果计数{0}”,results.Count));
如果(results.Count>0)
{
foreach(结果中的SearchResult项)
{
UserProfileModel UserProfileModel=新的UserProfileModel();
userProfileModel.Name=GetADProperty(“名称”,项);
userProfileModel.email=GetADProperty(“邮件”,项目);
userProfileModel.identity=GetADProperty(“userPrincipalName”,项目);
userProfileModel.first_name=GetADProperty(“givenName”,项目);
userProfileModel.last_name=GetADProperty(“序号”,项目);
添加(userProfileModel);
raw=String.Format(“{0}/n{1}”,raw,userProfileModel.ToString());
}
Debug(String.Format(“Active Directory搜索结果字符串:{0}”,原始));
}
}
捕获(例外e)
{
LOGGER.Error(“无法查询Active Directory”,e);
}
返回用户;
}
我需要将其转换为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;
}
}
[HttpGet]
公共异步任务GetByName(字符串别名)
{
int ldapPort=LdapConnection.DEFAULT\u端口;
字符串ldapHost=“ourOrg.gov”;
字符串loginDn=@“ourOrg\myName”;
字符串password=“myPass”;
列表用户=新列表();
if(alias==null | | alias.Trim().Equals(“”)
{
返回用户;
}
尝试
{
使用(var con=new LdapConnection())
{
con.Connect(LDAPPOST、ldapPort);
con.Bind(登录名、密码);
LdapSearchResults results=con.Search(
“cn=用户,dc=我们的组织,dc=政府”,
LdapConnection.SCOPE\u ONE,
“samaccountname=*”,
无效的
假);
//无结果:(
}
返回用户;
}
捕获(例外情况除外)
{
掷骰子;
}
}
我没有错。
但结果是零
我最初有这个部分:
“samaccountname=*”
比如:
“samaccountname={alias}”
但我只是想在这一点上得到结果。我得到了这个结果:
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;
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Threading.Tasks;
使用Microsoft.AspNetCore.Mvc;
使用Microsoft.Extensions.Options;
使用Hrsa.Core.Web.App.Models.ViewModels;
使用Novell.Directory.Ldap;
//有关为空项目启用Web API的更多信息,请访问http://go.microsoft.com/fwlink/?LinkID=397860
命名空间Hrsa.Core.Web.App.Controllers.Api
{
[路由(“api/[控制器]”)]
公共类ActiveDirectoryController:控制器
{
私有只读应用设置\u应用设置;
公共ActiveDirectoryController(IOPS应用设置)
{
_appSettings=appSettings.Value;
}
[HttpGet]
公共异步任务GetByName(字符串别名)
{
int ldapPort=LdapConnection.DEFAULT\u端口;
字符串ldapHost=\u appSettings.HrsaLdapHost;//ourOrgName.gov
字符串loginDn=\u appSettings.AdUser;
字符串密码=_appSettings.AdPassword;
字符串searchBase=\u appSettings.HrsaAdSearchBase;
字符串searchFilter=$“(samaccountname=*{alias}*)”;
字符串[]属性=新字符串[]{“cn”、“userPrincipalName”、“st”、“givenname”、“samaccountname”,
“说明”、“电话号码”、“部门”、“显示名称”、“姓名”、“邮件”、“赠品名称”、“序号”};
列表用户=新列表();
if(alias==null | | alias.Trim().Equals(“”)
{
返回用户;
}
尝试
{
使用(var con=new LdapConnection())
{
con.Connect(LDAPPOST、ldapPort);
con.Bind(登录名、密码);
LdapSearchQueue=con.Search(
搜索基地,
LdapConnection.SCOPE_SUB,
搜索过滤器,
属性,
假,,
(LdapSearchQueue)nu