我有一个LDAP查询,我用它来在C#中执行搜索.它使用两个字符串变量(用户名和域),出于安全原因需要对其进行转义.
我应该如何摆脱弦乐?C#.NET中是否有可用的功能来执行此操作?
示例LDAP搜索条件:
(objectCategory=person)
(userprincipalname=username@domain*)
(samaccountname=username)
Run Code Online (Sandbox Code Playgroud)
C#中的LDAP查询字符串示例:
string search = "(&(&(objectCategory=person)(userprincipalname="
+ username
+ "@"
+ domain
+ "*)(samaccountname="
+ username
+ ")))";
Run Code Online (Sandbox Code Playgroud)
编辑:我已经有LDAP查询工作,并返回结果.我想要的只是逃避参数.
(这个问题类似于Get UPN or email for login user in a .NET web application,但不完全相同。)
在使用 Windows 身份验证的 .NET (C#) Web 应用程序中,我想找到登录用户的 UPN。
我知道如何通过查询 Active Directory 来做到这一点:从HttpContext.Current.User.Identity as WindowsIdentity;获取 'DOMAINNAME\userName' 在 下查找 DOMAINNAMEldap://RootDSE并找到它dnsRoot;查询(&(objectCategory=person)(objectClass=user)(sAMAccountName=...userName...))下ldap://...dnsRoot...;获取此条目的userPrincipalName属性。(更多细节在回答/sf/answers/35956791/ 中)。
我的问题:是否可以在不调用 Active Directory 的情况下找到 UPN ?鉴于 Microsoft 专注于在任何地方使用 UPN,UPN 是不是已经存储在用户向 Web 服务器进行身份验证时创建的令牌中的某处?
(支持观察:如果我whoami /upn在 Windows 7 上运行,那么 Wireshark 不会显示任何 Active Directory 连接。)
(如果有什么不同:请注意,我们的 Web 应用程序不使用 …
我按照这个问题的解决方案如何从活动目录中获取用户列表?并且能够从AD获得用户列表.我遇到的问题是加载所有记录需要35秒.
必须有一种更有效的方法来一次查询所有数据,而不是等待35秒才能返回700多条记录.我写了一个方法来返回用户列表.我已经添加了一些额外的代码来尝试过滤掉任何不属于人类的用户.
public List<ActiveUser> GetActiveDirectoryUsers()
{
List<ActiveUser> response = new List<ActiveUser>();
using (var context = new PrincipalContext(ContextType.Domain, "mydomain"))
{
using (var searcher = new PrincipalSearcher(new UserPrincipal(context)))
{
foreach (var result in searcher.FindAll())
{
DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry;
if (de.NativeGuid != null && !Convert.ToBoolean((int)de.Properties["userAccountControl"].Value & 0x0002) &&
de.Properties["department"].Value != null && de.Properties["sn"].Value != null) response.Add(new ActiveUser(de));
}
}
}
return response.OrderBy(x => x.DisplayName).ToList();
}
Run Code Online (Sandbox Code Playgroud)
ActiveUser的构造函数只需要输入entry.property ["whataver"]并将其分配给该类的属性.开销似乎就行了
DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry;
Run Code Online (Sandbox Code Playgroud)
我可以将用户列表缓存到一个文件中,但是对于一个列表来说仍然需要超过30秒的加载时间.必须有一种更快的方法来做到这一点.
有人可以解释ldap字符串部分的构成.
我拥有的是:
string strSQL = "SELECT mail FROM 'LDAP://DC=amrs,DC=win,DC=ml,dc=COM' WHERE samaccountname = '" + UserName.Replace(@"AMRS\", "") + "'";
Run Code Online (Sandbox Code Playgroud)
这会收到特定用户名的电子邮件.现在我需要从ldap查询中获取其他信息并且无法获得正确的设置,而且我也不知道ldap设置中的值是什么."LDAP:// DC = AMRS,DC =取胜,DC =毫升,DC = COM"
有人能解释一下吗?