Search code examples
javagroovyactive-directoryldapupn

LDAP Searching a user in Active directory with UPN


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


Solution

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