2016-11-02 1123 views
2

我想在.NET Core中執行ActiveDirectory/LDAP-Authentication。
但是,在.NET Core(NetStandard 1.6)中沒有System.DirectoryServices。
因此,我使用Novell.Directory.Ldap,它工作正常,只要您知道rootDSE即可。如何在.NET Core中獲取defaultNamingContext?

通常情況下,我會得到的ActiveDirectory RootDSE的-defaultNamingContext是這樣的:

public static string GetRootDSE() 
{ 
    DirectoryEntry rootDSE = new DirectoryEntry("LDAP://rootDSE"); 
    string defaultNamingContext = rootDSE.Properties["defaultNamingContext"].Value; 
    return defaultNamingContext: 
} 

然而,因爲沒有的System.DirectoryServices,我不能這樣做。
Novell.Directory.Ldap似乎沒有相應的功能,或者至少我找不到它。

現在例如在VBScript中,你可以得到defaultNamingContext這樣的:

Set objRootDSE = GetObject("LDAP://rootDSE") 
strADsPath = "LDAP://" & objRootDSE.Get("defaultNamingContext") 
Set objDomain = GetObject(strADsPath) 

於是,我就用在C#中相同的COM-對象(因爲是.NET的核心沒有VB.NET,我無法調用VB.NET運行時)。
這是我設法弄到:

[System.Runtime.InteropServices.DllImport("Activeds", ExactSpelling = true, 
EntryPoint = "ADsGetObject", 
CharSet = System.Runtime.InteropServices.CharSet.Unicode)] 
public static extern int ADsGetObject(string path 
    , [System.Runtime.InteropServices.In, System.Runtime.InteropServices.Out] ref System.Guid iid 
    , [System.Runtime.InteropServices.Out, System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Interface)] out object ppObject 
); 


public static void Test() 
{ 
    dynamic ppObject; 

    // System.Guid IID_IDirectorySearch = new System.Guid("{ 0x109BA8EC, 0x92F0, 0x11D0, { 0xA7, 0x90, 0x00, 0xC0, 0x4F, 0xD8, 0xD5, 0xA8 } }"); 
    // System.Guid IID_IADsContainer = new System.Guid("{ 0x001677D0, 0xFD16, 0x11CE, { 0xAB, 0xC4, 0x02, 0x60, 0x8C, 0x9E, 0x75, 0x53 } }"); 
    System.Guid IID_IADs = new System.Guid("{ 0xFD8256D0, 0xFD15, 0x11CE, { 0xAB, 0xC4, 0x02, 0x60, 0x8C, 0x9E, 0x75, 0x53 } }"); 
    ADsGetObject("LDAP://rootDSE", ref IID_IADs, out ppObject); 
    System.Console.WriteLine(ppObject); 

    object objDSE; 
    // System.IntPtr objDSE; 
    // https://msdn.microsoft.com/en-us/library/aa705950(v=vs.85).aspx 
    // https://msdn.microsoft.com/en-us/library/aa746347(v=vs.85).aspx 
    // HRESULT Get([in] BSTR bstrName, [out] VARIANT *pvProp); 
    // ppObject.Get("defaultNamingContext", out objDSE); 

    System.IntPtr bstr = System.Runtime.InteropServices. 
     Marshal.StringToBSTR("defaultNamingContext"); 
    ppObject.Get(bstr, out objDSE); 
    System.Console.WriteLine(objDSE); 
} 

我得到的COM-對象(「ppObject」),似乎直到我打電話找上ppObject做工精細。
我在那裏得到"Not Implemented-Exception: Method or process is not implemented."
我在做什麼錯?另外,如果有人有更好的方式在.NET Core中獲得RootDSE,那麼我就會全神貫注。

PS:
我知道這隻適用於Windows - 我只是將它設置爲在Linux上手動配置。

回答

4

啊,沒關係,我得到了答案:

正如你可以看到here,你可以運行

nslookup -type=srv _ldap._tcp.DOMAINNAME 

,以獲得期望的結果。
所以,當你運行

nslookup -type=srv _ldap._tcp.YourDomain.local 

,這意味着你查詢默認DNS的執行機器的域的LDAP服務器的記錄,並使用該結果。

您可以使用任何支持.NET Core的DNS庫(如ArSoft)來完成此操作。

Dig

public static void Test4() 
{ 
    System.Net.NetworkInformation.IPGlobalProperties ipgp = 
     System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties(); 

    // IDnsResolver resolver = new RecursiveDnsResolver(); // Warning: Doesn't work 
    IDnsResolver resolver = new DnsStubResolver(); 

    List<SrvRecord> srvRecords = resolver.Resolve<SrvRecord>("_ldap._tcp." + ipgp.DomainName, RecordType.Srv); 

    foreach (SrvRecord thisRecord in srvRecords) 
    { 
     // System.Console.WriteLine(thisRecord.Name); 
     System.Console.WriteLine(thisRecord.Target); 
     System.Console.WriteLine(thisRecord.Port); 

     // Note: OR LDAPS:// - but Novell doesn't want these parts anyway 
     string url = "LDAP://" + thisRecord.Target + ":" + thisRecord.Port; 
     System.Console.WriteLine(url); 
    } // Next thisRecord 

}