Jason

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

1、获得用户的所有属性

string strPath = "LDAP://CN=王林,OU=HXZ,OU=gzzf,DC=iw,DC=local";
            DirectoryEntry myEntry 
= new DirectoryEntry(strPath);
            DirectorySearcher mySearcher 
= new DirectorySearcher(myEntry);
            SearchResult myResult 
= mySearcher.FindOne();
            
if (myResult != null)
            
{
                ResultPropertyCollection myResultProColl;
                myResultProColl 
= myResult.Properties;
                
foreach (string myKey in myResultProColl.PropertyNames)
                
{
                    
foreach (Object myCollection in myResultProColl[myKey])
                    
{
                        
this.textBox1.Text += "PropertyName:" + myKey + " = " + myCollection + "\r\n";
                    }

                }

            }
2、将用户添加到某个组
string ADpath = "LDAP://DC=iw,DC=local";
string ADUser = "iw\\admin";
string ADPassword = "password";
string groupName = "workgroup"//组名
string userPath = "CN=王林,OU=HXZ,OU=gzzf,DC=iw,DC=local";
DirectoryEntry entry 
= new DirectoryEntry(ADpath,ADUser,ADPassword,AuthenticationTypes.Secure);
DirectorySearcher deSearch 
= new DirectorySearcher(entry);
deSearch.Filter    
= "(&(objectClass=group)(cn=" +    groupName +"))";
deSearch.SearchScope 
= SearchScope.Subtree;
try
 
{
    SearchResult result    
= deSearch.FindOne();
    DirectoryEntry deGroup 
= new DirectoryEntry(result.Path);
    deGroup.Properties[
"member"].Add(userPath);
    
//或者用下面方法获得用户。
    
//deGroup.Properties["member"].Add(deUser.Properties["distinguishedName"].Value);
    deGroup.CommitChanges();
 }

catch (Exception ex)
{
    
throw ex;    
}
上面的代码还不完善,最好先判定一下该组是否存在。
posted on 2006-01-19 13:41  Jsang  阅读(594)  评论(0)    收藏  举报