Search code examples
c#authenticationldapenvironment-variablesnetworkcredentials

Pass String Variable Into Nework Credential Constructor for Password Arguement


What I have is a function that allows a domain user to be authenticated against their LDAP credentials. However, it works long as I hard-code a known password as a raw string... which is a no-no, of course. I wish to pass in a string value received from a TextBox I have set up. Here is the function:

public static bool fnValLDAPCreds()
    {
        bool validation;                                 

        try
        {                

            LdapConnection ADConn = new LdapConnection(new LdapDirectoryIdentifier((string)null, false, false));
            NetworkCredential NetCred = new NetworkCredential(Environment.UserName, "Password123",  Environment.UserDomainName);

            ADConn.Credential = NetCred;
            ADConn.AuthType = AuthType.Negotiate;
            // the user's authenticated here; creds used to login on the domain controller.

            ADConn.Bind(NetCred);
            validation = true;
            MessageBox.Show("You were successfully authenticated against AD using LDAP!");
        }

        catch (LdapException)
        {
            validation = false;
            MessageBox.Show("Your login was unsuccesful. Try a different set of credentials.");
        }

        return validation;
    }

What I've tried to do was substitute in a value from my TextBox, but since it lies in the static bool I have not been successful with making any external references to a control in the current context. I'm calling this function in button handler to fire it off. How can I swap in a string DomPassWord variable that gets its value from the textbox I have setup to obtain it?

NetworkCredential NetCred = new NetworkCredential(Environment.UserName, DomPassWord, Environment.UserDomainName); is what I'm striving for, as I can securely match a password in the domain with no hard-coding, using something like DomPassWord = txtUserPW.Text. Tried the SecureString route, but was unsuccessful in that regard as well. Any ideas?


Solution

  • You cannot access text boxes inside a static method, since they aren't static fields (at least it looks like it from what you've written).

    But you can simply pass your arguments to your method. Change it to something like this:

    public void ButtonClick(object sender, EventArgs args)
    {
        // bool valid = fnValLDAPCreds(Environment.UserName, "Password123", Environment.UserDomainName);
        bool valid = fnValLDAPCreds(txtUserName.Text, txtUserPW.Text, Environment.UserDomainName);
    }
    
    public static bool fnValLDAPCreds(string username, string password, string domain)
    {
        try
        {
            LdapConnection ADConn = new LdapConnection(new LdapDirectoryIdentifier((string)null, false, false));
            NetworkCredential NetCred = new NetworkCredential(username, password,  domain);
    
            ADConn.Credential = NetCred;
            ADConn.AuthType = AuthType.Negotiate;
            // the user's authenticated here; creds used to login on the domain controller.
    
            ADConn.Bind(NetCred);
            MessageBox.Show("You were successfully authenticated against AD using LDAP!");
            return true;
        }
        catch (LdapException)
        {
            MessageBox.Show("Your login was unsuccesful. Try a different set of credentials.");
            return false;
        }
    }