internal static string GetMachineConfigKey()

in src/Azure.WebSites.DataProtection/Util.cs [46:78]


        internal static string GetMachineConfigKey()
        {
            if (!IsAppServiceEnvironment())
            {
                return null;
            }

            // First, attempt to resolve the key from the environment.
            // The App Service infrastructure will inject this in warmup scenarios:
            string key = Environment.GetEnvironmentVariable(AzureWebsiteEnvironmentMachineKey);
            if (!string.IsNullOrEmpty(key))
            {
                return key;
            }

            // If we don't have an environment key set, attemp to read the key from
            // the rootweb.config file:
            string configPath = Environment.ExpandEnvironmentVariables(RootWebConfigPath);
            if (File.Exists(configPath))
            {
                using (var reader = new StringReader(File.ReadAllText(configPath)))
                {
                    var xdoc = XDocument.Load(reader);

                    string siteName = Environment.GetEnvironmentVariable(AzureWebsitesIISSiteName);
                    string xpath = string.Format(CultureInfo.InvariantCulture, MachingKeyXPathFormat, siteName);

                    key = ((IEnumerable)xdoc.XPathEvaluate(xpath)).Cast<XAttribute>().FirstOrDefault()?.Value;
                }
            }

            return key;
        }