针对Active Directory C#-Visual Studio for Mac的登录验证
我正在尝试验证Active Directory中MVC Web应用程序(C#)的凭据。我正在Visual Studio for Mac上编写该应用程序。在寻找答案的过程中,我注意到了很多未实现的异常和其他奇怪的情况。 下面是我尝试过的事情和失败的事情的(不全面的)列表 首先,该代码:针对Active Directory C#-Visual Studio for Mac的登录验证,c#,asp.net-mvc,macos,visual-studio-mac,C#,Asp.net Mvc,Macos,Visual Studio Mac,我正在尝试验证Active Directory中MVC Web应用程序(C#)的凭据。我正在Visual Studio for Mac上编写该应用程序。在寻找答案的过程中,我注意到了很多未实现的异常和其他奇怪的情况。 下面是我尝试过的事情和失败的事情的(不全面的)列表 首先,该代码: string domainAndUsername = domain + @"\" + username; DirectoryEntry entry = new DirectoryEntry(_path, dom
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;
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;
行object obj=entry.NativeObject
抛出一个NotImplementedException
我尝试注释掉该行(因为obj
从未在其他地方使用过),但没有用。我也尝试过类似代码的其他变体
我尝试的另一条路线是以下代码:
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;
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)
因为这是一个Web Api,我使用的是一个控制器。这需要Web.config中的一些代码。它也抛出一个NotImplementedException
那么,这三种常用方法是否都依赖于VS for Mac中尚未实现的相同底层功能?还是我遗漏了什么?此外,如果有人能提供任何解决办法,我们将不胜感激
谢谢 首先,您需要引用Microsoft.DirectoryServices.dll
其次,您需要使用具有授权访问权限的域帐户运行此查询。在服务器(iis)或其他服务器中发布时要小心,因为服务运行此代码,并且必须对其具有权限
对不起,我的英语:多亏了我的努力,我终于学好了。问题的一部分是配置问题,不幸的是,我不能透露(机密性和诸如此类),但这里是最后的代码:
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
是。第三,参数$“{subDomain}\\{username}”
是用户名在AD中的显示方式。在我的特殊情况下,域的形式为“corporation.mycompany.com”,在AD中用户名显示为corporation\username
。因此,在本例中,string subDomain=“corporation”
在此阶段,您只能通过Novell Ldap库手动实现这一点。Microsoft正在为.NET Core开发跨平台的.DirectoryServices,但这是否会将其覆盖范围扩展到Mono尚不清楚。此问题/答案可能会有所帮助:谢谢大家的评论。我正在试用Novell.Directory.Ldap。我得到了<代码>无效的凭据错误,我认为这是一个正确的步骤。它被包含了,否则错误将是一个未定义的类的界线:根据异常判断,类被定义但未实现。System.DirectoryServices.AccountManagement不作为Visual Studio for Mac()的库存在,因此PrincipalContext未定义(我也尝试过,很抱歉我忘了在问题中提到它)。好的,那么您必须使用这个类