这是否有可能为设置一个“逐例查询”主体的条件--不像,而不是,比如?
类方法:
UserPrincipal qbeUser = new UserPrincipal(ctx);
qbeUser.Name= "Mario";这将返回所有名为"Mario“的用户。
这是否有可能创建一个条件,让所有未命名为"Mario“的用户?
这样的东西:
UserPrincipal qbeUser = new UserPrincipal(ctx);
qbeUser.Name != "Mario";所有的用户用的是"Mario“以外的其他名字。
发布于 2014-07-21 19:35:51
不,但是你可以得到所有的用户。然后使用linq过滤它们。
UserPrincipal qbeUser = new UserPrincipal(ctx);
PrincipalSearcher pSearch = new PrincipalSearcher(qbeUser);
PrincipalSearchResult<Principal> pResult = pSearch.FindAll();
var notMario = (from u in pResult
where u.Name != "Mario"
select u);那取决于你想做什么
foreach (Principal p in notMario) {
// Do Somthing
}发布于 2014-07-22 13:24:57
这可以通过扩展UserPrincipal类来实现:
[DirectoryRdnPrefix("CN")]
[DirectoryObjectClass("Person")]
public class UserPrincipalEXT : UserPrincipal
{
// Inplement the constructor using the base class constructor.
public UserPrincipalEXT(PrincipalContext context)
: base(context)
{ }
// Implement the constructor with initialization parameters.
public UserPrincipalEXT(PrincipalContext context,
string samAccountName,
string password,
bool enabled)
: base(context, samAccountName, password, enabled)
{ }
// Create the "employeeType" property with the "!" for NOT LIKE.
[DirectoryProperty("!employeeType")]
public string NotLikeEmployeeType
{
get
{
if (ExtensionGet("!employeeType").Length != 1)
return string.Empty;
return (string)ExtensionGet("!employeeType")[0];
}
set { ExtensionSet("!employeeType", value); }
}
// Implement the overloaded search method FindByIdentity.
public static new UserPrincipalEXT FindByIdentity(PrincipalContext context, string identityValue)
{
return (UserPrincipalEXT)FindByIdentityWithType(context, typeof(UserPrincipalEXT), identityValue);
}
// Implement the overloaded search method FindByIdentity.
public static new UserPrincipalEXT FindByIdentity(PrincipalContext context, IdentityType identityType, string identityValue)
{
return (UserPrincipalEXT)FindByIdentityWithType(context, typeof(UserPrincipalEXT), identityType, identityValue);
}
}重要的是要明白这一点:
// Create the "employeeType" property.
[DirectoryProperty("!employeeType")]
public string NotLikeEmployeeType
{
get
{
if (ExtensionGet("!employeeType").Length != 1)
return string.Empty;
return (string)ExtensionGet("!employeeType")[0];
}
set { ExtensionSet("!employeeType", value); }
}假设带有ExtensionGet和ExtensionSet的“NotLikeEmployeeType”在属性(在本例中为NotLikeEmployeeType)不是空时创建条件,则可以添加"!“在AD属性之前(本例中为employeeType)。
以下是我们可以使用扩展的方式:
UserPrincipalEXT qbeUser = new UserPrincipalEXT(ctx);
qbeUser.NotLikeEmployeeType = "exclude";现在返回的条件是:
!employeeType = exclude这正是我们想要的!不像!)
而且,扩展的好处是可以将AD属性添加到通常不公开的类中(如employeeType)。
https://stackoverflow.com/questions/24872715
复制相似问题