c# winform active directory: acces to another form if login succes

前端 未结 4 2247
礼貌的吻别
礼貌的吻别 2021-01-26 07:56

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

4条回答
  •  渐次进展
    2021-01-26 08:15

    Here is your code, edited.

    1. Verify the textboxes (!string.Empty).
    2. Validate credentials.
    3. Display the form you want depending on user type.

    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 == "WantedAdminUser")
                {
                    using (AdminForm form2 = new AdminForm())
                       form2.ShowDialog();
                    Show(); 
                }
                else
                {
                    using (user userform = new user())
                        userform.ShowDialog();
                    Show();
                }
            }
        }
    }
    

    As said previously, as you are new in C#, here are some advice:

    1. Split your code: methods must be short and separated by purpose. It's easier for you and for any who works on your code
    2. Manage the exception
    3. Dispose objects
    4. Take care about this txtboxlogin.Text == "WantedAdminUser" it's dangerous.

提交回复
热议问题