C# 管理WinNt 帳號

來源:互聯網
上載者:User
//WinNT使用者管理
using System;
using System.DirectoryServices; 
namespace Host.AdminManager.Inc
{
public class WindwosUser
{
//建立NT使用者
//傳入參數:Username要建立的使用者名稱,Userpassword使用者密碼,Path主資料夾路徑
public static bool CreateNTUser(string Username,string Userpassword,string Path)
{
DirectoryEntry obDirEntry = null;
try
{
obDirEntry = new DirectoryEntry("WinNT://" + Environment.MachineName);
DirectoryEntry obUser = obDirEntry.Children.Add(Username, "User"); //增加使用者名稱
obUser.Properties["FullName"].Add(Username); //使用者全稱
obUser.Invoke("SetPassword", Userpassword); //使用者密碼
obUser.Invoke("Put", "Description","Test User from .NET");//使用者詳細描述
//obUser.Invoke("Put","PasswordExpired",1); //使用者下次登入需更改密碼
obUser.Invoke("Put","UserFlags",66049); //密碼永不到期
obUser.Invoke("Put","HomeDirectory",Path); //主資料夾路徑
obUser.CommitChanges();//儲存使用者
DirectoryEntry grp = obDirEntry.Children.Find("Users", "group");//Users組
if(grp.Name!="")
{
grp.Invoke("Add",obUser.Path.ToString());//將使用者添加到某組
}
return true;
}
catch
{
return false;
}
}
//刪除NT使用者
//傳入參數:Username使用者名稱
public static bool DelNTUser(string Username)
{
try
{
DirectoryEntry obComputer = new DirectoryEntry("WinNt://" + Environment.MachineName);//獲得電腦執行個體
DirectoryEntry obUser = obComputer.Children.Find(Username,"User");//找得使用者
obComputer.Children.Remove(obUser);//刪除使用者
return true;
}
catch
{
return false;
}
}

//修改NT使用者密碼
//傳入參數:Username使用者名稱,Userpassword使用者新密碼
public static bool InitNTPwd(string Username,string Userpassword)
{
try
{
DirectoryEntry obComputer = new DirectoryEntry("WinNt://" + Environment.MachineName);
DirectoryEntry obUser = obComputer.Children.Find(Username,"User");
obUser.Invoke("SetPassword", Userpassword);
obUser.CommitChanges();
obUser.Close();
obComputer.Close();
return true;
}
catch
{
return false;
}
}
}
}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.