Although I cannot find it anywhere in the MSDN documentation, retrieving the NativeObject forces authentication. It will throw an exception when the authentication failed.
Why AdsObject is being binded to a native object in asp.net?
-
01-07-2022 - |
Domanda
I was working with an asp.net web application. I used form authentication to validate users from active directory. Everything works fine. But, I got confused why I should bind AdsObject to a native object which is never being used. My codes goes like this
DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, pwd);
string[] uName;
try
{
//Bind to the native AdsObject to force authentication.
object obj = entry.NativeObject;
DirectorySearcher search = new DirectorySearcher(entry);
uName = domainAndUsername.Split('\\');
search.Filter = "(SAMAccountName=" + uName[1] + ")";
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);
}
I even commented the line and tested it. It worked fine too. I searched everywhere for an answer, never found one. Hope i'll get it here.. :)
Soluzione
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow