2017-06-21 51 views
0

我想驗證Active Directory中的MVC Web應用程序(C#)的憑據我正在寫在Visual Studio的Mac上。在尋找答案時,我注意到了很多NotImplementedExceptions和其他奇怪的事件。 這裏是我嘗試過的東西和失敗的東西(非全面的)列表。登錄驗證對Active Directory C# - Visual Studio的Mac

首先,此代碼:

string domainAndUsername = domain + @"\" + username; 
DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, pwd); 

try 
{ 
    //Bind to the native AdsObject to force authentication. 
    // This line throws a not implemented exception 
    object obj = entry.NativeObject; 

    DirectorySearcher search = new DirectorySearcher(entry) 
    { 
      Filter = "(SAMAccountName=" + username + ")" 
    }; 
    search.PropertiesToLoad.Add("cn"); 

    Console.WriteLine(search.ToString()); 

    SearchResult result = search.FindOne(); 

    //Debug.WriteLine(result.ToString()); 

    if (null == result) 
    { 
     return false; 
    } 

    //Update the new path to the user in the directory. 
    _path = result.Path; 
    _filterAttribute = (string)result.Properties["cn"][0]; 
} 
catch (Exception ex) 
{ 
    throw new Exception("Error authenticating user. " + ex.Message); 
} 
     return true; 

object obj = entry.NativeObject拋出一個NotImplementedException我試着註釋掉行(因爲obj從未在其他地方使用),但無濟於事。我也嘗試過非常類似代碼的其他變體。

我嘗試另一條路線是這樣的代碼:

var creds = new NetworkCredential(username, password); 
var srvid = new LdapDirectoryIdentifier(adPath); 
// This line throws a NotImplementedException 
var conn = new LdapConnection(srvid, creds); 

try 
{ 
    conn.Bind(); 
} 
catch (Exception) 
{ 
    return false; 
} 

conn.Dispose(); 

return true; 

與其他同樣想法的變化。該生產線var conn = new LdapConnection(srvid, creds);拋出一個NotImplementedException

最後,我去了一個更簡單的途徑和所用

Membership.ValidateUser(model.username, model.password) 

由於這是一個網絡API,我使用的控制器。這需要Web.config available here中的一些代碼。它也會拋出一個NotImplementedException

那麼這三種常用方法都依賴於尚未在VS for Mac中實現的相同底層函數嗎?還是有我失蹤的東西?此外,如果有人可以提供任何解決方法,它將非常感激。

謝謝!

+0

您只能在此階段通過Novell Ldap庫手動實現該功能。微軟正在爲跨平臺'System.DirectoryServices'工作。NET Core,但是否會將其覆蓋範圍擴展到Mono還不得而知。 –

+0

這個問題可能有幫助: https://stackoverflow.com/questions/37330705/working-with-directoryservices-in-asp-net-core – bnem

+0

感謝您的評論傢伙。我正在嘗試Novell.Directory.Ldap。我得到了'無效的證書'錯誤,我認爲這是一個正確的方向。 – rtherman

回答

0

所有你需要引用Microsoft.DirectoryServices.dll

System.DirectoryServices

二:首先,您需要使用與授予訪問權限的域帳戶運行此查詢。在服務器(iis)或其他服務器上發佈時要小心,因爲該服務運行此代碼,並且必須獲得該代碼的許可。

check this

對不起,我的英語:d

+0

它被包含在內,否則錯誤將會沿着未定義的類的方向發生:由Exception判斷,該類已定義但未實現。 System.DirectoryServices.AccountManagement不存在作爲Visual Studio for Mac([引用​​Mono的代碼庫](https://github.com/mono/mono/tree/master/mcs/class))的庫,因此PrincipalContext未定義(我曾嘗試過,抱歉,我忘了在提問中提到它)。 – rtherman

+0

好的,那麼你必須使用這個類https://github.com/mono/mono/blob/master/mcs/class/Mono.Directory.LDAP/Mono.Directory.LDAP/LDAP.cs –

0

感謝的Javier Jimenez MatillabnemLexi Li的那種努力,我得到它的工作。部分的問題是一個配置的問題,可惜我不能透露(保密和諸如此類的東西),但這裏的最終代碼:

using Novell.Directory.Ldap; 
try 
{ 
    var conn = new LdapConnection(); 
    conn.Connect(domain, 389); 
    conn.Bind(LdapConnection.Ldap_V3, $"{subDomain}\\{username}", password); 
    return true; 
} 
catch (LdapException ex) 
{ 
    Console.WriteLine(ex.Message); 
    return false; 
} 

有幾件事情要注意。首先,LdapConnection實際上是Novell.Directory.Ldap.LdapConnection。其次,硬編碼389是端口LDAP likes to communicate over。第三,參數$"{subDomain}\\{username}"是用戶名出現在AD中的方式。對於我的具體情況,該域的格式爲「corporation.mycompany.com」,在AD中,用戶名顯示爲corporation\username。因此,在此示例中,string subDomain = "corporation"