C# Asp.net中的Active Directory性能问题
我已使用active directory登录 我有一些性能问题 这是我的密码C# Asp.net中的Active Directory性能问题,c#,asp.net,angularjs,performance,active-directory,C#,Asp.net,Angularjs,Performance,Active Directory,我已使用active directory登录 我有一些性能问题 这是我的密码 public bool IsAuthenticated(String domain, String username, String pwd) { String domainAndUsername = domain + @"\" + username; DirectoryEntry entry = new DirectoryEntry("LDAP://
public bool IsAuthenticated(String domain, String username, String pwd)
{
String domainAndUsername = domain + @"\" + username;
DirectoryEntry entry = new DirectoryEntry("LDAP://" + _path, domainAndUsername, pwd);
try
{ //Bind to the native AdsObject to force authentication.
Object obj = entry.NativeObject;
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "(SAMAccountName=" + username + ")";
search.PropertiesToLoad.Add("cn");
SearchResult result = search.FindOne();
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;
DirectorySearcher search = new DirectorySearcher(entry);
当登录输入错误时,返回结果需要很长时间
所以我的问题是
- 每当我输入错误的登录详细信息时,为什么要花很长时间加载这些行?
- 每当我输入正确的登录详细信息时,为什么这些行需要很快加载?
但没有人对此负责。请告诉我解决方案:)这需要很长时间,因为它必须检查每个对象以与输入进行比较,因为它永远找不到正确的对象 但是,这是一种不必要的复杂方法,无法在active directory上对用户进行身份验证 这要容易得多:
public bool ValidateCredentials(string domain, string username, string password)
{
using (PrincipalContext context = new PrincipalContext(ContextType.Domain, domain))
{
return context.ValidateCredentials(username, password);
}
}
然后再更新值。我尝试过这个,但它总是返回false,所以我改变了它。缓慢的原因是
.Net
运行时和COM对象之间的上下文切换(NativeObject
)。通常情况就是这样。