Skip to content
This repository was archived by the owner on Dec 30, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 24 additions & 19 deletions Helpers/ProjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -357,25 +357,30 @@ public static async Task<Dictionary<string,string>> GetEvaluatedPropertiesAsync(
}

}
var unConfiguredProject = context.UnconfiguredProject;
var configuredProject = await unConfiguredProject.GetSuggestedConfiguredProjectAsync();
var properties = configuredProject.Services.ProjectPropertiesProvider.GetCommonProperties();

return new Dictionary<string, string>
{
{ "ProjectPath", await properties.GetEvaluatedPropertyValueAsync("ProjectPath").ConfigureAwait(false)},
{ "TargetPath", await properties.GetEvaluatedPropertyValueAsync("TargetPath").ConfigureAwait(false)},
{ "TargetName", await properties.GetEvaluatedPropertyValueAsync("TargetName").ConfigureAwait(false)},
{ "TargetExt", await properties.GetEvaluatedPropertyValueAsync("TargetExt").ConfigureAwait(false)},
{ "TargetFileName", await properties.GetEvaluatedPropertyValueAsync("TargetFileName").ConfigureAwait(false)},
{ "TargetDir", await properties.GetEvaluatedPropertyValueAsync("TargetDir").ConfigureAwait(false)},
{ "ProjectDir", await properties.GetEvaluatedPropertyValueAsync("ProjectDir").ConfigureAwait(false)},
{ "ProjectName", await properties.GetEvaluatedPropertyValueAsync("ProjectName").ConfigureAwait(false)},
{ "SolutionName", await properties.GetEvaluatedPropertyValueAsync("ProjectDir").ConfigureAwait(false)},
{ "SolutionDir", await properties.GetEvaluatedPropertyValueAsync("SolutionDir").ConfigureAwait(false)},
{ "PlatformName", await properties.GetEvaluatedPropertyValueAsync("ProjectDir").ConfigureAwait(false)},
{ "ConfigurationName", await properties.GetEvaluatedPropertyValueAsync("ConfigurationName").ConfigureAwait(false)},
};
var unConfiguredProject = context?.UnconfiguredProject;
if (unConfiguredProject != null)
{
var configuredProject = await unConfiguredProject.GetSuggestedConfiguredProjectAsync();
var properties = configuredProject.Services.ProjectPropertiesProvider.GetCommonProperties();

return new Dictionary<string, string>
{
{ "ProjectPath", await properties.GetEvaluatedPropertyValueAsync("ProjectPath").ConfigureAwait(false)},
{ "TargetPath", await properties.GetEvaluatedPropertyValueAsync("TargetPath").ConfigureAwait(false)},
{ "TargetName", await properties.GetEvaluatedPropertyValueAsync("TargetName").ConfigureAwait(false)},
{ "TargetExt", await properties.GetEvaluatedPropertyValueAsync("TargetExt").ConfigureAwait(false)},
{ "TargetFileName", await properties.GetEvaluatedPropertyValueAsync("TargetFileName").ConfigureAwait(false)},
{ "TargetDir", await properties.GetEvaluatedPropertyValueAsync("TargetDir").ConfigureAwait(false)},
{ "ProjectDir", await properties.GetEvaluatedPropertyValueAsync("ProjectDir").ConfigureAwait(false)},
{ "ProjectName", await properties.GetEvaluatedPropertyValueAsync("ProjectName").ConfigureAwait(false)},
{ "SolutionName", await properties.GetEvaluatedPropertyValueAsync("ProjectDir").ConfigureAwait(false)},
{ "SolutionDir", await properties.GetEvaluatedPropertyValueAsync("SolutionDir").ConfigureAwait(false)},
{ "PlatformName", await properties.GetEvaluatedPropertyValueAsync("ProjectDir").ConfigureAwait(false)},
{ "ConfigurationName", await properties.GetEvaluatedPropertyValueAsync("ConfigurationName").ConfigureAwait(false)},
};
}

return new Dictionary<string, string>();
}
catch
{
Expand Down
53 changes: 24 additions & 29 deletions InternalSecureStorage/SecureLocalStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ public class SecureLocalStorage : ISecureLocalStorage

internal void CreateIfNotExists(string path)
{
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
if (!Directory.Exists(path)) Directory.CreateDirectory(path);
}

public SecureLocalStorage(ISecureLocalStorageConfig configuration)
Expand All @@ -27,45 +26,41 @@ public SecureLocalStorage(ISecureLocalStorageConfig configuration)
Read();
}


internal byte[] EncryptData(string data, byte[] key, DataProtectionScope scope)
{
if (data == null)
throw new ArgumentNullException(nameof(data));
if (data.Length <= 0)
throw new ArgumentException("data");
if (key == null)
throw new ArgumentNullException(nameof(key));
if (key.Length <= 0)
throw new ArgumentException("key");
if (data == null) throw new ArgumentNullException(nameof(data));
if (data.Length <= 0) throw new ArgumentException("data");
if (key == null) throw new ArgumentNullException(nameof(key));
if (key.Length <= 0) throw new ArgumentException("key");

return ProtectedData.Protect(Encoding.UTF8.GetBytes(data), key, scope);
}

internal string DecryptData(byte[] data, byte[] key, DataProtectionScope scope)
{
if (data == null)
throw new ArgumentNullException(nameof(data));
if (data.Length <= 0)
throw new ArgumentException("data");
if (key == null)
throw new ArgumentNullException(nameof(key));
if (key.Length <= 0)
throw new ArgumentException("key");
if (null == data) throw new ArgumentNullException(nameof(data));
if (data.Length <= 0) throw new ArgumentException("data");
if (null == key) throw new ArgumentNullException(nameof(key));
if (key.Length <= 0) throw new ArgumentException("key");

return Encoding.UTF8.GetString(ProtectedData.Unprotect(data, key, scope));
}

internal void Read()
=> StoredData =
File.Exists(Path.Combine(Config.StoragePath, "default"))
? JsonSerializer.Deserialize<Dictionary<string, string>>(DecryptData(File.ReadAllBytes(Path.Combine(Config.StoragePath, "default")), Key, DataProtectionScope.LocalMachine))
: new Dictionary<string, string>();

{
Write(); // TODO remove it, forcing regenerate default file
StoredData = File.Exists(Path.Combine(Config.StoragePath, "default"))
? JsonSerializer.Deserialize<Dictionary<string, string>>(DecryptData(File.ReadAllBytes(Path.Combine(Config.StoragePath, "default")), Key, DataProtectionScope.LocalMachine))
: new Dictionary<string, string>();
}

internal void Write()
=> File.WriteAllBytes(Path.Combine(Config.StoragePath, "default"),
EncryptData(JsonSerializer.Serialize(StoredData), Key, DataProtectionScope.LocalMachine));
{
if (StoredData is null) return;

File.WriteAllBytes(Path.Combine(Config.StoragePath, "default"),
EncryptData(JsonSerializer.Serialize(StoredData), Key, DataProtectionScope.LocalMachine));
}

public int Count => StoredData.Count;

Expand All @@ -76,8 +71,7 @@ public void Clear()
public bool Exists()
=> File.Exists(Path.Combine(Config.StoragePath, "default"));

public bool Exists(string key)
=> StoredData.ContainsKey(key);
public bool Exists(string key) => StoredData?.ContainsKey(key) ?? false;

public string Get(string key)
=> !StoredData.TryGetValue(key, out var value) ? default : JsonSerializer.Deserialize<string>(value ?? string.Empty);
Expand All @@ -93,7 +87,8 @@ public IReadOnlyCollection<string> Keys()

public void Remove(string key)
{
StoredData.Remove(key);
if (Exists(key)) StoredData?.Remove(key);

Write();
}

Expand Down
7 changes: 2 additions & 5 deletions ShieldVSExtensionPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,7 @@ protected override async Task InitializeAsync(CancellationToken cancellationToke

solutionPersistenceService.LoadPackageUserOpts(this, ShieldConfiguration);

if (isSolutionLoaded)
SolutionEventsOnOpened();
if (isSolutionLoaded) SolutionEventsOnOpened();

solutionEvents.Opened += SolutionEventsOnOpened;

Expand All @@ -149,9 +148,7 @@ private bool TryReloadStorage()

try
{
LocalStorage = new SecureLocalStorage(
new CustomLocalStorageConfig(null, "DotnetsaferShieldForVisualStudio").WithDefaultKeyBuilder()
);
LocalStorage = new SecureLocalStorage( new CustomLocalStorageConfig(null, "DotnetsaferShieldForVisualStudio").WithDefaultKeyBuilder());

ExtensionConfiguration = LocalStorage.Exists(ExtensionConfigurationFile)
? LocalStorage.Get<ShieldExtensionConfiguration>(ExtensionConfigurationFile)
Expand Down
2 changes: 1 addition & 1 deletion ToolWindows/ConfigurationWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace ShieldVSExtension.ToolWindows
{
[Guid("23b0bdd9-76b7-4917-a75e-e29a99bcd863")]
public class ConfigurationWindow : ToolWindowPane
public sealed class ConfigurationWindow : ToolWindowPane
{
public ConfigurationWindow() : base(null)
{
Expand Down
Loading