Diễn đàn    Window Form programming    Đăng nhập dùng tài khoản của windows

Thành viênTrả lời
aspnet

Lập trình không biên giới
608  bài
24-3-2009 14:6:17
public class WindowAuthenticate
{
[DllImport("advapi32.dll", SetLastError = true)]
static extern bool LogonUser(
string principal,
string authority,
string password,
LogonSessionType logonType,
LogonProvider logonProvider,
out IntPtr token);
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool CloseHandle(IntPtr handle);
enum LogonSessionType : uint
{
Interactive = 2,
Network,
Batch,
Service,
NetworkCleartext = 8,
NewCredentials
}
enum LogonProvider : uint
{
Default = 0, // default for platform (use this!)
WinNT35, // sends smoke signals to authority
WinNT40, // uses NTLM
WinNT50 // negotiates Kerb or NTLM
}

public static bool UserLogin(string username, string domain, string password, System.Web.HttpResponse response)
{
IntPtr token = IntPtr.Zero;
WindowsImpersonationContext impersonatedUser = null;

try
{
// Create a token for DomainName\Bob
// Note: Credentials should be encrypted in configuration file
bool result = LogonUser(username, domain,
password,
LogonSessionType.Interactive,
LogonProvider.Default,
out token);
if (result)
{
WindowsIdentity id = new WindowsIdentity(token);

// Begin impersonation
impersonatedUser = id.Impersonate();
string full_username = WindowsIdentity.GetCurrent().Name;
System.Web.Security.FormsAuthentication.SetAuthCookie(full_username, false);

return true;
}
else
{
response.Write("</p>LogonUser failed: " +
Marshal.GetLastWin32Error().ToString());
}
}
catch
{
// Prevent any exceptions that occur while the thread is
// impersonating from propagating
}
finally
{
// Stop impersonation and revert to the process identity
if (impersonatedUser != null) impersonatedUser.Undo();
// Free the token
if (token != IntPtr.Zero) CloseHandle(token);
}

return false;
}
}
 

Chủ đề gần đây :

Cùng loại :

Tên file Mô tả chi tiết Ngày
NWeb.zip (1) Module đơn giản Newsweb trên Dotnetnuke v10.x.x.x10/18/2025 8:08:11 AM
vspforum.zip (11) Ma nguon vspforum ngay xua4/18/2023 6:38:37 AM
pdfjs.rar (2) pdfjs 2017 : hiển thị tốt trên iphone 11, 12, 13 không lỗi, bản 2012 sẽ lỗi trên iphone6/21/2022 11:52:48 AM
pdfjs2.rar (2) Xem file pdf bằng viewer.hml cua pdfjs (thư viện chuẩn mozilla) 2012. https://mozilla.github.io/pdf.js/getting_started/#download có thể download bản prebuild tại đây6/21/2022 11:52:04 AM
runner.zip (0) using three.js, orbitcontrol to view an object move random on map. Di chuyển 1 đồ vật ngẫu nhiên trên bản đồ, sử dụng với demo nhân viên di chuyển trong văn phòng. Toàn js download về là chạy12/5/2019 5:55:14 PM
gmap.zip (1) google map + marker7/17/2019 2:25:05 PM
vinsmarthomeservice.zip (1) java post json to api, use AsyncTask, event listener7/9/2019 5:00:10 PM
fblogin.zip (0) Login facebook bang javascript SDK7/9/2019 9:16:37 AM
autocomplete-location.zip (2) autocomplete location geo from google place, html + js7/4/2019 4:37:55 PM
WebAPI.zip (8) api for android access db (v1.0.0)7/4/2019 9:14:17 AM