0

Need a quick help.

I am using LDAP for authentication with active directory,

I want to know how to fetch all UPN alias of a domain & domain from UPN alias,

Is their any way to get this.

Please help!

Charu Jain
  • 852
  • 1
  • 7
  • 18

1 Answers1

1

You can actually read these values from AD over LDAP from the Configuration tree. If you read the following object: CN=Partitions,CN=Configuration,DC=your,DC=domain,DC=com that will contain an attribute called uPNSuffixes.

This attribute contains only the extra suffixes not the default one (you will have to get that from the domain name itself I guess).

Rule is, if the uPNSuffixes attribute is not available, only the default UPN suffix is valid.

EDIT: a simple example would be this:

public List<String> getUpnSuffixes( LdapContext ctx, String domainName )
{
   // Domain name should be in DC=you,DC=domain,DC=com format
   String domConfig = "CN=Partitions,CN=Configuration," +domainName ;
   List<String> names = new ArrayList<String>();
   // Dirty hack to get the default suffix
   names.add( domainName.replaceAll( "DC=", "" ).replaceAll( "," , "." );
   // Read the configuration
   Attributes attrs = ctx.getAttributes( domConfig , new String[] { "uPNSuffixes" } );
   Attribute attr = attrs.get( "uPNSuffixes" );
   for ( int i=0; i<attr.size(); i++ )
   {
       names.add( attr.get(i) );
   }
   // Now you have all the suffixes in the "names" list. 
   return names;
}

Note that you will probably have to catch NamingException for the ctx.getAttributes() and attr.get() calls.

EDIT 2: if you want the reverse, search for the uPNSuffixes attribute value:

public String getDomainFromUpnSuffix( LdapContext ctx, String uPNSuffix )
{
   String filter = "(&(CN=Partitions)(uPNSuffixes=" + uPNSuffix + "))" ;
   // Find the configuration for this suffix
   NamingEnumeration<SearchResult> results = ctx.search( "", filter, null );
   while ( results.hasMore() )
   {
       SearchResult result = results.next();
       return result.getNameInNamespace();
   }
   return null;
}
mvreijn
  • 2,807
  • 28
  • 40
  • Hi, thanks for the awesome reply, can you also tell me how do i get domain name from UPN Alias? – Charu Jain Mar 06 '14 at 06:22
  • The default domain name (or real domain name) is not listed in the UPN suffixes list. It is sort of assumed that you know it ;-). That's why I determine it from the `domainName` parameter. – mvreijn Mar 06 '14 at 08:38
  • Can you explain more. Didn't get you exactly. – Charu Jain Mar 06 '14 at 08:44
  • I think we're talking about different things. I'll edit my post (may take a while) – mvreijn Mar 06 '14 at 09:07
  • Hi @mvreijn, need a help i have a UPN alias testserver.ad registered for domain demo.com, now do i fetch domain of testserver.ad from code base. – Charu Jain Mar 07 '14 at 05:51
  • @mveijn , can you please help me on this http://stackoverflow.com/questions/22285357/ldap-searching-a-user-in-active-directory-with-upn – Charu Jain Mar 09 '14 at 17:18
  • Hi mveijn ,EDIT 2, extracting Domain from UPN didn't seems to work. it's showing null in result. – Charu Jain Mar 10 '14 at 05:40
  • I think it's best to switch to your second question, as that is more to the point. – mvreijn Mar 10 '14 at 12:10
  • Hi mvreijn, can you please help me with Edit -2 ,i.e fetching the domain with UPN, in the above code when i pass ldapContext and zzserver.ad(UPN) as arguements, the method returns null :( . Stuck! in this really :( – Charu Jain Mar 12 '14 at 10:57
  • That's because the `ctx.search()` requires a search root, which is `""` in my example. That should be a domain context like `DC=domain,DC=com`. Which leads to my comment here: http://stackoverflow.com/questions/22285357/ldap-searching-a-user-in-active-directory-with-upn/22303991?noredirect=1#comment33913171_22303991 – mvreijn Mar 12 '14 at 14:19