public static (string, string) GetDetailsFromADObjectId(string objectId, ActiveDirectoryClient adClient) { var displayName = ""; var upnOrSpn = ""; var objectType = "Unknown"; if (adClient == null || string.IsNullOrWhiteSpace(objectId)) { return(displayName, objectType); } try { var obj = adClient.GetObjectsByObjectId(new List <string> { objectId }).FirstOrDefault(); if (obj != null) { if (obj.Type.Equals("user", StringComparison.InvariantCultureIgnoreCase)) { var user = adClient.FilterUsers(new ADObjectFilterOptions { Id = objectId }).FirstOrDefault(); displayName = user.DisplayName; upnOrSpn = user.UserPrincipalName; objectType = "User"; } else if (obj.Type.Equals("serviceprincipal", StringComparison.InvariantCultureIgnoreCase)) { var odataQuery = new Rest.Azure.OData.ODataQuery <Graph.RBAC.Version1_6.Models.ServicePrincipal>(s => s.ObjectId == objectId); var servicePrincipal = adClient.FilterServicePrincipals(odataQuery).FirstOrDefault(); displayName = servicePrincipal.DisplayName; upnOrSpn = servicePrincipal.ServicePrincipalNames.FirstOrDefault(); objectType = "Service Principal"; } else if (obj.Type.Equals("group", StringComparison.InvariantCultureIgnoreCase)) { var group = adClient.FilterGroups(new ADObjectFilterOptions { Id = objectId }).FirstOrDefault(); displayName = group.DisplayName; objectType = "Group"; } } } catch { // Error occurred. Don't get the friendly name } return( displayName + (!string.IsNullOrWhiteSpace(upnOrSpn) ? (" (" + upnOrSpn + ")") : ""), objectType ); }
public static string GetDisplayNameForADObject(string objectId, ActiveDirectoryClient adClient) { string displayName = ""; string upnOrSpn = ""; if (adClient == null || string.IsNullOrWhiteSpace(objectId)) { return(displayName); } try { #if NETSTANDARD var obj = adClient.GetObjectsByObjectId(new List <string> { objectId }).FirstOrDefault(); if (obj != null) { if (obj.Type.Equals("user", StringComparison.InvariantCultureIgnoreCase)) { var user = adClient.FilterUsers(new ADObjectFilterOptions { Id = objectId }).FirstOrDefault(); displayName = user.DisplayName; upnOrSpn = user.UserPrincipalName; } else if (obj.Type.Equals("serviceprincipal", StringComparison.InvariantCultureIgnoreCase)) { var servicePrincipal = adClient.FilterServicePrincipals(new ADObjectFilterOptions { Id = objectId }).FirstOrDefault(); displayName = servicePrincipal.DisplayName; upnOrSpn = servicePrincipal.ServicePrincipalNames.FirstOrDefault(); } else if (obj.Type.Equals("group", StringComparison.InvariantCultureIgnoreCase)) { var group = adClient.FilterGroups(new ADObjectFilterOptions { Id = objectId }).FirstOrDefault(); displayName = group.DisplayName; } } #else var obj = adClient.GetObjectsByObjectIdsAsync(new[] { objectId }, new string[] { }).GetAwaiter().GetResult().FirstOrDefault(); if (obj != null) { if (obj.ObjectType.Equals("user", StringComparison.InvariantCultureIgnoreCase)) { var user = adClient.Users.GetByObjectId(objectId).ExecuteAsync().GetAwaiter().GetResult(); displayName = user.DisplayName; upnOrSpn = user.UserPrincipalName; } else if (obj.ObjectType.Equals("serviceprincipal", StringComparison.InvariantCultureIgnoreCase)) { var servicePrincipal = adClient.ServicePrincipals.GetByObjectId(objectId).ExecuteAsync().GetAwaiter().GetResult(); displayName = servicePrincipal.AppDisplayName; upnOrSpn = servicePrincipal.ServicePrincipalNames.FirstOrDefault(); } else if (obj.ObjectType.Equals("group", StringComparison.InvariantCultureIgnoreCase)) { var group = adClient.Groups.GetByObjectId(objectId).ExecuteAsync().GetAwaiter().GetResult(); displayName = group.DisplayName; upnOrSpn = group.MailNickname; } } #endif } catch { // Error occured. Don't get the friendly name } return(displayName + (!string.IsNullOrWhiteSpace(upnOrSpn) ? (" (" + upnOrSpn + ")") : "")); }