LDAP Searching a user in Active directory with UPN

15,833

Solution 1

This is more an ordinary user search:

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;
}

Solution 2

Not sure what you are trying to accomplish but a filter like:

([email protected])

Will locate a user from the value of the userPrincipalName attribute. -jim

Share:
15,833
Charu Jain
Author by

Charu Jain

Updated on June 05, 2022

Comments

  • Charu Jain
    Charu Jain almost 2 years

    I am using LDAP Authentication, Need a help

    Suppose i have a user([email protected]), where zzservers.ad is a UPN Alias of demo.com domain , i already know of a way to search a user in active directory by domain.

    But Does anyone know about how to search a user in active directory by UPN Alias.

    Actually when user [email protected] login into the application, i want to know if user is present in AD, so as to proceed authentication further.

    Any help would be hugely appreciated.

    Thanks

  • Charu Jain
    Charu Jain about 10 years
    I don't have demo.com domain, i just have email [email protected] and i want to authenticate that, i dont have any idea of which domain the user will fall.
  • mvreijn
    mvreijn about 10 years
    Hi Charu, for a normal LDAP search you need a root context. So you have two options: either resolve the domain context (through DNS: stackoverflow.com/questions/749268/…) or use AD's phantom root search control: msdn2.microsoft.com/en-us/library/aa366988.aspx.
  • Charu Jain
    Charu Jain about 10 years
    thanks mvreijn these links really helps, my problem is solved now :)