[CSHAR] How to Lock and Unlock User in Domain Windows.
[C#] Chia sẻ code lock và unlock user trong domain Window
Để khóa và mở khóa được tài khoản user trong domain.Các bạn cần phải có tài khoản của Admin Domain, thì các bạn mới có thể khóa và mở khóa cho user được.
Ở bài này, mình chia sẻ function, các bạn có thể copy và sử dụng nó.
Source code C#:
Ở hàm dưới này, các bạn cần truyền vào 4 tham số để hoạt động:username: là tài khoản user cần thao tác khóa hoặc mở khóadomain: tên domain của bạn ex:hung.pro.vnAdmin_User: nhập tài khoảnUser_domainAdmin_Password: nhập mật khẩuPassword_domain
Full Source Code
using System;
using System.DirectoryServices;
class Program
{
static void Main()
{
string username = "User_LockorUnLock"; // Specify the username of the account you want to unlock or lock
string domain = "hung.pro.vn"; // Specify your domain name
string Admin_User = "User_domain"; // Specify the username of your administrator account
string Admin_Password = "Password_domain"; // Specify the password of your administrator account
UnlockOrLockUserAccount(username, domain, userAdmin, passAdmin);
}
static void UnlockOrLockUserAccount(string username, string domain, string userAdmin, string passAdmin)
{
try
{
DirectoryEntry entry = new DirectoryEntry("LDAP://" + domain, userAdmin, passAdmin);
DirectorySearcher searcher = new DirectorySearcher(entry);
searcher.Filter = "(&(objectClass=user)(samAccountName=" + username + "))";
SearchResult result = searcher.FindOne();
if (result != null)
{
DirectoryEntry userEntry = result.GetDirectoryEntry();
int userFlags = (int)userEntry.Properties["userAccountControl"].Value;
bool isAccountLocked = (userFlags & 0x2) == 0x2; // Check if the "account is locked" bit is set
// Toggle the lock status
if (isAccountLocked)
{
// Account is currently locked, unlock it
int newFlags = userFlags & ~0x2; // Clear the "account is locked" bit
userEntry.Properties["userAccountControl"].Value = newFlags;
userEntry.CommitChanges();
Console.WriteLine("Account unlocked successfully.");
}
else
{
// Account is currently unlocked, lock it
int newFlags = userFlags | 0x2; // Set the "account is locked" bit
userEntry.Properties["userAccountControl"].Value = newFlags;
userEntry.CommitChanges();
Console.WriteLine("Account locked successfully.");
}
}
else
{
Console.WriteLine("User not found.");
}
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}
}
Chúc mọi người thành công với thủ thuật trên, và có thể áp dụng để mỡ khóa một chương trình dạng domain cho người dùng trên internet cũng ok nhé mọi người.
![[CSHAR] How to Lock and Unlock User in Domain Windows. [CSHAR] How to Lock and Unlock User in Domain Windows.](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjThQrcReB_s4KsXwn6erVksm68mtvossbIGUDiGezM_8hoVRYuZ_wUtQzXWZuB0TxPw-ulxCXgmq1BAYwK9gTr34QJ6DTVTN19FuTnAzFptXPFA3QQTIC3lSrGY88BZ68qVB5NgDkdhM1fXBRURe4V-gBzamHhCmo48SeupcLZy9CaHW8HFnfgiVW3h5A/s16000/%5BCSHAR%5D%20How%20to%20Lock%20and%20Unlock%20User%20in%20Domain%20Windows..png)