58 lines
2.2 KiB
C#
58 lines
2.2 KiB
C#
// 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
|
|
{
|
|
/// <summary>
|
|
/// Provides paths for user secrets configuration files.
|
|
/// </summary>
|
|
internal class PathHelper
|
|
{
|
|
internal const string SecretsFileName = "secrets.json";
|
|
|
|
/// <summary>
|
|
/// <para>
|
|
/// Returns the path to the JSON file that stores user secrets.
|
|
/// </para>
|
|
/// <para>
|
|
/// This uses the current user profile to locate the secrets file on disk in a location outside of source control.
|
|
/// </para>
|
|
/// </summary>
|
|
/// <param name="userSecretsId">The user secret ID.</param>
|
|
/// <returns>The full path to the secret file.</returns>
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
}
|