簡體   English   中英

c#通過使用針對LDAP的Active Directory身份驗證使用登錄事件

[英]c# Log In click event using authentication against Active Directory through LDAP

我對c#和LDAP還是很陌生,我正在做這個項目,以便可以通過更多的實踐方法來了解它們。

我要創建的是一個具有登錄單擊事件的登錄表單,該事件將在用戶使用LDAP通過活動目錄輸入用戶名和密碼后對用戶名和密碼進行身份驗證。

我已經閱讀了.NET Framework 3.5中的《管理目錄安全性主體》,以便能夠更好地理解該主題,並且在此我也經歷了類似的主題,這個主題本身涉及驗證( C#-針對Active Directory驗證用戶名和密碼? )和一個用於驗證用戶名的用戶( 針對LDAP上的Active Directory使用c#

從第一個鏈接的主題中,我了解到以下代碼可以完成驗證用戶名和密碼的技巧:

  using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, "LDAP://example.string.com/OU=Users, Dc=example, Dc= string, DC=com"))
        {
            bool isValid = pc.ValidateCredentials(User, Password);
        }

因此,我將其合並到click事件中的方法如下:

using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, "LDAP://example.string.com/OU=Users, Dc=example, Dc= string, DC=com"))

            bool isValid = pc.ValidateCredentials(User, Password);

             if(isValid)
             {
                 Main m = new Main();
                 this.Close();
                 m.Show();
             }
             else
             {
                 MessageBox.Show("Invalid Username and/or Password","Error!");
                 textBox1.Clear();
                 textBox2.Clear();
                 textBox1.Focus();

             }

這給了我嵌入式語句的布爾錯誤。 我嘗試了從第二篇文章中讀到的另一種方法,即使用此代碼僅對用戶名進行身份驗證:

         PrincipalContext pc = new PrincipalContext(ContextType.Domain, "LDAP://example.com/OU=Computers,OU=Users,dc=example,dc=com");

        UserPrincipal user = UserPrincipal.FindByIdentity(pc, "username");


        bool userExists = (user != null);

但是我發現我無法使用此方法對密碼進行身份驗證,因為UserPrincipal.FindByPassword不存在。

我也嘗試過這種方式,但是再次。 密碼不存在:

 PrincipalContext pc = new PrincipalContext(ContextType.Domain,"LDAP://....");

        UserPrincipal qbeUser = new UserPrincipal(pc);
        qbeUser.EmployeeId = User;

        //.Password does not exist
        UserPrincipal qbePassword = new UserPrincipal(pc);
        qbePassword.Password = Password;

        // create your principal searcher passing in the QBE principal    
        PrincipalSearcher srchUser = new PrincipalSearcher(qbeUser);
         PrincipalSearcher srchPass = new PrincipalSearcher(qbePassword);
        // try to find that user and password
        UserPrincipal founduser = srchUser.FindOne() as UserPrincipal;
        UserPrincipal foundpass = srchPass.FindOne() as UserPrincipal;

        if (founduser != null)
        {
            if (foundpass != null)
            {
                Main m = new Main();
                this.Close();
                m.Show();
            }
            else
            {
                MessageBox.Show("Password Not Valid.");
                textBox2.Clear();
                textBox2.Focus();
            }
        }

        else
        {
            MessageBox.Show("Username Not Valid.");
            textBox1.Clear();
            textBox1.Focus();
        }

有人可以請我指導我如何正確地做到這一點。

先感謝您。

我已經做到了,但是沒有使用PrincipalContext。 相反,我發現許多人都在努力使用該對象。

我的實現是winforms表單,並且Submit按鈕調用執行以下代碼的4 las行的方法。

我針對這個宏偉的免費測試LDAP服務器進行了測試

var path = "LDAP://ldap.forumsys.com:389/dc=example,dc=com";
var user = $@"uid={username},dc=example,dc=com";
var pass = "password";

var directoryEntry = new DirectoryEntry(path, user, pass, AuthenticationTypes.None);

var searcher = new DirectorySearcher(directoryEntry);
searcher.PropertiesToLoad.Add("*");
var searchResult = searcher.FindOne();

我不完全了解所有這些行的作用。

重要提示

在路徑上,“ LDAP://”字符串應位於mayus塊上。

在用戶中,根據測試服務器,您使用“ cn = username-admin”來驗證管理員,請確保還將身份驗證類型也設置為ServerBind。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM