c# - 在机器上下文中通过不区分大小写的 SAM 查找特定用户的高效方法
问题描述
这段代码在域上下文中就像一个魅力:
var user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, username);
它也适用于机器上下文,但速度极慢(在大约 20 个用户中找到一个用户需要 13 秒)。
但首先,这种方法不区分大小写,这对我来说是必须的。
我在某处找到了机器上下文的替代代码:
var context = new PrincipalContext(ContextType.Machine);
var user = new UserPrincipal(context)
{
SamAccountName = username
};
using (var searcher = new PrincipalSearcher(user))
{
user = searcher.FindOne() as UserPrincipal;
}
不幸的是,此代码区分大小写。
谁能建议一种在机器上下文中既快速又不区分大小写的方法?
解决方案
如果有很多本地用户,可能不是最好的解决方案,但它有效:
var username = "wHaTeVer";
UserPrincipal user = null;
using (var context = new PrincipalContext(ContextType.Machine))
{
user = new UserPrincipal(context);
using (var searcher = new PrincipalSearcher(user))
{
user = searcher.FindAll().FirstOrDefault(x => x.SamAccountName.Equals(username, StringComparison.InvariantCultureIgnoreCase)) as UserPrincipal;
}
}
推荐阅读
- javascript - 我不明白为什么我会收到“未捕获的 TypeError:无法在 Function.entries 将 undefined 或 null 转换为对象(
)" - postgresql - AWS postgres 每天将数据从一个数据库复制到另一个数据库?
- flutter - 小部件的颤动映射如何
- flutter - 为什么我的 statefull 小部件在颤振中的 setState() 后无法正确重建?
- node.js - 找不到模块nodeJS
- php - 防止直接访问 php 文件 jquery load()
- javascript - 反向页面计算算法
- c - 如何将一组数字插入到链表中?
- mysql - 具有相同 ID 的 MySQL SUM 行
- python - Python:遍历循环和列表