Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

Authenticating in Active Directory over VPN in C#

Avatar of r3nder
r3nderFlag for United States of America asked on
ProgrammingVPNC#Active Directory
2 Comments1 Solution1463 ViewsLast Modified:
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");


        }

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;
        }
    }

If anyone has any ideas I would greatly appreciate it
ASKER CERTIFIED SOLUTION
Avatar of r3nder
r3nderFlag of United States of America image

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Commented:
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answers