C#WinForm的活动目录:存取权限为另一种形式,如果登录更迭权限、形式、目录、WinForm

由网友(别笑了眼泪都掉了)分享简介:我想创建从那里我已经登录文本框和密码文本框,以及登录按钮的窗体控件。当我将进入活动目录帐户名和密码我要到另一种形式。有人可以帮我这个吧。在这code例如我选择了只占登录。我想选择它并键入密码,并从表(登录)为例去目的地的形式,形成(用户界面)。私人无效radiobtnAD_CheckedChanged(对象发件...

我想创建从那里我已经登录文本框和密码文本框,以及登录按钮的窗体控件。 当我将进入活动目录帐户名和密码我要到另一种形式。 有人可以帮我这个吧。 在这code例如我选择了只占登录。 我想选择它并键入密码,并从表(登录)为例去目的地的形式,形成(用户界面)。

 私人无效radiobtnAD_CheckedChanged(对象发件人,EventArgs的)
    {
        如果(radiobtnAD.Checked)
        {
           尝试
        {

            串滤波器=(及(objectCategory属性=人)(objectClass的=用户)(userAccountControl的:1.2.840.113556.1.4.803:= 2));
            字符串[] propertiesToLoad =新的字符串[1] {姓名};


            使用(的DirectoryEntry根=新的DirectoryEntry(LDAP://域))
            使用(DirectorySearcher从搜索=新DirectorySearcher从(根,过滤器,propertiesToLoad))
            使用(SearchResultCollection结果= searcher.FindAll())
            {
                的foreach(信息搜索结果导致的结果)
                {
                    字符串名称=(字符串)result.Properties [名称] [0];
                    comboBox1.Items.Add(名称);

                }
            }
        }
        抓住
        {
        }
        }

    }
 

解决方案

下面是你的code,编辑。

验证文本框(!的String.Empty)。 验证凭据。 显示您要根据用户类型的形式。

这是易如反掌,当你正确地分割你的code。

 使用系统;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data这;
使用System.Drawing中;
使用System.Linq的;
使用System.Text;
使用System.Threading.Tasks;
使用System.Windows.Forms的;
使用LibXmlSettings.Settings;
使用Microsoft.ApplicationBlocks.Data;
使用System.Data.Sql;
使用System.Data.SqlClient的;
使用System.Data.SqlTypes;
使用System.DirectoryServices中;
使用System.IO;
使用System.Linq.Ex pressions;
使用了System.Runtime.InteropServices;

使用System.DirectoryServices.AccountManagement;

命名空间GestionnaireDesPlugins
{
    公共部分类登录:表单
    {
        市民登录(字符串帐户名,字符串帐户密码)
        {
            的InitializeComponent();
        }

        私人无效LoginOnClick(对象发件人,EventArgs的)
        {
            如果(的IsValid())
            {
                的getUser();
                // 做你想做的
                ShowForm();
            }
        }

        私人无效的getUser()
        {
            尝试
            {
                LdapConnection连接=新LdapConnection(AD);
                的NetworkCredential的凭证=新的NetworkCredential(txtboxlogin.Text,txtboxpass.Text);
                connection.Credential =凭证;
                connection.Bind();
            }
            赶上(LdapException lexc)
            {
                字符串错误= lexc.ServerErrorMessage;
                的MessageBox.show(错误的帐号或密码。);
            }
            赶上(例外EXC)
            {
                的MessageBox.show(exc.ToString());
            }
        }

        私人布尔的IsValid()
        {
            //检查,如果用户没有选择一个帐户
            !如果(string.IsNullOrEmpty(txtboxlogin.Text){返回false;}

            //检查,如果用户没有选择一个帐户
            如果{返回false(string.IsNullOrEmpty(txtboxpass.Text)!); }

            //检查,如果用户没有选择一个帐户
            如果{返回false(string.IsNullOrEmpty(comboBox1.Text)!); }

            //检查密码文本框为空
            如果{返回false(string.IsNullOrEmpty(textBox1.Text)!); }
            使用(PrincipalContext PC =新PrincipalContext(ContextType.Domain,YOURDOMAIN))
            {
                //验证凭据
                BOOL的isValid = pc.ValidateCredentials(txtboxlogin.Text,txtboxpass.Text);
            }
            返回的isValid;
        }

        私人无效ShowForm()
        {
            如果(txtboxlogin.Text ==WantedAdminUser)
            {
                使用(AdminForm窗口2 =新AdminForm())
                   form2.ShowDialog();
                显示();
            }
            其他
            {
                使用(用户窗体=新用户())
                    userform.ShowDialog();
                显示();
            }
        }
    }
}
 
怎样修改win10c盘权限

正如所说previously,因为你是新的C#,这里有一些建议:

在分裂您的code:方法必须短而按用途分开。它更容易为你和任何谁的作品在您的code 管理异常 在释放对象 请注意这个 txtboxlogin.Text ==WantedAdminUser这是危险的。

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.

Verify the textboxes (!string.Empty). Validate credentials. 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:

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 == "WantedAdminUser" it's dangerous.

阅读全文

相关推荐

最新文章