internal static bool BeginImpersonation(NetCred credential, out IntPtr hUserToken) { hUserToken = IntPtr.Zero; IntPtr zero = IntPtr.Zero; if (credential != null) { string parsedUserName = credential.ParsedUserName; string password = credential.Password; string domain = credential.Domain; if (parsedUserName != null || password != null) { int num = UnsafeNativeMethods.LogonUser(parsedUserName, domain, password, 9, 3, ref zero); if (num != 0) { num = UnsafeNativeMethods.ImpersonateLoggedOnUser(zero); if (num != 0) { hUserToken = zero; return(true); } else { int lastWin32Error = Marshal.GetLastWin32Error(); UnsafeNativeMethods.CloseHandle(zero); object[] objArray = new object[1]; objArray[0] = lastWin32Error; throw new PrincipalOperationException(string.Format(CultureInfo.CurrentCulture, StringResources.UnableToImpersonateCredentials, objArray)); } } else { int lastWin32Error1 = Marshal.GetLastWin32Error(); object[] objArray1 = new object[1]; objArray1[0] = lastWin32Error1; throw new PrincipalOperationException(string.Format(CultureInfo.CurrentCulture, StringResources.UnableToImpersonateCredentials, objArray1)); } } else { return(false); } } else { return(false); } }
internal static bool BeginImpersonation(NetCred credential, out IntPtr hUserToken) { GlobalDebug.WriteLineIf(GlobalDebug.Info, "Utils", "Entering BeginImpersonation"); hUserToken = IntPtr.Zero; IntPtr hToken = IntPtr.Zero; // default credential is specified, no need to do impersonation if (credential == null) { GlobalDebug.WriteLineIf(GlobalDebug.Info, "Utils", "BeginImpersonation: nothing to impersonate"); return(false); } // Retrive the parsed username which has had the domain removed because LogonUser // expects creds this way. string userName = credential.ParsedUserName; string password = credential.Password; string domainName = credential.Domain; // no need to do impersonation as username and password are both null if (userName == null && password == null) { GlobalDebug.WriteLineIf(GlobalDebug.Info, "Utils", "BeginImpersonation: nothing to impersonate (2)"); return(false); } GlobalDebug.WriteLineIf(GlobalDebug.Info, "Utils", "BeginImpersonation: trying to impersonate " + userName); int result = UnsafeNativeMethods.LogonUser( userName, domainName, password, 9, /* LOGON32_LOGON_NEW_CREDENTIALS */ 3, /* LOGON32_PROVIDER_WINNT50 */ ref hToken); // check the result if (result == 0) { int lastError = Marshal.GetLastWin32Error(); GlobalDebug.WriteLineIf(GlobalDebug.Error, "Utils", "BeginImpersonation: LogonUser failed, gle=" + lastError); throw new PrincipalOperationException( String.Format(CultureInfo.CurrentCulture, StringResources.UnableToImpersonateCredentials, lastError)); } result = UnsafeNativeMethods.ImpersonateLoggedOnUser(hToken); if (result == 0) { int lastError = Marshal.GetLastWin32Error(); GlobalDebug.WriteLineIf(GlobalDebug.Error, "Utils", "BeginImpersonation: ImpersonateLoggedOnUser failed, gle=" + lastError); // Close the token the was created above.... UnsafeNativeMethods.CloseHandle(hToken); throw new PrincipalOperationException( String.Format(CultureInfo.CurrentCulture, StringResources.UnableToImpersonateCredentials, lastError)); } hUserToken = hToken; return(true); }