NEWS
                        
                    - [DEVEXPRESS] Hỗ trợ tìm kiếm highlight không dấu và không khoảng cách trên Gridview Filter
- [C#] Chia sẻ source code phần mềm Image Downloader tải hàng loạt hình ảnh từ danh sách link url
- [C#] Chụp hình và quay video từ camera trên winform
- [C#] Chia sẽ full source code tách file Pdf thành nhiều file với các tùy chọn
- Giới thiệu về Stock Tracker Widget - Công cụ theo dõi cổ phiếu và cảnh báo giá tăng giảm bằng C# và WPF
- [VB.NET] Chia sẻ công cụ nhập số tiền tự động định dạng tiền tệ Việt Nam
- [VB.NET] Hướng dẫn fill dữ liệu từ winform vào Microsoft word
- [VB.NET] Hướng dẫn chọn nhiều dòng trên Datagridview
- Hướng Dẫn Đăng Nhập Nhiều Tài Khoản Zalo Trên Máy Tính Cực Kỳ Đơn Giản
- [C#] Chia sẻ source code phần mềm đếm số trang tập tin file PDF
- [C#] Cách Sử Dụng DeviceId trong C# Để Tạo Khóa Cho Ứng Dụng
- [SQLSERVER] Loại bỏ Restricted User trên database MSSQL
- [C#] Hướng dẫn tạo mã QRcode Style trên winform
- [C#] Hướng dẫn sử dụng temp mail service api trên winform
- [C#] Hướng dẫn tạo mã thanh toán VietQR Pay không sử dụng API trên winform
- [C#] Hướng Dẫn Tạo Windows Service Đơn Giản Bằng Topshelf
- [C#] Chia sẻ source code đọc dữ liệu từ Google Sheet trên winform
- [C#] Chia sẻ source code tạo mã QR MOMO đa năng Winform
- [C#] Chia sẻ source code phần mềm lên lịch tự động chạy ứng dụng Scheduler Task Winform
- [C#] Hướng dẫn download file từ Minio Server Winform
[C#] Chia sẻ code lock và unlock user trong domain Window
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ó.
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óa
- domain: tên domain của bạn ex: laptrinhvb.net
- userAdmin: nhập tài khoản Admin Domain
- passAdmin: nhập mật khẩu Admin Domain
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);
        }
    }
}
Thanks for watching!

![[C#] Chia sẻ code lock và unlock user trong domain Window](https://laptrinhvb.net/uploads/users/9a8cb514e4428e85fb4ca07588e9103f.png)

![[C#] Chia sẻ source code phần mềm Image Downloader tải hàng loạt hình ảnh từ danh sách link url](https://laptrinhvb.net/uploads/source/new_image_baiviet/tool_image_downloader.png)
![[C#] Chụp hình và quay video từ camera trên winform](https://laptrinhvb.net/uploads/source/new_image_baiviet/thumb_recordvideo_camera_csharp.png)
![[C#] Chia sẽ full source code tách file Pdf thành nhiều file với các tùy chọn](https://laptrinhvb.net/uploads/source/new_image_baiviet/split_pdf_tool_thumb.jpg)


![[C#] Chia sẻ source code phần mềm đếm số trang tập tin file PDF](https://laptrinhvb.net/uploads/source/new_image_baiviet/pdf_counter_tool.png)
![[C#] Cách Sử Dụng DeviceId trong C# Để Tạo Khóa Cho Ứng Dụng](https://laptrinhvb.net/uploads/source/new_image_baiviet/hwid_csharp.png)
![[C#] Hướng dẫn tạo mã QRcode Style trên winform](https://laptrinhvb.net/uploads/source/new_image_baiviet/demo_qrcode_style.png)
![[C#] Hướng dẫn sử dụng temp mail service api trên winform](https://laptrinhvb.net/uploads/source/new_image_baiviet/temp_mail_service.png)
![[C#] Hướng dẫn tạo mã thanh toán VietQR Pay không sử dụng API trên winform](https://laptrinhvb.net/uploads/source/new_image_baiviet/vietqr_image.jpg)
![[C#] Hướng Dẫn Tạo Windows Service Đơn Giản Bằng Topshelf](https://laptrinhvb.net/uploads/source/new_image_baiviet/topsheft_windowsservices.png)
![[C#] Chia sẻ source code đọc dữ liệu từ Google Sheet trên winform](https://laptrinhvb.net/uploads/source/new_image_baiviet/google_sheet_thumb.png)
![[C#] Chia sẻ source code tạo mã QR MOMO đa năng Winform](https://laptrinhvb.net/uploads/source/new_image_baiviet/momo_danang.png)
![[C#] Chia sẻ source code phần mềm lên lịch tự động chạy ứng dụng Scheduler Task Winform](https://laptrinhvb.net/uploads/source/new_image_baiviet/my_scheduler_app.png)
![[C#] Hướng dẫn download file từ Minio Server Winform](https://laptrinhvb.net/uploads/source/new_image_baiviet/Minio_demo.png)
![[C#] Hướng dẫn đăng nhập zalo login sử dụng API v4 trên winform](https://laptrinhvb.net/uploads/source/new_image_baiviet/zalo-api-la-gi.jpg)
![[C#] Việt hóa Text Button trên MessageBox Dialog Winform](https://laptrinhvb.net/uploads/source/new_image_baiviet/change_text_button_messagebox.png)
![[POWER AUTOMATE] Hướng dẫn gởi tin nhắn zalo từ file Excel - No code](https://laptrinhvb.net/uploads/source/new_image_baiviet/zalo_instance_csharp.png)
![[C#] Hướng dẫn bảo mật ứng dụng 2FA (Multi-factor Authentication) trên Winform](https://laptrinhvb.net/uploads/source/new_image_baiviet/microsoft-authenticator-cover.jpg)
![[C#] Hướng dẫn convert HTML code sang PDF File trên NetCore 7 Winform](https://laptrinhvb.net/uploads/source/new_image_baiviet/HTML_TO_PDF_NETCORE.png)
![[C#] Hướng dẫn viết ứng dụng chat với Gemini AI Google Winform](https://laptrinhvb.net/uploads/source/new_image_baiviet/1_AsXti9JBcuEGIODbisEAYw.jpg)


![[C#] Hướng dẫn sử dụng thư viện AutoITx lấy id và password Ultraviewer trên winform](https://laptrinhvb.net/uploads/source/auto_it_thumb.png)
