Exemplo n.º 1
0
 internal extern static bool LogonUserA(
     string lpszUsername,
     string lpszDomain,
     string lpszPassword,
     LOGON_TYPE dwLogonType,
     LOGON_PROVIDER dwLogonProvider,
     out IntPtr phToken);
Exemplo n.º 2
0
 public static extern bool LogonUserA(
     string lpszUsername,
     string lpszDomain,
     string lpszPassword,
     LOGON_TYPE dwLogonType,
     LOGON_PROVIDER dwLogonProvider,
     ref IntPtr phToken);
 internal static extern bool LogonUser(
     [MarshalAs(UnmanagedType.LPWStr)] string username,
     [MarshalAs(UnmanagedType.LPWStr)] string domain,
     [MarshalAs(UnmanagedType.LPWStr)] string password,
     LOGON_TYPE logonType,
     LogonProvider logonProvider,
     out SafeFileHandle token);
Exemplo n.º 4
0
        public static bool MakeToken(string Username, string Domain, string Password, LOGON_TYPE LogonType = LOGON_TYPE.LOGON32_LOGON_NEW_CREDENTIALS)
        {
            IntPtr hProcessToken = IntPtr.Zero;

            if (!LogonUserA(
                    Username, Domain, Password,
                    LogonType,
                    LOGON_PROVIDER.LOGON32_PROVIDER_WINNT50,
                    ref hProcessToken))
            {
                Console.Error.WriteLine("LogonUserA() Error: " + new Win32Exception(Marshal.GetLastWin32Error()).Message);
                return(false);
            }

            if (!ImpersonateLoggedOnUser(hProcessToken))
            {
                Console.Error.WriteLine("ImpersonateLoggedOnUser() Error: " + new Win32Exception(Marshal.GetLastWin32Error()).Message);
                CloseHandle(hProcessToken);
                return(false);
            }
            return(true);
        }