// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.IO; using Microsoft.VisualStudio.SecretManager; namespace Microsoft.Extensions.Configuration.UserSecrets { /// /// Provides paths for user secrets configuration files. /// internal class PathHelper { internal const string SecretsFileName = "secrets.json"; /// /// /// Returns the path to the JSON file that stores user secrets. /// /// /// This uses the current user profile to locate the secrets file on disk in a location outside of source control. /// /// /// The user secret ID. /// The full path to the secret file. public static string GetSecretsPathFromSecretsId(string userSecretsId) { if (string.IsNullOrEmpty(userSecretsId)) { throw new ArgumentException(Resources.Common_StringNullOrEmpty, nameof(userSecretsId)); } var badCharIndex = userSecretsId.IndexOfAny(Path.GetInvalidFileNameChars()); if (badCharIndex != -1) { throw new InvalidOperationException( string.Format( Resources.Error_Invalid_Character_In_UserSecrets_Id, userSecretsId[badCharIndex], badCharIndex)); } var root = Environment.GetEnvironmentVariable("APPDATA") ?? // On Windows it goes to %APPDATA%\Microsoft\UserSecrets\ Environment.GetEnvironmentVariable("HOME"); // On Mac/Linux it goes to ~/.microsoft/usersecrets/ if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("APPDATA"))) { return Path.Combine(root, "Microsoft", "UserSecrets", userSecretsId, SecretsFileName); } else { return Path.Combine(root, ".microsoft", "usersecrets", userSecretsId, SecretsFileName); } } } }