gpt4 book ai didi

c# - 如何找到未使用 PrincipalSearcher 设置属性的 UserPrincipal?

转载 作者:太空狗 更新时间:2023-10-29 18:25:16 25 4
gpt4 key购买 nike

我正在尝试在未设置属性的 AD LDS (ADAM) 实例中搜索用户,例如,“公司”属性未设置为 ADAM 存储(或 AD 中的值)事)。

当我将 PrincipalSearcher 和自定义 UserPrincipal 与自定义 AdvancedSearchFilters 对象一起使用时,出现错误:

An unhandled exception of type 'System.ArgumentException' occurred in System.DirectoryServices.dll

Additional information: The (&(objectClass=user)(!(company=))) search filter is invalid.

这是我的示例代码:

using System;
using System.DirectoryServices.AccountManagement;
using System.Security.Permissions;
using System.Linq;

namespace AdamDump
{
class Program
{
static void Main(string[] args)
{
PrincipalContext context = new PrincipalContext(ContextType.ApplicationDirectory, "MyAdamInstance:50000", "OU=Adam Users,dc=apps01,dc=mydomain", "queryaccount", "password");

// initialize a Query By Example
using (MyUserPrincipal myUserPrincipal = new MyUserPrincipal(context))
{
myUserPrincipal.MyAdvancedFilters.WhereCompanyNotSet();

PrincipalSearchResult<Principal> principals = null;

// do the search...
using (PrincipalSearcher principalSearcher = new PrincipalSearcher(myUserPrincipal))
{
principals = principalSearcher.FindAll();
}

var myUsers = principals.Select(principal => principal as MyUserPrincipal).ToList();

foreach (var user in myUsers)
Console.WriteLine("Name: {0}, Account{1}", user.DisplayName, user.SamAccountName);

Console.WriteLine("Total found: {0}", myUsers.Count);
}
}
}


[DirectoryObjectClass("user")]
[DirectoryRdnPrefix("CN")]
[EnvironmentPermissionAttribute(SecurityAction.LinkDemand, Unrestricted = true)]
public class MyUserPrincipal : UserPrincipal
{
private MyAdvancedFilters _myAdvancedFilters;

/// <summary>
/// Initializes a new instance of the <see cref="MyUserPrincipal"/> class.
/// </summary>
/// <param name="context">A <see cref="PrincipalContext"/> to associate this instance with.</param>
[EnvironmentPermissionAttribute(SecurityAction.LinkDemand, Unrestricted=true)]
public MyUserPrincipal(PrincipalContext context)
: base(context) { }

public MyAdvancedFilters MyAdvancedFilters
{
get
{
return this.AdvancedSearchFilter as MyAdvancedFilters;
}
}

public override AdvancedFilters AdvancedSearchFilter
{
get
{
if (_myAdvancedFilters == null)
{
_myAdvancedFilters = new MyAdvancedFilters(this);
}

return _myAdvancedFilters;
}
}
}

public class MyAdvancedFilters : AdvancedFilters
{
/// <summary>
/// Initializes a new instance of the <see cref="MyAdvancedFilters"/> class.
/// </summary>
/// <param name="principal">The source <see cref="Principal"/></param>
public MyAdvancedFilters(Principal principal) : base(principal) { }

public void WhereCompanyNotSet()
{
this.AdvancedFilterSet("company", "", typeof(string), MatchType.NotEquals);
}
}
}

最佳答案

将我的 AdvanceFilters 类修改为以下内容可获得我需要的结果。

public class MyAdvancedFilters : AdvancedFilters
{
/// <summary>
/// Initializes a new instance of the <see cref="MyAdvancedFilters"/> class.
/// </summary>
/// <param name="principal">The source <see cref="Principal"/></param>
public MyAdvancedFilters(Principal principal) : base(principal) { }

public void WhereCompanyNotSet()
{
this.AdvancedFilterSet("company", "*", typeof(string), MatchType.NotEquals);
}
}

它是AdvancedFilterSet中取值部分的"*"

感谢 Sean 带领正确的路径得出适合 AccountManagement 对象的答案。

关于c# - 如何找到未使用 PrincipalSearcher 设置属性的 UserPrincipal?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13769168/

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