.NET User Secret management

This commit is contained in:
Matt Parker
2025-10-29 19:02:21 +10:00
parent a3d055573f
commit 0d974267e2
4 changed files with 72 additions and 1 deletions

View File

@@ -0,0 +1,39 @@
using Ardalis.GuardClauses;
using SharpIDE.Application.Features.SolutionDiscovery.VsPersistence;
namespace SharpIDE.Application.Features.Evaluation;
public class DotnetUserSecretsService
{
public async Task<(Guid, string filePath)> GetOrCreateUserSecretsId(SharpIdeProjectModel projectModel)
{
using var _ = SharpIdeOtel.Source.StartActivity($"{nameof(DotnetUserSecretsService)}.{nameof(GetOrCreateUserSecretsId)}");
Guard.Against.Null(projectModel, nameof(projectModel));
var userSecretsId = ProjectEvaluation.GetOrCreateDotnetUserSecretsId(projectModel);
var userSecretsFilePath = GetUserSecretsFilePath(userSecretsId);
var file = new FileInfo(userSecretsFilePath);
if (file.Exists)
{
return (userSecretsId, userSecretsFilePath);
}
var directory = file.Directory;
if (directory!.Exists is false)
{
directory.Create();
}
await File.WriteAllTextAsync(userSecretsFilePath, "{}").ConfigureAwait(false);
return (userSecretsId, userSecretsFilePath);
}
private static string GetUserSecretsFilePath(Guid userSecretsId)
{
var appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
var userSecretsPath = OperatingSystem.IsWindows() switch
{
true => Path.Combine(appDataPath, "Microsoft", "UserSecrets", userSecretsId.ToString(), "secrets.json"),
false => Path.Combine(appDataPath, ".microsoft", "usersecrets", userSecretsId.ToString(), "secrets.json")
};
return userSecretsPath;
}
}

View File

@@ -37,4 +37,22 @@ public static class ProjectEvaluation
Guard.Against.NullOrWhiteSpace(targetPath, nameof(targetPath));
return targetPath;
}
public static Guid GetOrCreateDotnetUserSecretsId(SharpIdeProjectModel projectModel)
{
Guard.Against.Null(projectModel, nameof(projectModel));
var project = _projectCollection.GetLoadedProjects(projectModel.FilePath).Single();
var projectRootElement = project.Xml;
var userSecretsId = project.GetPropertyValue("UserSecretsId");
if (string.IsNullOrWhiteSpace(userSecretsId))
{
var newGuid = Guid.NewGuid();
var property = projectRootElement.AddProperty("UserSecretsId", newGuid.ToString());
project.Save();
return newGuid;
}
return Guid.Parse(userSecretsId);
}
}