Esempio n. 1
0
        /// <inheritdoc />
        public async Task <Outcome <ClientCredentialsResponse> > AcquireTokenAsync(
            CancellationToken cancellationToken,
            Credentials clientCredentials = null,
            MultiStringValue scope        = null)
        {
            try
            {
                var basicAuthCredentials = validateBasicAuthCredentials(clientCredentials ?? OnGetCredentials());
                using var client = new HttpClient();
                client.DefaultRequestHeaders.Authorization = basicAuthCredentials.ToAuthenticationHeaderValue();
                var content = new FormUrlEncodedContent(new Dictionary <string, string>
                {
                    ["grant_type"] = "client_credentials"
                });
                var response = await client.PostAsync(
                    _authConfig.TokenIssuerUrl,
                    content,
                    cancellationToken);

                if (!response.IsSuccessStatusCode)
                {
#if NET5_0_OR_GREATER
                    var errorContent = await response.Content.ReadAsStringAsync(cancellationToken);
#else
                    var errorContent = await response.Content.ReadAsStringAsync();
#endif
                    var statusCode = ((int)response.StatusCode).ToString();
                    var ex         = new Exception($"Call failed with status: {statusCode} {response.ReasonPhrase}. {errorContent}");
                    Logger?.LogError(ex, "Client credentials failure");
                    return(Outcome <ClientCredentialsResponse> .Fail(ex));
                }

#if NET5_0_OR_GREATER
                var stream = await response.Content.ReadAsStreamAsync(cancellationToken);
#else
                var stream = await response.Content.ReadAsStreamAsync();
#endif
                var responseBody =
                    await JsonSerializer.DeserializeAsync <ClientCredentialsResponse>(stream,
                                                                                      cancellationToken : cancellationToken);

                return(Outcome <ClientCredentialsResponse> .Success(responseBody));
            }
            catch (Exception ex)
            {
                ex = new Exception($"Failed to acquire token using client credentials. {ex.Message}", ex);
                Logger.Error(ex);
                return(Outcome <ClientCredentialsResponse> .Fail(ex));
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Harvest a registry key.
        /// </summary>
        /// <param name="registryKey">The registry key to harvest.</param>
        /// <param name="registryValues">The collected registry values.</param>
        private static void HarvestRegistryKey(RegistryKey registryKey, ArrayList registryValues)
        {
            // harvest the sub-keys
            foreach (string subKeyName in registryKey.GetSubKeyNames())
            {
                using (RegistryKey subKey = registryKey.OpenSubKey(subKeyName))
                    HarvestRegistryKey(subKey, registryValues);
            }

            string[] parts = GetPathParts(registryKey.Name);

            RegistryRootType root;

            switch (parts[0])
            {
            case "HKEY_CLASSES_ROOT":
                root = RegistryRootType.HKCR;
                break;

            case "HKEY_CURRENT_USER":
                root = RegistryRootType.HKCU;
                break;

            case "HKEY_LOCAL_MACHINE":
                root = RegistryRootType.HKLM;
                break;

            case "HKEY_USERS":
                root = RegistryRootType.HKU;
                break;

            default:
                throw new InvalidOperationException(string.Format("Unexpected root."));
            }

            // harvest the values
            foreach (string valueName in registryKey.GetValueNames())
            {
                var registryValue = new RegistryValue();

                registryValue.Action = RegistryValue.ActionType.write;

                registryValue.Root = root;

                if (1 < parts.Length)
                {
                    registryValue.Key = parts[1];
                }

                if (null != valueName && 0 < valueName.Length)
                {
                    registryValue.Name = valueName;
                }

                object value = registryKey.GetValue(valueName);

                if (value == null)
                {
                    throw new InvalidOperationException(string.Format("Value is Null."));
                }

                if (value is byte[])                // binary
                {
                    var hexadecimalValue = new StringBuilder();

                    // convert the byte array to hexadecimal
                    foreach (byte byteValue in (byte[])value)
                    {
                        hexadecimalValue.Append(byteValue.ToString("X2", CultureInfo.InvariantCulture.NumberFormat));
                    }

                    registryValue.Type  = RegistryValue.TypeType.binary;
                    registryValue.Value = hexadecimalValue.ToString();
                }
                else if (value is int)                // integer
                {
                    registryValue.Type  = RegistryValue.TypeType.integer;
                    registryValue.Value = ((int)value).ToString(CultureInfo.InvariantCulture);
                }
                else if (value is string[])                // multi-string
                {
                    registryValue.Type = RegistryValue.TypeType.multiString;

                    foreach (string multiStringValueContent in (string[])value)
                    {
                        var multiStringValue = new MultiStringValue();

                        multiStringValue.Content = multiStringValueContent;

                        registryValue.AddChild(multiStringValue);
                    }
                }
                else if (value is string)                // string, expandable (there is no way to differentiate a string and expandable value in .NET 1.1)
                {
                    registryValue.Type  = RegistryValue.TypeType.@string;
                    registryValue.Value = (string)value;
                }
                else
                {
                    throw new InvalidOperationException(string.Format("Value is {0}.", value.GetType().AssemblyQualifiedName));
                }

                registryValues.Add(registryValue);
            }
        }
Esempio n. 3
0
        protected void Load(RegistryCaptureKey key, IParentElement component)
        {
            // Iterate.

            foreach (RegistryCaptureKey subKey in key.SubKeys)
            {
                Load(subKey, component);
            }

            // Iterate over values.

            foreach (RegistryCaptureValue value in key.Values)
            {
                // Add the common information.

                RegistryValue registryValue = new RegistryValue();
                if (!string.IsNullOrEmpty(value.Name))
                {
                    registryValue.Name = value.Name;
                }
                registryValue.Action = RegistryValue.ActionType.write;
                registryValue.Root   = GetRegistryRoot(key.Root);
                registryValue.Key    = key.RootRelativePath;

                // Add the type specific value.

                if (value is RegistryCaptureStringValue)
                {
                    registryValue.Type  = RegistryValue.TypeType.@string;
                    registryValue.Value = GetValue((RegistryCaptureStringValue)value);
                }
                else if (value is RegistryCaptureDWordValue)
                {
                    registryValue.Type  = RegistryValue.TypeType.integer;
                    registryValue.Value = GetValue((RegistryCaptureDWordValue)value);
                }
                else if (value is RegistryCaptureBinaryValue)
                {
                    registryValue.Type  = RegistryValue.TypeType.binary;
                    registryValue.Value = GetValue((RegistryCaptureBinaryValue)value);
                }
                else if (value is RegistryCaptureExpandStringValue)
                {
                    registryValue.Type  = RegistryValue.TypeType.@string;
                    registryValue.Value = GetValue((RegistryCaptureExpandStringValue)value);
                }
                else if (value is RegistryCaptureMultiStringValue)
                {
                    registryValue.Type = RegistryValue.TypeType.multiString;

                    foreach (string multiStringValueContent in ((RegistryCaptureMultiStringValue)value).Value)
                    {
                        MultiStringValue multiStringValue = new MultiStringValue();
                        multiStringValue.Content = multiStringValueContent;
                        registryValue.AddChild(multiStringValue);
                    }
                }
                else
                {
                    registryValue.Type  = RegistryValue.TypeType.@string;
                    registryValue.Value = GetValue(value);
                }

                component.AddChild(registryValue);
            }
        }
Esempio n. 4
0
        private void ResolveMultiStringValue(SortedList <string, string> directoryIds, SortedList <string, string> fileIds, MultiStringValue multiStringValue)
        {
            string value = multiStringValue.Content.ToLower(CultureInfo.InvariantCulture);

            // Replace file paths with ids.

            foreach (KeyValuePair <string, string> pair in fileIds)
            {
                int index;
                while ((index = value.IndexOf(pair.Key)) != -1)
                {
                    // Replace the non-lower case value itself.

                    multiStringValue.Content = multiStringValue.Content.Remove(index, (pair.Key).Length);
                    multiStringValue.Content = multiStringValue.Content.Insert(index, pair.Value);
                    value = multiStringValue.Content.ToLower(CultureInfo.InvariantCulture);
                }
            }

            // Replace directory paths with ids.

            foreach (KeyValuePair <string, string> pair in directoryIds)
            {
                int index;
                while ((index = value.IndexOf(pair.Key)) != -1)
                {
                    // Replace the non-lower case value itself.

                    multiStringValue.Content = multiStringValue.Content.Remove(index, (pair.Key).Length);
                    multiStringValue.Content = multiStringValue.Content.Insert(index, pair.Value);
                    value = multiStringValue.Content.ToLower(CultureInfo.InvariantCulture);
                }
            }
        }