c# winform 活动目录:如果登录成功,则访问另一个表单

c# winform active directory: acces to another form if login succes(c# winform 活动目录:如果登录成功,则访问另一个表单)
本文介绍了c# winform 活动目录:如果登录成功,则访问另一个表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想从我有登录文本框和密码文本框以及登录按钮的表单创建一个控件.当我输入活动目录帐户名及其密码时,我想转到另一个表单.有人可以帮我解决这个问题.在此代码示例中,我选择了仅用于登录的帐户.我想选择它并输入密码并通过例如从表单(登录)到表单(用户界面)进入目标表单.

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.

  1. 验证文本框 (!string.Empty).
  2. 验证凭据.
  3. 根据用户类型显示您想要的表单.

当您正确拆分代码时,这很容易.

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

如前所述,由于您是 C# 新手,这里有一些建议:

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

  1. 拆分代码:方法必须简短且按用途分开.这对您和任何处理您的代码的人来说都更容易
  2. 管理异常
  3. 处理对象
  4. 注意这个 txtboxlogin.Text == "WantedAdminUser" 它很危险.
  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.

这篇关于c# winform 活动目录:如果登录成功,则访问另一个表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

What#39;s the difference between retrieving WindowsPrincipal from WindowsIdentity and Thread.CurrentPrincipal?(从 WindowsIdentity 和 Thread.CurrentPrincipal 检索 WindowsPrincipal 之间有什么区别?)
How do I find a user#39;s Active Directory display name in a C# web application?(如何在 C# Web 应用程序中查找用户的 Active Directory 显示名称?)
How can I authenticate against Active Directory in Nancy?(如何在 Nancy 中对 Active Directory 进行身份验证?)
How to get a username in Active Directory from a display name in C#?(如何从 C# 中的显示名称获取 Active Directory 中的用户名?)
Working with DirectoryServices in ASP.NET Core(在 ASP.NET Core 中使用 DirectoryServices)
Create Active Directory user in .NET (C#)(在 .NET (C#) 中创建 Active Directory 用户)