我想从具有登录文本框和密码文本框以及登录按钮的表单创建控件。当我输入活动目录帐户名及其密码时,我想使用另一种形式。有人可以帮我这个忙。在此代码示例中,我选择了仅用于登录的帐户。我想选择它并输入密码,然后从表单(登录)到表单(用户界面)按目的地进入目的地表单。
private void radiobtnAD_CheckedChanged(object sender, EventArgs e)
{
if (radiobtnAD.Checked)
{
try
{
string filter = "(&(objectCategory=person)(objectClass=user)(!userAccountControl:1.2.840.113556.1.4.803:=2))";
string[] propertiesToLoad = new string[1] { "name" };
using (DirectoryEntry root = new DirectoryEntry("LDAP://DOMAIN"))
using (DirectorySearcher searcher = new DirectorySearcher(root, filter, propertiesToLoad))
using (SearchResultCollection results = searcher.FindAll())
{
foreach (SearchResult result in results)
{
string name = (string)result.Properties["name"][0];
comboBox1.Items.Add(name);
}
}
}
catch
{
}
}
}
这是您的代码,已编辑。
当您正确分割代码时,这很容易做到。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using LibXmlSettings.Settings;
using Microsoft.ApplicationBlocks.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.DirectoryServices;
using System.IO;
using System.Linq.Expressions;
using System.Runtime.InteropServices;
using System.DirectoryServices.AccountManagement;
namespace GestionnaireDesPlugins
{
public partial class Login : Form
{
public Login(string accountName, string accountPassword)
{
InitializeComponent();
}
private void LoginOnClick(object sender, EventArgs e)
{
if (IsValid())
{
GetUser();
// Do whatever you want
ShowForm();
}
}
private void GetUser()
{
try
{
LdapConnection connection = new LdapConnection("AD");
NetworkCredential credential = new NetworkCredential(txtboxlogin.Text, txtboxpass.Text);
connection.Credential = credential;
connection.Bind();
}
catch (LdapException lexc)
{
String error = lexc.ServerErrorMessage;
MessageBox.Show("error account or password.");
}
catch (Exception exc)
{
MessageBox.Show(exc.ToString());
}
}
private bool IsValid()
{
// Check if the user haven't chosen an account
if (!string.IsNullOrEmpty(txtboxlogin.Text) { return false; }
// Check if the user haven't chosen an account
if (!string.IsNullOrEmpty(txtboxpass.Text)) { return false; }
// Check if the user haven't chosen an account
if (!string.IsNullOrEmpty(comboBox1.Text)) { return false; }
// Check if the password TextBox is empty
if (!string.IsNullOrEmpty(textBox1.Text)) { return false; }
using(PrincipalContext pc = new PrincipalContext(ContextType.Domain, "YOURDOMAIN"))
{
// validate the credentials
bool isValid = pc.ValidateCredentials(txtboxlogin.Text, txtboxpass.Text);
}
return isValid;
}
private void ShowForm()
{
if (txtboxlogin.Text == "WantedAdminUser")
{
using (AdminForm form2 = new AdminForm())
form2.ShowDialog();
Show();
}
else
{
using (user userform = new user())
userform.ShowDialog();
Show();
}
}
}
}
如前所述,由于您是C#的新手,因此这里有一些建议:
txtboxlogin.Text == "WantedAdminUser"
很危险。本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句