Authenticating in Active Directory over VPN in C#

r3nder
r3nder used Ask the Experts™
on
I am unable to authenticate while logged into a VPN client through my application - I am able to use the wireless router outside the network at work and authenticate but when I go home and load my VPN client and start the app I created I can not get authenticated - it returns false.
I have also tried using LOGON32_LOGON_INTERACTIVE and that didnt work. I used LOGON32_LOGON_NEW_CREDENTIALS and everything returned true even if the login was "blah"
Here is what I am using

  public void btnLogon_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty("DOMAIN".Trim()) && !String.IsNullOrEmpty(txtBxUser.Text.Trim()))
            {


                if (LogonHelper.VerifyADLogon("DOMAIN", txtBxUser.Text, txtBxPass.Text))
                {
                    MessageBox.Show("Your login has been authenticated.......Thank you", "Info");
                    tabControl1.TabPages.Add(tabKnowledge);
                    tabControl1.TabPages.Add(tabWeb);
                    tabControl1.TabPages.Add(tabCamp);
                    tabControl1.TabPages.Add(tabInstall);
                    btnEVid.Enabled = true;
                    btnStartLP.Enabled = true;
                    btnLogon.Enabled = false;

                }


                else
                    MessageBox.Show("You password is not correct.....Please retry", "Retry");
                txtBxPass.Focus();
            }
            else
                MessageBox.Show("You must enter at least a Username and a Password.", "Info");


        }

Open in new window


and here is the logonhelper
   static class LogonHelper
    {
        [DllImport("kernel32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool CloseHandle(IntPtr hObject);

        [DllImport("advapi32.dll", SetLastError = true)]
        private static extern bool LogonUser(
            string lpszUsername,
            string lpszDomain,
            string lpszPassword,
            LogonType dwLogonType,
            LogonProvider dwLogonProvider,
            out IntPtr phToken
            );

        private enum LogonType : int
        {
            /// <summary>
            /// This logon type is intended for users who will be interactively using the computer, such as a user being logged on  
            /// by a terminal server, remote shell, or similar process.
            /// This logon type has the additional expense of caching logon information for disconnected operations; 
            /// therefore, it is inappropriate for some client/server applications,
            /// such as a mail server.
            /// </summary>
            LOGON32_LOGON_INTERACTIVE = 2,

            /// <summary>
            /// This logon type is intended for high performance servers to authenticate plaintext passwords.

            /// The LogonUser function does not cache credentials for this logon type.
            /// </summary>
            LOGON32_LOGON_NETWORK = 3,

            /// <summary>
            /// This logon type is intended for batch servers, where processes may be executing on behalf of a user without 
            /// their direct intervention. This type is also for higher performance servers that process many plaintext
            /// authentication attempts at a time, such as mail or Web servers. 
            /// The LogonUser function does not cache credentials for this logon type.
            /// </summary>
            LOGON32_LOGON_BATCH = 4,

            /// <summary>
            /// Indicates a service-type logon. The account provided must have the service privilege enabled. 
            /// </summary>
            LOGON32_LOGON_SERVICE = 5,

            /// <summary>
            /// This logon type is for GINA DLLs that log on users who will be interactively using the computer. 
            /// This logon type can generate a unique audit record that shows when the workstation was unlocked. 
            /// </summary>
            LOGON32_LOGON_UNLOCK = 7,

            /// <summary>
            /// This logon type preserves the name and password in the authentication package, which allows the server to make 
            /// connections to other network servers while impersonating the client. A server can accept plaintext credentials 
            /// from a client, call LogonUser, verify that the user can access the system across the network, and still 
            /// communicate with other servers.
            /// NOTE: Windows NT:  This value is not supported. 
            /// </summary>
            LOGON32_LOGON_NETWORK_CLEARTEXT = 8,

            /// <summary>
            /// This logon type allows the caller to clone its current token and specify new credentials for outbound connections.
            /// The new logon session has the same local identifier but uses different credentials for other network connections. 
            /// NOTE: This logon type is supported only by the LOGON32_PROVIDER_WINNT50 logon provider.
            /// NOTE: Windows NT:  This value is not supported. 
            /// </summary>
            LOGON32_LOGON_NEW_CREDENTIALS = 9,
        }

        private enum LogonProvider : int
        {
            /// <summary>
            /// Use the standard logon provider for the system. 
            /// The default security provider is negotiate, unless you pass NULL for the domain name and the user name 
            /// is not in UPN format. In this case, the default provider is NTLM. 
            /// NOTE: Windows 2000/NT:   The default security provider is NTLM.
            /// </summary>
            LOGON32_PROVIDER_DEFAULT = 0,
        }

        public static bool VerifyADLogon(string DomainName, string Username, string Password)
        {
            IntPtr hToken;

            LogonUser(Username, DomainName, Password, LogonType.LOGON32_LOGON_NETWORK, LogonProvider.LOGON32_PROVIDER_DEFAULT, out hToken);

            if (hToken != IntPtr.Zero)
            {
                CloseHandle(hToken);
                return true;
            }
            else
                return false;
        }
    }

Open in new window


If anyone has any ideas I would greatly appreciate it
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
I found out by changing the username and password at my home PC to my username and password at work - that it then authenticates - anybody got any ideas?
Commented:
This is the solution - On the local machine that is outside the domain got to the control panel go to user accounts - select your home account - if you do not have a password on this user you will have to add one.
Log in as that user
go to user accounts again and select the user
On the left hand side you will see "Manage my network passwords"
Click it then type the IP or domain name (i used IP)
under username put domain\your username
and leave the password blank
click ok
Now next time the app or computer looks for that domain it will use the application credentials and domain to authenticate.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial