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