ADSI管理Windows2003系统帐号

     最近在开发一个虚拟主机管理的软件,需要用到用户权限管理这块,上网找了找资料,发现ADSI这块还不错,好像还有另外的一个新的东西可以实现操作用户帐号的功能,那个以后在研究下吧,操作Window2003用户的代码如下:

using System;
using System.DirectoryServices;
using System.Collections;

namespace CNVP.FM.IISControlService
{
    
/// <summary>
    
/// 实现在开通IIS站点时候自动对系统帐号进行添加删除操作
    
/// </summary>

    public class UserManager
    
{
        
public UserManager()
        
{
            
        }

        
/// <summary>
        
/// 创建IIS登录帐号
        
/// </summary>
        
/// <param name="Accounts">登录帐号</param>
        
/// <returns></returns>
      
        
public static bool CreateUser(string Accounts)
        
{
            
try
            
{
                DirectoryEntry AD 
= new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer");
                DirectoryEntry NewUser 
= AD.Children.Add(Accounts, "user");//增加用户名
                NewUser.Invoke("SetPassword"new object[] "LanTian123456" });//用户密码
                NewUser.Invoke("Put"new object[] "FullName", Accounts });//用户全称  
                NewUser.Invoke("Put"new object[] "Description""太平洋主机管理帐号" });//用户详细描述
                NewUser.Invoke("Put""PasswordExpired"0);//用户下次登录需更改密码
                
//NewUser.Invoke("Put", "UserFlags", 66049);//密码永不过期
                
//NewUser.Invoke("Put", "HomeDirectory", Path); //主文件夹路径

                
//NewUser.Properties["UserFlags"].Add(0x0002);//禁用登录帐号
                
//NewUser.Properties["UserFlags"].Add(0x0040);//用户不能更改密码
                ArrayList arl = new ArrayList();
                arl.Add(
0x0002);
                arl.Add(
0x0040);
                arl.Add(
0x10000);
                NewUser.Properties[
"UserFlags"].Value = arl.ToArray();

                
//NewUser.Properties["UserFlags"].Add(0x10000);//用户密码永不到期

                NewUser.CommitChanges();
                DirectoryEntry ObjUser
=AD.Children.Find("Guests""group");
                
if (ObjUser.Name != null
                
{
                    ObjUser.Invoke(
"Add"new object[] { NewUser.Path.ToString() }); 
                }

                ObjUser.Close();
                AD.Close();
                
return true;
            }

            
catch
            
{
                
return false;
            }

        }

        
/// <summary>
        
/// 修改IIS登录帐号密码
        
/// </summary>
        
/// <param name="Accounts">登录帐号</param>
        
/// <param name="Password">登录密码</param>
        
/// <returns></returns>

        public static bool EditUser(string Accounts, string Password)
        
{
            
try
            
{
                DirectoryEntry AD 
= new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer");
                DirectoryEntry ObjUser 
= AD.Children.Find(Accounts, "user");
                ObjUser.Invoke(
"SetPassword", Password);
                ObjUser.CommitChanges();
                ObjUser.Close();
                AD.Close();
                
return true;
            }

            
catch
            
{
                
return false;
            }

        }

        
/// <summary>
        
/// 删除IIS登录帐号
        
/// </summary>
        
/// <param name="Accounts">登录帐号</param>
        
/// <returns></returns>

        public static bool DelUser(string Accounts)
        
{
            
try
            
{
                DirectoryEntry AD 
= new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer");
                DirectoryEntry ObjUser 
= AD.Children.Find(Accounts, "user");//找得用户
                
//AD.Children.Remove(ObjUser);//删除用户
                ObjUser.GetType();
                ObjUser.Close();
                AD.Close();
                
return true;
            }

            
catch
            
{
                
return false;
            }

        }

    }

}
posted on 2007-05-20 12:01  北极熊,我来了!  阅读(1351)  评论(1编辑  收藏  举报