在c#中使用LDAP对来自外部域的用户的组详细信息 [英] Group Details of user from external domain using LDAP in c#

查看:69
本文介绍了在c#中使用LDAP对来自外部域的用户的组详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我有一个要求,我需要获取所有组名称及其描述(哪个用户是成员,还有那些没有用户的组)。  与外部域的
连接必须通过LDAP与端口389和用户的凭证进行连接。


目前我能够使用以下代码验证用户:

 public string UserValidation(string username,string domain,string password,string url)
{        
var credentials = new NetworkCredential(用户名,密码,域名);       
var serverId = new LdapDirectoryIdentifier(url);
LdapConnection connection = new LdapConnection(serverId,credentials);       
string result =" true";           
试试           
{               
connection.Bind();           
}           
catch(例外e)           
{               
result = e.ToString();           
}           
connection.Dispose();           
返回结果;       
}

我很难得到小组的详细信息。

解决方案

Hello Pawan,


有一种方法可以循环所有组并列出其中的所有成员。您可以参考它。

 

使用System;
使用System.DirectoryServices.AccountManagement;
使用System.DirectoryServices.Protocols ;
使用System.Net;

类程序
{
//设置域上下文
static PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

static void Main(string [] args)
{
TestGroups();
}

public static void GetMembers(string groupName)
{
//查找有问题的组
GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx,团队名字);

//如果找到....
if(group!= null)
{
//迭代成员
foreach(Principal p in group.GetMembers())
{
Console.WriteLine(" {0}:{1}:{2}",p.Name,p.DisplayName,p.UserPrincipalName);

//为这些成员做任何你需要做的事情
UserPrincipal theUser = p as UserPrincipal;

if(theUser!= null)
{
if(theUser.IsAccountLockedOut())
{

}
else
{

}
}
}
}
}

public static void TestGroups()
{
//定义"按示例查询" principal - 在这里,我们搜索GroupPrincipal
GroupPrincipal qbeGroup = new GroupPrincipal(ctx);

//创建你的主要搜索者传递QBE校长
PrincipalSearcher srch = new PrincipalSearcher(qbeGroup);

//查找所有匹配项
foreach(在srch.FindAll()中找到var)
{
//在此处执行任何操作 - " found"是"主要"类型的 - 它可以是用户,组,计算机......
Console.WriteLine(" \t _________________" + found.Name);

GetMembers(found.Name);
}
}

祝你好运,


Neil Hu


Hi All,

I've a requirement where in I need to get all group name and its description (of which user is member and also those groups which don't have user). The connection to the external domain has to be trough LDAP with port 389 and with user's credential.

For now I am able to validate the user by using below code:

public string UserValidation(string username, string domain, string password, string url)
{        
var credentials = new NetworkCredential(username, password, domain);        
var serverId = new LdapDirectoryIdentifier(url);
LdapConnection connection = new LdapConnection(serverId, credentials);        
string result = "true";            
try            
{                
connection.Bind();            
}            
catch (Exception e)            
{                
result = e.ToString();            
}            
connection.Dispose();            
return result;        
}

I am struggling to get the group details.

解决方案

Hello Pawan,

There is a way that you could loop all groups and list all members of which. You could take reference with it.

using System;
using System.DirectoryServices.AccountManagement;
using System.DirectoryServices.Protocols;
using System.Net;

class Program { // set up domain context static PrincipalContext ctx = new PrincipalContext(ContextType.Domain); static void Main(string[] args) { TestGroups(); } public static void GetMembers(string groupName) { // find the group in question GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, groupName); // if found.... if (group != null) { // iterate over members foreach (Principal p in group.GetMembers()) { Console.WriteLine("{0}: {1}:{2}", p.Name, p.DisplayName, p.UserPrincipalName); // do whatever you need to do to those members UserPrincipal theUser = p as UserPrincipal; if (theUser != null) { if (theUser.IsAccountLockedOut()) { } else { } } } } } public static void TestGroups() { // define a "query-by-example" principal - here, we search for a GroupPrincipal GroupPrincipal qbeGroup = new GroupPrincipal(ctx); // create your principal searcher passing in the QBE principal PrincipalSearcher srch = new PrincipalSearcher(qbeGroup); // find all matches foreach (var found in srch.FindAll()) { // do whatever here - "found" is of type "Principal" - it could be user, group, computer..... Console.WriteLine("\t_________________" + found.Name); GetMembers(found.Name); } }

Best regards,

Neil Hu


这篇关于在c#中使用LDAP对来自外部域的用户的组详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆