public static bool SelectAccount(System.Windows.Forms.IWin32Window parent, string targetComputerName, ref string acctName, out bool isGroup, out bool isService, out string sid)
 {
     Locations l = Locations.EnterpriseDomain | Locations.ExternalDomain | Locations.GlobalCatalog | Locations.JoinedDomain | Locations.LocalComputer;
     DirectoryObjectPickerDialog dlg = new DirectoryObjectPickerDialog() { TargetComputer = targetComputerName, MultiSelect = false, SkipDomainControllerCheck = true, AllowedLocations = l, DefaultLocations = l };
     dlg.AllowedObjectTypes = ObjectTypes.Users; // | ObjectTypes.WellKnownPrincipals | ObjectTypes.Computers;
     if (NativeMethods.AccountUtils.CurrentUserIsAdmin(targetComputerName)) dlg.AllowedObjectTypes |= ObjectTypes.BuiltInGroups | ObjectTypes.Groups;
     dlg.DefaultObjectTypes = dlg.AllowedObjectTypes;
     dlg.AttributesToFetch.Add("objectSid");
     if (dlg.ShowDialog(parent) == System.Windows.Forms.DialogResult.OK)
     {
         if (dlg.SelectedObject != null)
         {
             try
             {
                 if (!String.IsNullOrEmpty(dlg.SelectedObject.Upn))
                     acctName = NameTranslator.TranslateUpnToDownLevel(dlg.SelectedObject.Upn);
                 else
                     acctName = dlg.SelectedObject.Name;
             }
             catch
             {
                 acctName = dlg.SelectedObject.Name;
             }
             sid = AttrToString(dlg.SelectedObject.FetchedAttributes[0]);
             isGroup = dlg.SelectedObject.SchemaClassName.Equals("Group", StringComparison.OrdinalIgnoreCase);
             isService = NativeMethods.AccountUtils.UserIsServiceAccount(acctName);
             return true;
         }
     }
     isGroup = isService = false;
     sid = null;
     return false;
 }
예제 #2
0
        private void BrowseBTN_Click(object sender, EventArgs e)
        {            
            try
            {
                DirectoryObjectPickerDialog picker = new DirectoryObjectPickerDialog();

                picker.AllowedObjectTypes = CubicOrange.Windows.Forms.ActiveDirectory.ObjectTypes.Computers | CubicOrange.Windows.Forms.ActiveDirectory.ObjectTypes.BuiltInGroups | CubicOrange.Windows.Forms.ActiveDirectory.ObjectTypes.Groups | CubicOrange.Windows.Forms.ActiveDirectory.ObjectTypes.Users | CubicOrange.Windows.Forms.ActiveDirectory.ObjectTypes.WellKnownPrincipals;
                picker.DefaultObjectTypes = picker.AllowedObjectTypes;
                picker.AllowedLocations = CubicOrange.Windows.Forms.ActiveDirectory.Locations.All;
                picker.DefaultLocations = CubicOrange.Windows.Forms.ActiveDirectory.Locations.All;
                picker.MultiSelect = false;
                picker.TargetComputer = null;
                
                if (picker.ShowDialog(this) != DialogResult.OK)
                {
                    return;
                } 
                
                DirectoryObject[] results = picker.SelectedObjects;

                if (results == null || results.Length != 1)
                {
                    return;
                }

                if (!String.IsNullOrEmpty(results[0].Path))
                {
                    string path = results[0].Path;                    
                    string[] fields = path.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

                    string domain  = fields[fields.Length-2];
                    string account = fields[fields.Length-1];

                    if (String.Compare(domain, System.Net.Dns.GetHostName(), StringComparison.InvariantCultureIgnoreCase) == 0)
                    {
                        m_identity = new NTAccount(account);
                    }
                    else
                    {
                        m_identity = new NTAccount(domain, account);
                    }
                }
                else
                {
                    m_identity = new NTAccount(results[0].Name);
                }

                if (m_identity != null)
                {
                    IdentityNameTB.Text = m_identity.ToString();
                }
            }
            catch (Exception exception)
            {
                GuiUtils.HandleException(this.Text, System.Reflection.MethodBase.GetCurrentMethod(), exception);
            }
        }
        /// <summary>
        /// ADs the object picker show dialog.
        /// </summary>
        /// <param name="handle">The owner handle.</param>
        /// <param name="showLocalUsersAndGroups">if set to <c>true</c> [show local users and groups].</param>
        /// <param name="showOnlyUsers">if set to <c>true</c> [show only users].</param>
        /// <param name="multipleSelection">if set to <c>true</c> [multiple selection].</param>
        /// <returns></returns>
        public static ADObject[] ADObjectPickerShowDialog(IWin32Window owner, bool showLocalUsersAndGroups, bool showOnlyUsers, bool multipleSelection)
        {
            #region OLD CODE
            //try
            //{
            //    // Initialize 1st search scope

            //    uint flType = 0;

            //    flType = flType |
            //        DSOP_SCOPE_TYPE_FLAGS.DSOP_SCOPE_TYPE_UPLEVEL_JOINED_DOMAIN |
            //        DSOP_SCOPE_TYPE_FLAGS.DSOP_SCOPE_TYPE_DOWNLEVEL_JOINED_DOMAIN |
            //        DSOP_SCOPE_TYPE_FLAGS.DSOP_SCOPE_TYPE_ENTERPRISE_DOMAIN |
            //        DSOP_SCOPE_TYPE_FLAGS.DSOP_SCOPE_TYPE_GLOBAL_CATALOG |
            //        DSOP_SCOPE_TYPE_FLAGS.DSOP_SCOPE_TYPE_EXTERNAL_DOWNLEVEL_DOMAIN |
            //        DSOP_SCOPE_TYPE_FLAGS.DSOP_SCOPE_TYPE_EXTERNAL_UPLEVEL_DOMAIN |
            //        DSOP_SCOPE_TYPE_FLAGS.DSOP_SCOPE_TYPE_USER_ENTERED_DOWNLEVEL_SCOPE |
            //        DSOP_SCOPE_TYPE_FLAGS.DSOP_SCOPE_TYPE_USER_ENTERED_UPLEVEL_SCOPE;
            //    //DSOP_SCOPE_TYPE_FLAGS.DSOP_SCOPE_TYPE_WORKGROUP;

            //    if (showLocalUsersAndGroups)
            //        flType = flType | DSOP_SCOPE_TYPE_FLAGS.DSOP_SCOPE_TYPE_TARGET_COMPUTER;

            //    uint flScope =
            //        DSOP_SCOPE_INIT_INFO_FLAGS.DSOP_SCOPE_FLAG_WANT_PROVIDER_LDAP |
            //        DSOP_SCOPE_INIT_INFO_FLAGS.DSOP_SCOPE_FLAG_DEFAULT_FILTER_USERS |
            //        DSOP_SCOPE_INIT_INFO_FLAGS.DSOP_SCOPE_FLAG_WANT_PROVIDER_WINNT |
            //        DSOP_SCOPE_INIT_INFO_FLAGS.DSOP_SCOPE_FLAG_STARTING_SCOPE |
            //        DSOP_SCOPE_INIT_INFO_FLAGS.DSOP_SCOPE_FLAG_WANT_DOWNLEVEL_BUILTIN_PATH; // Starting !?;

            //    if (!showOnlyUsers)
            //        flScope = flScope | DSOP_SCOPE_INIT_INFO_FLAGS.DSOP_SCOPE_FLAG_DEFAULT_FILTER_GROUPS;

            //    uint flBothModes =
            //        DSOP_FILTER_FLAGS_FLAGS.DSOP_FILTER_INCLUDE_ADVANCED_VIEW |
            //        DSOP_FILTER_FLAGS_FLAGS.DSOP_FILTER_USERS;

            //    if (!showOnlyUsers)
            //        flBothModes = flBothModes |
            //        DSOP_FILTER_FLAGS_FLAGS.DSOP_FILTER_BUILTIN_GROUPS |
            //        //DSOP_FILTER_FLAGS_FLAGS.DSOP_FILTER_DOMAIN_LOCAL_GROUPS_DL |
            //        DSOP_FILTER_FLAGS_FLAGS.DSOP_FILTER_DOMAIN_LOCAL_GROUPS_SE |
            //        //DSOP_FILTER_FLAGS_FLAGS.DSOP_FILTER_GLOBAL_GROUPS_DL |
            //        DSOP_FILTER_FLAGS_FLAGS.DSOP_FILTER_GLOBAL_GROUPS_SE |
            //        //DSOP_FILTER_FLAGS_FLAGS.DSOP_FILTER_UNIVERSAL_GROUPS_DL |
            //        DSOP_FILTER_FLAGS_FLAGS.DSOP_FILTER_UNIVERSAL_GROUPS_SE |
            //        DSOP_FILTER_FLAGS_FLAGS.DSOP_FILTER_WELL_KNOWN_PRINCIPALS;

            //    uint flDownlevel =
            //        //DSOP_DOWNLEVEL_FLAGS.DSOP_DOWNLEVEL_FILTER_ANONYMOUS |
            //        //DSOP_DOWNLEVEL_FLAGS.DSOP_DOWNLEVEL_FILTER_AUTHENTICATED_USER |
            //        //DSOP_DOWNLEVEL_FLAGS.DSOP_DOWNLEVEL_FILTER_BATCH |
            //        //DSOP_DOWNLEVEL_FLAGS.DSOP_DOWNLEVEL_FILTER_CREATOR_GROUP |
            //        //DSOP_DOWNLEVEL_FLAGS.DSOP_DOWNLEVEL_FILTER_CREATOR_OWNER |
            //        //DSOP_DOWNLEVEL_FLAGS.DSOP_DOWNLEVEL_FILTER_DIALUP |
            //        //DSOP_DOWNLEVEL_FLAGS.DSOP_DOWNLEVEL_FILTER_INTERACTIVE |
            //        //DSOP_DOWNLEVEL_FLAGS.DSOP_DOWNLEVEL_FILTER_LOCAL_SERVICE |
            //        //DSOP_DOWNLEVEL_FLAGS.DSOP_DOWNLEVEL_FILTER_NETWORK |
            //        //DSOP_DOWNLEVEL_FLAGS.DSOP_DOWNLEVEL_FILTER_NETWORK_SERVICE |
            //        //DSOP_DOWNLEVEL_FLAGS.DSOP_DOWNLEVEL_FILTER_REMOTE_LOGON |
            //        //DSOP_DOWNLEVEL_FLAGS.DSOP_DOWNLEVEL_FILTER_SERVICE |
            //        //DSOP_DOWNLEVEL_FLAGS.DSOP_DOWNLEVEL_FILTER_SYSTEM |
            //        //DSOP_DOWNLEVEL_FLAGS.DSOP_DOWNLEVEL_FILTER_TERMINAL_SERVER |
            //        DSOP_DOWNLEVEL_FLAGS.DSOP_DOWNLEVEL_FILTER_USERS;
            //    //DSOP_DOWNLEVEL_FLAGS.DSOP_DOWNLEVEL_FILTER_WORLD;

            //    if (!showOnlyUsers)
            //    {
            //        flDownlevel = flDownlevel
            //            | DSOP_DOWNLEVEL_FLAGS.DSOP_DOWNLEVEL_FILTER_ALL_WELLKNOWN_SIDS
            //            | DSOP_DOWNLEVEL_FLAGS.DSOP_DOWNLEVEL_FILTER_GLOBAL_GROUPS
            //            | DSOP_DOWNLEVEL_FLAGS.DSOP_DOWNLEVEL_FILTER_LOCAL_GROUPS;
            //    }

            //    ADObjectPickerClass cadObjectPicker = new ADObjectPickerClass();
            //    cadObjectPicker.InitInfo_OptionFlags = DSOP_INIT_INFO_FLAGS.DSOP_FLAG_SKIP_TARGET_COMPUTER_DC_CHECK;
            //    if (multipleSelection)
            //    {
            //        cadObjectPicker.InitInfo_OptionFlags = cadObjectPicker.InitInfo_OptionFlags
            //            | DSOP_INIT_INFO_FLAGS.DSOP_FLAG_MULTISELECT;
            //    }

            //    cadObjectPicker.ScopeTypeFlags = flType;
            //    cadObjectPicker.ScopeFlags = flScope;
            //    cadObjectPicker.UplevelFilterFlags_Both = flBothModes;
            //    cadObjectPicker.DownLevelFilterFlags = flDownlevel;
            //    cadObjectPicker.InvokeDialog(handle.ToInt32());
            //    ADObjectColl result = (ADObjectColl)cadObjectPicker.ADObjectsColl;
            //    ADObject[] results = new ADObject[result.Count];
            //    for (uint j = 1; j <= result.Count; j++)
            //    {
            //        try
            //        {
            //            int i = (int)j;
            //            ADObjectInfo info = (ADObjectInfo)result.Item(i);
            //            results[j - 1] = new ADObject();
            //            results[j - 1].ADSPath = info.ADPath;
            //            results[j - 1].ClassName = info.Class;
            //            results[j - 1].Name = info.Name;
            //            results[j - 1].UPN = info.UPN;
            //        }
            //        catch
            //        {
            //            continue;
            //        }
            //    }
            //    return results;
            //}
            //catch (System.ArgumentException)
            //{
            //    return new ADObject[0];
            //}
            #endregion OLD CODE
            try
            {
                // Show dialog
                DirectoryObjectPickerDialog picker = new DirectoryObjectPickerDialog();
                ObjectTypes allowedTypes = ObjectTypes.None;

                if (!showOnlyUsers)
                    allowedTypes = ObjectTypes.BuiltInGroups | ObjectTypes.Groups | ObjectTypes.Users | ObjectTypes.WellKnownPrincipals;
                else
                    allowedTypes = ObjectTypes.Users;
                ObjectTypes defaultTypes = allowedTypes;
                picker.AllowedObjectTypes = allowedTypes;
                picker.DefaultObjectTypes = defaultTypes;
                Locations allowedLocations = Locations.None;
                Locations defaultLocations = Locations.None;
                if (showLocalUsersAndGroups)
                {
                    allowedLocations = Locations.All;
                    defaultLocations = Locations.GlobalCatalog | Locations.EnterpriseDomain | Locations.ExternalDomain | Locations.JoinedDomain;
                    if (Environment.MachineName.Equals(Environment.UserDomainName, StringComparison.CurrentCultureIgnoreCase))
                    {
                        //NOT JOINED TO A DOMAIN
                        defaultLocations |= Locations.LocalComputer;
                    }
                }
                else
                {
                    allowedLocations = Locations.GlobalCatalog | Locations.EnterpriseDomain | Locations.ExternalDomain | Locations.JoinedDomain;
                    defaultLocations = Locations.GlobalCatalog | Locations.EnterpriseDomain | Locations.ExternalDomain | Locations.JoinedDomain;
                }
                picker.AllowedLocations = allowedLocations;
                picker.DefaultLocations = defaultLocations;
                picker.MultiSelect = multipleSelection;
                picker.ShowAdvancedView = true;
                DialogResult dialogResult = picker.ShowDialog(owner);
                if (dialogResult == DialogResult.OK)
                {
                    if (picker.SelectedObjects == null)
                    {
                        return new ADObject[0];
                    }
                    ADObject[] results = new ADObject[picker.SelectedObjects.Length];
                    for (int j = 0; j < picker.SelectedObjects.Length; j++)
                    {
                        try
                        {
                            DirectoryObject info = (DirectoryObject)picker.SelectedObjects[j];
                            results[j] = new ADObject();
                            results[j].ADSPath = info.Path;
                            results[j].ClassName = info.SchemaClassName;
                            results[j].Name = info.Name;
                            results[j].UPN = info.Upn;
                        }
                        catch
                        {
                            continue;
                        }
                    }
                    return results;
                }
                else
                {
                    return new ADObject[0];
                }
            }
            catch
            {
                return new ADObject[0];
            }
        }