Hi Martin,
Here's the code I'm using. Please note that the Win32 API calls are wrapped
up in a static class called Impersonation:
IntPtr pWindowsIdentity = IntPtr.Zero;
int iResult = Impersonation.LogonUser("MyAccount",
"MyDomain", "MyPassword", Impersonation.LOGON32_LOGON_INTERACTIVE,
Impersonation.LOGON32_PROVIDER_DEFAULT, ref pWindowsIdentity);
if (iResult == 0 && pWindowsIdentity != IntPtr.Zero) {
WindowsIdentity oNewWI = new WindowsIdentity(pWindowsIdentity);
Impersonation.CloseHandle(pWindowsIdentity);
WindowsImpersonationContext oWIC = oNewWI.Impersonate();
// .... Do code to access network resource here
oWIC.Undo();
}
else {
int iError = Impersonation.GetLastError();
throw new Exception(string.Format("Could not logon user using credentials
provided. Error No: {0} - Error: {1}", iError,
Impersonation.CreateLogonUserError(iError)));
}
Regards
Ben
> Hi Ben,
>
[quoted text clipped - 35 lines]
> >
> > Ben
Ben Fidge - 16 Dec 2005 13:37 GMT
Found it!!
First of all, I was assuming that LogonUser returned 0 (zero) on success,
wrongly. Secondly, I changed my code to use LOGON32_LOGON_NEW_CREDENTIALS
instead of LOGON32_LOGON_NETWORK, and it works a treat.
Ben
> Hi Martin,
>
[quoted text clipped - 67 lines]
> > >
> > > Ben