private static void FetchMetadata(Logging.IInternalLogger logger, IdentityProviders identityProviders, string metadataLocation) { // Get new metadata files foreach (var identityProvider in identityProviders) { logger.DebugFormat("Attempting to fetch SAML metadata file for identity provider {0}", identityProvider.Id); var metadataEndpoint = identityProvider.Endpoints.FirstOrDefault(x => x.Type == EndpointType.Metadata); if (metadataEndpoint == null) { continue; } var metadataEndpointUrl = metadataEndpoint.Url; var metadataFile = Path.Combine(metadataLocation, identityProvider.Id + ".xml"); // Fetch new file try { var client = new WebClient(); client.DownloadFile(metadataEndpointUrl, metadataFile + ".new"); // Wipe old file if (File.Exists(metadataFile)) { File.Delete(metadataFile); } // Move new file into place File.Move(metadataFile + ".new", metadataFile); logger.DebugFormat("Successfully updated SAML metadata file for identity provider {0}", identityProvider.Id); } catch (WebException ex) { logger.Warn(string.Format("Unable to fetch SAML metadata file for identity provider {0}", identityProvider.Id), ex); } } }