ActiveDirectory学习日记(一)

1:如何获取域中某个组的所有用户

private   void  Page_Load( object  sender, System.EventArgs e)
{
    StringCollection groupMembers 
= this.GetGroupMembers("pardesifashions","Debugger Users");
    
foreach (string strMember in groupMembers)
    
{
        Response.Write(
"<br><b>" + strMember + "</b>");
    }

}


public  StringCollection GetGroupMembers( string  strDomain,  string  strGroup)
{
    StringCollection groupMemebers 
= new StringCollection();
    
try
    
{
        DirectoryEntry ent 
= new DirectoryEntry("LDAP://DC=" + strDomain + ",DC=com");
        DirectorySearcher srch 
= new DirectorySearcher("(CN=" + strGroup + ")");
        SearchResultCollection coll 
= srch.FindAll();
        
foreach (SearchResult rs in coll)
        
{
            ResultPropertyCollection resultPropColl 
= rs.Properties;
            
foreach( Object memberColl in resultPropColl["member"])
            
{
                DirectoryEntry gpMemberEntry 
= new DirectoryEntry("LDAP://" + memberColl);
                System.DirectoryServices.PropertyCollection userProps 
= gpMemberEntry.Properties;
                
object obVal = userProps["sAMAccountName"].Value;
                
if (null != obVal)
                
{
                    groupMemebers.Add(obVal.ToString());
                }

            }

        }

    }

    
catch (Exception ex)
    
{
        Trace.Write(ex.Message);
    }

    
return groupMemebers;
}
        
 
2:获取某个AD中所有的域
private   void  Page_Load( object  sender, System.EventArgs e)
{
    StringCollection adDomains 
= this.GetDomainList();
    
foreach (string strDomain in adDomains)
    
{
        Response.Write(
"<br>" + strDomain + "</b>");
    }

}


private  StringCollection GetDomainList()
{
    StringCollection domainList 
= new StringCollection();
    
try
    
{
        DirectoryEntry en 
= new DirectoryEntry("LDAP://");
        
// Search for objectCategory type "Domain"
        DirectorySearcher srch = new DirectorySearcher("objectCategory=Domain");
        SearchResultCollection coll 
= srch.FindAll();
        
// Enumerate over each returned domain.
        foreach (SearchResult rs in coll)
        
{
            ResultPropertyCollection resultPropColl 
= rs.Properties;
            
foreachobject domainName in resultPropColl["name"])
            
{
                domainList.Add(domainName.ToString());
            }

        }

    }

    
catch (Exception ex)
    
{
        Trace.Write(ex.Message);
    }

    
return domainList;
}
            

3:获取当前登录用户的全名
private   void  Page_Load( object  sender, System.EventArgs e)
{
    WindowsPrincipal p 
= Thread.CurrentPrincipal as WindowsPrincipal;
    Response.Write(GetFullName(p.Identity.Name));
}


private   string  GetFullName( string  strLogin)
{
    
string str = "";
    
// Parse the string to check if domain name is present.
    int idx = strLogin.IndexOf('/');
    
if (idx == -1)
    
{
        idx 
= strLogin.IndexOf('@');
    }


    
string strDomain;
    
string strName;

    
if (idx != -1)
    
{
        strDomain 
= strLogin.Substring(0, idx);
        strName 
= strLogin.Substring(idx+1);
    }

    
else
    
{
        strDomain 
= Environment.MachineName;
        strName 
= strLogin;
    }


    DirectoryEntry obDirEntry 
= null;
    
try
    
{
        obDirEntry 
= new DirectoryEntry("WinNT://" + strDomain + "/" + strName);
        System.DirectoryServices.PropertyCollection  coll 
= obDirEntry.Properties;
        
object obVal = coll["FullName"].Value;
        str 
= obVal.ToString();
    }

    
catch (Exception ex)
    
{
        str 
= "";
        Trace.Write(ex.Message);
    }

    
return str;
}
        
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值