Ejemplo n.º 1
0
        private Dictionary <string, MachineIdentitifier> ConfigureIdentities()
        {
            Dictionary <string, MachineIdentitifier> identifiersDict = new Dictionary <string, MachineIdentitifier>(StringComparer.OrdinalIgnoreCase)
            {
                {
                    "MachineName",
                    new MachineIdentitifier(lazyMachineStore, "MachineName", MachineIdentitifier.IdType.Secondary, () => MachineName)
                },
                {
                    "DNSDomain",
                    new MachineIdentitifier(lazyMachineStore, "DNSDomain", MachineIdentitifier.IdType.Secondary, () => DNSDomain)
                },
                {
                    "SelectedMACAddress",
                    new MachineIdentitifier(lazyMachineStore, "SelectedMACAddress", MachineIdentitifier.IdType.Secondary, () => SelectedMACAddress)
                },
                {
                    "BiosUUID",
                    new MachineIdentitifier(lazyMachineStore, "BiosUUID", MachineIdentitifier.IdType.PrimaryImmutableId, () => FormatPropertyValue(BiosUUID))
                },
                {
                    "BiosSerialNumber",
                    new MachineIdentitifier(lazyMachineStore, "BiosSerialNumber", MachineIdentitifier.IdType.PrimaryImmutableId, () => BiosSerialNumber)
                },
                {
                    "ConfigVersion",
                    new MachineIdentitifier(lazyMachineStore, "ConfigVersion", MachineIdentitifier.IdType.Informational, () => ConfigVersion, (string persistedConfigVersion) => string.CompareOrdinal(persistedConfigVersion, ConfigVersion) >= 0)
                },
                {
                    "PersistedSelectedMACAddress",
                    new MachinePrimaryIdentitifier(lazyMachineStore, "PersistedSelectedMACAddress", () => SelectedMACAddress, (string persistedSelectedMACAddress) => !InvalidateOnPrimaryIdChange, MACAddressStillExists)
                }
            };

            identifiersDict.Add("PersistedSelectedInterface", new MachineIdentitifier(lazyMachineStore, "PersistedSelectedInterface", MachineIdentitifier.IdType.Informational, () => SelectedNetworkInterfaceDescription, (string persistedInterface) => identifiersDict["PersistedSelectedMACAddress"].IsLastPersistedValueValid || object.Equals(persistedInterface, SelectedNetworkInterfaceDescription), null, NetworkInterfaceStillExists));
            identifiersDict.Add("HardwareId", new MachineComboIdentitifier(lazyMachineStore, "HardwareId", () => GetHardwareIdWithInvalidation(), () => HardwareIdValues.Select((string idName) => identifiersDict[idName])));
            return(identifiersDict);
        }
Ejemplo n.º 2
0
        private string GetHardwareIdWithInvalidation()
        {
            string text = HardwareIdNotObtained;

            try
            {
                text = MachineIdentifiers["HardwareId"].LastPersistedValue;
                if (!PersistedIdWasInvalidated && string.IsNullOrWhiteSpace(text))
                {
                    PersistedIdWasInvalidated     = true;
                    PersistedIdInvalidationReason = HardwareIdNotPeristed;
                }
                if (!PersistedIdWasInvalidated)
                {
                    foreach (MachineIdentitifier item2 in from name in HardwareIdValues
                             select MachineIdentifiers[name] into id
                             where id.Type == MachineIdentitifier.IdType.PrimaryImmutableId
                             select id)
                    {
                        if (!PersistedIdWasInvalidated && !item2.IsLastPersistedValueValid)
                        {
                            PersistedIdWasInvalidated     = true;
                            PersistedIdInvalidationReason = string.Format(string.IsNullOrWhiteSpace(item2.LastPersistedValue) ? "Persisted identity '{0}' was not found." : "{0} change requires invalidation of persisted ID.", item2.Name);
                        }
                    }
                }
                MachineIdentitifier machineIdentitifier = MachineIdentifiers["ConfigVersion"];
                if (!PersistedIdWasInvalidated && string.Compare(machineIdentitifier.LastPersistedValue, MinValidConfigVersion, StringComparison.OrdinalIgnoreCase) < 0)
                {
                    PersistedIdWasInvalidated     = true;
                    PersistedIdInvalidationReason = $"Persisted config version '{machineIdentitifier.LastPersistedValue}' is less than MinValidConfigVersion '{MinValidConfigVersion}'";
                }
                if (!PersistedIdWasInvalidated && InvalidateOnPrimaryIdChange)
                {
                    foreach (MachineIdentitifier item3 in from name in HardwareIdValues
                             select MachineIdentifiers[name] into id
                             where id.Type == MachineIdentitifier.IdType.PrimaryId && !PersistedIdWasInvalidated && !id.IsLastPersistedValueValid
                             select id)
                    {
                        PersistedIdWasInvalidated     = true;
                        PersistedIdInvalidationReason = string.Format(string.IsNullOrWhiteSpace(item3.LastPersistedValue) ? "Persisted identity '{0}' was not found." : "PrimaryId '{0}' value no longer found on machine.", item3.Name);
                    }
                }
                if (PersistedIdWasInvalidated)
                {
                    text = JoinIdentifiers(HardwareIdValues.Select((string idName) => MachineIdentifiers[idName]), (MachineIdentitifier id) => id.CurrentSystemValue);
                    lazyMachineStore.Value.SetProperty(StoredPropertyNamesToInts["HardwareIdDate"], DateTime.UtcNow.ToString("o"));
                    lazyMachineStore.Value.SetProperty(StoredPropertyNamesToInts["ConfigVersion"], ConfigVersion);
                    string[] hardwareIdValues = HardwareIdValues;
                    foreach (string key in hardwareIdValues)
                    {
                        MachineIdentifiers[key].StoreValidValue();
                    }
                }
                lazyMachineStore.Value.Persist();
                return(text);
            }
            catch (Exception item)
            {
                ExceptionsEncounteredObtainingHardwareId.Add(item);
                return(text);
            }
        }