我想验证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中实现的相同底层函数吗?还是有我失踪的东西?此外,如果有人可以提供任何解决方法,它将非常感激。
谢谢!
您只能在此阶段通过Novell Ldap库手动实现该功能。微软正在为跨平台'System.DirectoryServices'工作。NET Core,但是否会将其覆盖范围扩展到Mono还不得而知。 –
这个问题可能有帮助: https://stackoverflow.com/questions/37330705/working-with-directoryservices-in-asp-net-core – bnem
感谢您的评论家伙。我正在尝试Novell.Directory.Ldap。我得到了'无效的证书'错误,我认为这是一个正确的方向。 – rtherman