2017-06-21 52 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"