Active Directory
A set of directory-based technologies included in Windows Server.
6,419 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
string searchFilter = "(" + obj.MatchingCriteriaAttributeName + " = " + MatchingCriteriaAttributeValue + ")";
which set as (userPrincipalName=a@domainname)
passed in
public bool UserExists(string username, string password, string searchRootpath, string searchFilter,string objectGuid,out string dnName)
{
dnName = null;
string sAccount = username.Replace(@"NOA\", "");
// Get root of directory entry given credentials and search root
using (DirectoryEntry rootDomaintoStart = searchRootpath == null ? new DirectoryEntry(GetCurrentDomainPath()) : new DirectoryEntry(searchRootpath, username, password))
{
// Create new instance of directory searcher with directory entry root
DirectorySearcher search = new DirectorySearcher(rootDomaintoStart);
// search.Filter = "(SAMAccountName=" + sAccount + ")";
//if (!string.IsNullOrEmpty(objectGuid))
search.Filter = searchFilter;
//else
SearchResult result;
// global.MessageBox.Show(deSearch.Filter.ToString());
try
{
result = search.FindOne();
}
catch (Exception)
{
throw;
}
if (null == result)
{
dnName = null;
return false;
}
Hello there,
Found this script online which is used to set the directory search filter for UPN in Active Directory.
public String findUserByUPN( LdapContext ctx, String username )
{
// Domain name should be in DC=your,DC=domain,DC=com format
String domain = "DC=demo,DC=com";
String filter = "(userPrincipalName=" + username + ")" ;
NamingEnumeration<SearchResult> results = ctx.search( domain, filter, null );
while ( results.hasMore() )
{
SearchResult result = results.next();
// If you get a result here, the user was found
return result.getNameInNamespace();
}
return null;
}
Hope this resolves your Query !!
--If the reply is helpful, please Upvote and Accept it as an answer–