CÁC GIẢI PHÁP LẬP TRÌNH C# - Trang 546

546
Chương 13: Bảo mật

args[0], // tên người dùng để đăng nhập

".", // sử dụng CSDL tài khoản cục bộ

args[1], // password của người dùng

LOGON32_LOGON_INTERACTIVE, // tạo một interactive login

LOGON32_PROVIDER_DEFAULT, // sử dụng logon provider mặc định

ref accessToken // nhận access token handle

);

// Nếu lỗi xảy ra (LogonUser trả về zero), hiển thị lỗi và thoát.

if (result == 0) {

Console.WriteLine("LogonUser returned error {0}",

Marshal.GetLastWin32Error());

} else {

// Tạo một WindowsIdentity mới từ Windows access token.

WindowsIdentity identity = new WindowsIdentity(accessToken);

// Hiển thị định danh đang tích cực (trước khi giả nhận).

Console.WriteLine("Identity before impersonation = {0}",

WindowsIdentity.GetCurrent().Name);

// Giả nhận người dùng đã được chỉ định. Đối tượng

// WindowsImpersonationContext chứa các thông tin

// cần thiết để trở về ngữ cảnh của người dùng cũ.

WindowsImpersonationContext impContext =

identity.Impersonate();

// Hiển thị định danh đang tích cực (trong lúc giả nhận).

Console.WriteLine("Identity during impersonation = {0}",

WindowsIdentity.GetCurrent().Name);

// ***************************************************************

// Thực hiện các hành động với danh nghĩa người dùng được giả nhận

// ***************************************************************