问题描述
我想从我有登录文本框和密码文本框以及登录按钮的表单创建一个控件.当我输入活动目录帐户名及其密码时,我想转到另一个表单.有人可以帮我解决这个问题.在此代码示例中,我选择了仅用于登录的帐户.我想选择它并输入密码并通过例如从表单(登录)到表单(用户界面)进入目标表单.
i want to create a control from a form where i have login textbox and password textbox, and login button. when i will enter the active directory account name and its password i want to go to another form. someone can help me with this please. in this code example i chose the account for login only. i want to chose it and type the password and go the destination form by exemple from form (login) to form (user interface).
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 { } } }
推荐答案
这是您的代码,已编辑.
Here is your code, edited.
- 验证文本框 (!string.Empty).
- 验证凭据.
- 根据用户类型显示您想要的表单.
当您正确拆分代码时,这很容易.
It is easy as pie, when you correctly split your code.
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 == "Wanted*****User") { using (*****Form form2 = new *****Form()) form2.ShowDialog(); Show(); } else { using (user userform = new user()) userform.ShowDialog(); Show(); } } } }
如前所述,由于您是 C# 新手,这里有一些建议:
As said previously, as you are new in C#, here are some advice:
- 拆分代码:方法必须简短且按用途分开.这对您和任何处理您的代码的人来说都更容易
- 管理异常
- 处理对象
- 注意这个 txtboxlogin.Text == "Wanted*****User" 它很危险.
- Split your code: methods must be short and separated by purpose. It's easier for you and for any who works on your code
- Manage the exception
- Dispose objects
- Take care about this txtboxlogin.Text == "Wanted*****User" it's dangerous.