Exemple #1
0
        private bool ValidateObjectId(string objId)
        {
            bool isValid = false;

            if (!string.IsNullOrWhiteSpace(objId))
            {
                var objectCollection = ActiveDirectoryClient.GetObjectsByObjectIdsAsync(new[] { objId }, new string[] { }).GetAwaiter().GetResult();
                if (objectCollection.Any())
                {
                    isValid = true;
                }
            }
            return(isValid);
        }
        private bool ValidateObjectId(string objId)
        {
            if (string.IsNullOrWhiteSpace(objId))
            {
                return(false);
            }
// TODO: Remove IfDef
#if NETSTANDARD
            var objectCollection = ActiveDirectoryClient.GetObjectsByObjectId(new List <string> {
                objId
            });
#else
            var objectCollection = ActiveDirectoryClient.GetObjectsByObjectIdsAsync(new[] { objId }, new string[] { }).GetAwaiter().GetResult();
#endif
            return(objectCollection.Any());
        }
        protected Guid GetObjectId(Guid objectId, string upn, string spn)
        {
            Guid   objId        = Guid.Empty;
            string objectFilter = objectId.ToString();

            if (!string.IsNullOrWhiteSpace(upn))
            {
                objectFilter = upn;
                var user = ActiveDirectoryClient.Users.Where(u =>
                                                             u.UserPrincipalName.Equals(upn) || u.Mail.Equals(upn) || u.OtherMails.Any(m => m.Equals(upn))).
                           ExecuteAsync().GetAwaiter().GetResult().CurrentPage.FirstOrDefault();
                if (user != null)
                {
                    objId = Guid.Parse(user.ObjectId);
                }
            }
            else if (!string.IsNullOrWhiteSpace(spn))
            {
                objectFilter = spn;
                var servicePrincipal = ActiveDirectoryClient.ServicePrincipals.Where(s =>
                                                                                     s.ServicePrincipalNames.Any(n => n.Equals(spn)))
                                       .ExecuteAsync().GetAwaiter().GetResult().CurrentPage.FirstOrDefault();
                if (servicePrincipal != null)
                {
                    objId = Guid.Parse(servicePrincipal.ObjectId);
                }
            }
            else if (objectId != Guid.Empty)
            {
                var objectCollection = ActiveDirectoryClient.GetObjectsByObjectIdsAsync(new[] { objectId.ToString() }, new string[] { }).GetAwaiter().GetResult();
                if (objectCollection.Any())
                {
                    objId = objectId;
                }
            }

            if (objId != Guid.Empty)
            {
                return(objId);
            }

            throw new ArgumentException(string.Format(PSKeyVaultProperties.Resources.ADObjectNotFound, objectFilter,
                                                      (_dataServiceCredential != null) ? _dataServiceCredential.TenantId : string.Empty));
        }
        protected string GetObjectId(string objectId, string upn, string spn)
        {
            var objId        = string.Empty;
            var objectFilter = objectId ?? string.Empty;

            if (!string.IsNullOrWhiteSpace(upn))
            {
                objectFilter = upn;
                var user = ActiveDirectoryClient.Users.Where(FilterByUpn(upn)).ExecuteAsync().GetAwaiter().GetResult().CurrentPage.FirstOrDefault();
                if (user != null)
                {
                    objId = user.ObjectId;
                }
            }
            else if (!string.IsNullOrWhiteSpace(spn))
            {
                objectFilter = spn;
                var servicePrincipal = ActiveDirectoryClient.ServicePrincipals.Where(s =>
                                                                                     s.ServicePrincipalNames.Any(n => n.Equals(spn, StringComparison.OrdinalIgnoreCase)))
                                       .ExecuteAsync().GetAwaiter().GetResult().CurrentPage.FirstOrDefault();
                if (servicePrincipal != null)
                {
                    objId = servicePrincipal.ObjectId;
                }
            }
            else if (!string.IsNullOrWhiteSpace(objectId))
            {
                var objectCollection = ActiveDirectoryClient.GetObjectsByObjectIdsAsync(new[] { objectId }, new string[] { }).GetAwaiter().GetResult();
                if (objectCollection.Any())
                {
                    objId = objectId;
                }
            }

            if (!string.IsNullOrWhiteSpace(objId))
            {
                return(objId);
            }

            throw new ArgumentException(string.Format(PSKeyVaultProperties.Resources.ADObjectNotFound, objectFilter,
                                                      (_dataServiceCredential != null) ? _dataServiceCredential.TenantId : string.Empty));
        }
        public static string GetDisplayNameForADObject(Guid id, ActiveDirectoryClient adClient)
        {
            string displayName = "";
            string upnOrSpn    = "";

            if (adClient == null || id == Guid.Empty)
            {
                return(displayName);
            }

            try
            {
                var obj = adClient.GetObjectsByObjectIdsAsync(new[] { id.ToString() }, new string[] { }).GetAwaiter().GetResult().FirstOrDefault();
                if (obj != null)
                {
                    if (obj.ObjectType.Equals("user", StringComparison.InvariantCultureIgnoreCase))
                    {
                        var user = adClient.Users.GetByObjectId(id.ToString()).ExecuteAsync().GetAwaiter().GetResult();
                        displayName = user.DisplayName;
                        upnOrSpn    = user.UserPrincipalName;
                    }
                    else if (obj.ObjectType.Equals("serviceprincipal", StringComparison.InvariantCultureIgnoreCase))
                    {
                        var servicePrincipal = adClient.ServicePrincipals.GetByObjectId(id.ToString()).ExecuteAsync().GetAwaiter().GetResult();
                        displayName = servicePrincipal.AppDisplayName;
                        upnOrSpn    = servicePrincipal.ServicePrincipalNames.FirstOrDefault();
                    }
                }
            }
            catch
            {
                // Error occured. Don't get the friendly name
            }

            return(displayName + (!string.IsNullOrWhiteSpace(upnOrSpn) ? (" (" + upnOrSpn + ")") : ""));
        }
Exemple #6
0
        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 + ")") : ""));
        }
        public static string GetDisplayNameForADObject(Guid id, ActiveDirectoryClient adClient)
        {
            string displayName = "";
            string upnOrSpn = "";

            if (adClient == null || id == Guid.Empty)
                return displayName;

            try
            {
                var obj = adClient.GetObjectsByObjectIdsAsync(new[] { id.ToString() }, new string[] { }).GetAwaiter().GetResult().FirstOrDefault();
                if (obj != null)
                {
                    if (obj.ObjectType.Equals("user", StringComparison.InvariantCultureIgnoreCase))
                    {
                        var user = adClient.Users.GetByObjectId(id.ToString()).ExecuteAsync().GetAwaiter().GetResult();
                        displayName = user.DisplayName;
                        upnOrSpn = user.UserPrincipalName;
                    }
                    else if (obj.ObjectType.Equals("serviceprincipal", StringComparison.InvariantCultureIgnoreCase))
                    {
                        var servicePrincipal = adClient.ServicePrincipals.GetByObjectId(id.ToString()).ExecuteAsync().GetAwaiter().GetResult();
                        displayName = servicePrincipal.AppDisplayName;
                        upnOrSpn = servicePrincipal.ServicePrincipalNames.FirstOrDefault();
                    }
                }
            }
            catch
            {
                // Error occured. Don't get the friendly name
            }

            return displayName + (!string.IsNullOrWhiteSpace(upnOrSpn) ? (" (" + upnOrSpn + ")") : "");
        }