2

我所拥有的功能是允许域用户对其LDAP凭证进行身份验证。然而,当我将一个已知的密码硬编码为一个原始字符串时,它的工作很长......当然,这是一个不行的密码。我希望传入从我设置的TextBox收到的字符串值。下面是函数:将字符串变量传入用于口令争用的Nework证书构造函数

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

什么我试图从我TextBox做一个值是替补,但由于它位于static bool我都没有成功作出任何外部引用的一个控制当前上下文。我在按钮处理函数中调用这个函数来关闭它。我怎样才能换一个string DomPassWord变量,从我设置的文本框中获取它的值来获取它?

NetworkCredential NetCred = new NetworkCredential(Environment.UserName, DomPassWord, Environment.UserDomainName);是我努力的工作,因为我可以使用类似DomPassWord = txtUserPW.Text的东西在域中安全地匹配密码而无需硬编码。尝试了SecureString路线,但在这方面也没有成功。有任何想法吗?

+2

为什么不给fnValLDAPCreds方法添加一个'string password'参数,用''password'替换''Password123'',并将其称为'fnValLDAPCreds(myTextBox.Text)'?或者你的问题不是很清楚 –

+0

我很难看到更大的图片。你是否这样做,以确保登录为'bob'的用户实际上是bob,通过让他重新输入他的密码?或者你是否在其他地方用它作为证据来完成证书? – Kevin

回答

5

你不能访问静态方法中的文本框,因为它们不是静态字段(至少它看起来像你写的内容)。

但是,您可以简单地将您的参数传递给您的方法。改变它是这样的:

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

感谢每一位具有良好洞察力的人,我希望我可以分享赏金,但这是现场。目标如前所述......确保查理真的是“查理”......查理是知道他的域名ID和域密码的人。 – DesignerMind

+0

为了进一步阐述,我不希望NTLM通过基于正确用户名的信用传递。如果查理更改为一个荒谬的密码字符串,他将需要输入该字符串进行身份验证。我知道在这一点上我可以通过'SecureString'加密密码,甚至在盒子上设置'UseSystemPasswordChar',但是在字符串传递功能上模糊了! – DesignerMind

0

有点切线,但你有没有想过AuthType.Ntlm?如果你这样做的目的是通过让用户输入他的密码来确保用户'Charlie'实际上是Charlie?那么你在正确的轨道上。但是,如果您尝试使用当前用户凭据连接到AD,以此作为获取AD本身的方式?然后,你可能想看看

ADConn.AuthType = AuthType.Ntlm; 

...并让Windows处理这件事情(无需在用户输入密码类型在所有 - 它会使用当前Windows凭据)

相关问题