Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
4.75/5 (4 votes)
See more:
Hello Everybody

I'm writing an application that allows to impersonate a user if required.
However, the impersonation keeps failing with message: "Logon failure: unknown user name or bad password".
Even the error sounds pretty clear, it can't be the case, cause the credentials are valid and I'm able to logon to the domain using the given credentials. I also tried different examples, which return the same error

MSDN Example[^]
or
A Complete Impersonation Demo in C#.NET[^]
or
User Impersonation in .NET[^]

Can anybody point-out what I'm doing wrong? The machine I'm testing on it not joined to any domain though. Could that be the problem?

Here's the code I'm using:

C#
public Impersonation(string domain, string username, string password, LogonType LOGON_TYPE, LogonProvider LOGON_PROVIDER)
        {
            bool ok = LogonUser(username, domain, password, (int)LOGON_TYPE, (int)LOGON_PROVIDER, out this._handle);
            if (!ok)
            {
                int ret = Marshal.GetLastWin32Error();
                throw new System.ComponentModel.Win32Exception(ret);
            }

            this._context = WindowsIdentity.Impersonate(this._handle.DangerousGetHandle());
        }

        public void Dispose()
        {
            this._context.Dispose();
            this._handle.Dispose();
        }

        [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
        private static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, out SafeTokenHandle phToken);

The calling method:
C#
try
            {
                _slImpersonation = new Impersonation(tbx_Domain.Text, tbx_UserName.Text, tbx_Password.Text, LogonType.LOGON32_LOGON_INTERACTIVE, LogonProvider.LOGON32_PROVIDER_DEFAULT);
                toolStripStatusLabel1.Text = "Impersonation succeeded";
                _slImpersonation.Dispose();
                _slImpersonation = null;
            }
            catch (Exception exp)
            {
                toolStripStatusLabel1.Text = "Impersonation failed";
                MessageBox.Show(this, exp.Message, "Error", MessageBoxButtons.OK);
                _slImpersonation = null;
                toolStripStatusLabel1.Text = "";
            }


Can anybody explain why this keeps failing although the credentials are valid?

Thanks very much for your answers
Posted
Updated 4-Feb-13 6:31am
v3
Comments
CHill60 4-Feb-13 10:50am    
For a starter - put a breakpoint on bool ok = LogonUser(username, domain, password, (int)LOGON_TYPE, (int)LOGON_PROVIDER, out this._handle); and run in debug mode. Check that the user details are really what you think they are
genese1977 4-Feb-13 10:57am    
Hi CHill60, I did that again, to verify and yes the credentials are as they should be. I also tried another set which also fails.
Interesting enough if I change the calling method to
_slImpersonation = new Impersonation(tbx_Domain.Text, tbx_UserName.Text, tbx_Password.Text, LogonType.LOGON32_LOGON_NEW_CREDENTIALS, LogonProvider.LOGON32_PROVIDER_DEFAULT);
it always succeeds even when providing wrong credentials. This is really wired.
CHill60 4-Feb-13 12:05pm    
Agree it's weird! Also agree with Marco - well formed question. This might come down to some environment "feature" ... what platform are you running on?
Marco Bertschi 4-Feb-13 11:22am    
Eventhough I don't know the answer, I like to congrat you because of the good formatted question which has a good code sample!
genese1977 4-Feb-13 12:22pm    
Thanks for the compliment guys!
@CHill60: This might come down to some environment "feature"
Well this is an Microsoft active directory. ADS is of version 2k8. The machine I'm testing on is not part of any domain but member of "workgroup".

Trying to impersonate against a different domain behaves the same. I even ran the application as local administrator and it still failed. This thing is driving me nuts :)

Is there additional logging one could turn on to better troubleshoot this issue?
Thanks very much for you help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900