[C#] How to Lock and Unlock User in Domain Windows.

Xin chào các bạn, bài viết hôm nay mình tiếp tục chia sẻ các bạn source code cách khóa và mở khóa (lock and unlock) tài khoản người dùng user trong domain Windows bằng C#.

[C#] How to Lock and Unlock User in Domain Windows.

Để 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ó.
Ở hàm dưới này, các bạn cần truyền vào 4 tham số để hoạt động:
  1. username: là tài khoản user cần thao tác khóa hoặc mở khóa
  2. domain: tên domain của bạn ex: hung.pro.vn
  3. userAdmin: nhập tài khoản Admin Domain
  4. passAdmin: nhập mật khẩu Admin Domain

Source code C#:

using System;
using System.DirectoryServices;

class Program
{
    static void Main()
    {
        string username = "userToUnlockOrLock"; // Specify the username of the account you want to unlock or lock
        string domain = "yourdomain.com"; // Specify your domain name
        string userAdmin = "adminUsername"; // Specify the username of your administrator account
        string passAdmin = "adminPassword"; // 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 các bạn thành công với thủ thuật trên.

PASSWORD UNZIP: HUNG.PRO.VN
Chúc Mọi Người Thành Công Với Thủ Thuật Trên.
Nếu mọi người có vướng mắc gì mình chia sẽ trên trang có thể gửi liên hê cho mình tại đây nhé.
Cảm ơn mọi người đã quan tâm.

2 comments:

Ju Hazutora said...

mọi người test thử nhé, hehe m test bài viết của công tác viên :D

Hung Program VN said...

cảm nhân thế nào?

All Right Reserved © 2015 By Hung Pro VN