首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C# 使用 Domino LDAP 服務驗證使用者

     編輯:關於C語言

前置條件

  • Domino LDAP 服務設定為**不允許匿名存取**。
  • Internet 認證設為「較多名稱變換,較低安全性」,支援「名+姓」、簡稱、Email 登入。
  • 密碼為 Internet 密碼。
  • 預設 LDAP 無 uid (簡稱) 屬性,需手動新增。

原理

使用輸入的帳號密碼建立 DirectoryEntry 進行 LDAP 繫結查詢,**若不拋出例外則驗證通過**。

LDAPHelper 實現


using System.DirectoryServices;
using System.Configuration;

public class LDAPHelper
{
    public string DominoServer { get; set; }
    public string LdapServer { get; set; }
    public string UserName { get; set; }
    public string PassWord { get; set; }

    public LDAPHelper(string userName, string passWord)
    {
        LdapServer = "LDAP://192.176.0.46"; // 實際 IP
        DominoServer = "192.176.0.46";
        UserName = userName;
        PassWord = passWord;
    }

    public bool VerifyUser()
    {
        try
        {
            var searcher = PrepareSearcher();
            var result = searcher.FindOne(); // 繫結並查詢
            return result != null;
        }
        catch
        {
            return false;
        }
    }

    private DirectorySearcher PrepareSearcher()
    {
        var root = new DirectoryEntry(LdapServer, UserName, PassWord, AuthenticationTypes.None);
        var searcher = new DirectorySearcher(root);
        // 過濾條件:dominoPerson 且 (cn=帳號 OR uid=簡稱 OR mail=Email)
        searcher.Filter = $"(&(objectClass=dominoPerson)(|(cn={UserName})(uid={UserName})(mail={UserName})))";
        return searcher;
    }
}

使用範例


var ldap = new LDAPHelper("userName", "password");
if (ldap.VerifyUser())
    Console.WriteLine("驗證成功");
else
    Console.WriteLine("帳號或密碼錯誤");

注意事項

  • AuthenticationTypes.None 表示簡單繫結 (明文傳輸),生產環境建議使用 SSL/LDAPS (LDAPS://) 或 AuthenticationTypes.Secure。
  • 過濾條件涵蓋 cn (完整名稱)、uid (簡稱)、mail (Email) 三種登入方式。
  • 異常捕獲泛型 catch,實際專案建議細分 DirectoryServicesCOMException 等具體例外處理。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved