00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 using System;
00013 using System.Runtime.InteropServices;
00014
00015 public class Win32Impersonation
00016 {
00017 [DllImport("advapi32.dll", SetLastError = true)]
00018 private static extern bool LogonUser(
00019 string principal,
00020 string authority,
00021 string password,
00022 LogonSessionType logonType,
00023 LogonProvider logonProvider,
00024 out IntPtr token);
00025
00026 [DllImport("kernel32.dll", SetLastError = true)]
00027 public static extern bool CloseHandle(IntPtr handle);
00028
00029 public static bool LogonUser(string userName, string domainName, string password, LogonSessionType sessionType, out IntPtr userToken)
00030 {
00031 return LogonUser(userName,
00032 domainName,
00033 password,
00034 sessionType,
00035 LogonProvider.Default,
00036 out userToken);
00037 }
00038
00039 public static int GetLastError()
00040 {
00041 return Marshal.GetLastWin32Error();
00042 }
00043
00044 #region Nested type: LogonProvider
00045
00046 public enum LogonProvider : uint
00047 {
00048 Default = 0,
00049 WinNT35,
00050 WinNT40,
00051 WinNT50
00052 }
00053
00054 #endregion
00055
00056 #region Nested type: LogonSessionType
00057
00058 public enum LogonSessionType : uint
00059 {
00060 Interactive = 2,
00061 Network,
00062 Batch,
00063 Service,
00064 NetworkCleartext = 8,
00065 NewCredentials
00066 }
00067
00068 #endregion
00069 }