C# 将字符串变量传递到用于密码参数的网络凭据构造函数中
我拥有的是一个函数,它允许域用户根据其LDAP凭据进行身份验证。但是,只要我将已知密码硬编码为原始字符串,它就可以工作。。。当然,这是一个否定。我希望传入从我设置的C# 将字符串变量传递到用于密码参数的网络凭据构造函数中,c#,authentication,ldap,environment-variables,networkcredentials,C#,Authentication,Ldap,Environment Variables,Networkcredentials,我拥有的是一个函数,它允许域用户根据其LDAP凭据进行身份验证。但是,只要我将已知密码硬编码为原始字符串,它就可以工作。。。当然,这是一个否定。我希望传入从我设置的文本框接收的字符串值。以下是函数: public static bool fnValLDAPCreds() { bool validation; try {
文本框
接收的字符串值。以下是函数:
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;
}
我试图做的是替换文本框中的值,但由于它位于静态bool
中,因此在当前上下文中我没有成功地对控件进行任何外部引用。我在按钮处理程序中调用这个函数来启动它。如何交换从设置的文本框中获取值的字符串DomPassWord
变量
NetworkCredential NetCred=新的NetworkCredential(Environment.UserName、domapassword、Environment.UserDomainName)
是我努力追求的目标,因为我可以使用类似于DomPassWord=txtextserpw.Text
的东西在域中安全地匹配密码,而无需硬编码。尝试了SecureString
路由,但在这方面也没有成功。有什么想法吗?您无法访问静态方法中的文本框,因为它们不是静态字段(至少从您编写的内容来看是这样的)
但是您可以简单地将参数传递给您的方法。将其更改为以下内容:
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;
}
}
有点相切,但是你考虑过AuthType.Ntlm吗?如果你这么做只是为了通过让用户“Charlie”输入密码来确保他就是Charlie?那你就走对了。但是,如果您试图使用当前用户凭据连接到广告,作为访问广告本身的一种方式?那么你可能想看看
ADConn.AuthType = AuthType.Ntlm;
。。。让windows为您处理这个问题(根本不需要用户输入密码-它将使用他们当前的windows凭据。)为什么不在FnValldapcles方法中添加一个字符串密码参数,用密码替换“Password123”
,并像FnValldapcles(myTextBox.Text)那样调用它呢
?或者你的问题不是很清楚我很难想象更大的画面。您这样做是为了确保以“bob”身份登录的用户实际上是bob,让他重新键入密码吗?还是你在其他地方用它作为论点来完成一份证书?多亏了所有有洞察力的人,我希望我能分得赏金,但这是恰到好处的。目标如前所述。。。确保查理真的是“查理”。。。Charlie知道他的域id和域密码。为了进一步说明,我不希望NTLM通过基于正确用户名的creds。如果Charlie更改为一个可笑的密码字符串,他需要输入该字符串以进行身份验证。我知道此时我可以通过SecureString
加密密码,甚至可以在框上设置UseSystemPasswordChar
,但在字符串传递到函数时模糊了!