From bc2ad6bdc1f374e4fbdce7ae025db755eb5622c2 Mon Sep 17 00:00:00 2001 From: Ryan Newington Date: Fri, 5 May 2023 06:53:31 +1000 Subject: [PATCH] Fixes an issue where a NullReferenceException occurrs when trying to set the store path and no policy has yet applied to the machine. --- src/PasswordProtection/Settings.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/PasswordProtection/Settings.cs b/src/PasswordProtection/Settings.cs index 6f60439..0fda870 100644 --- a/src/PasswordProtection/Settings.cs +++ b/src/PasswordProtection/Settings.cs @@ -19,7 +19,7 @@ public string StorePath throw new DirectoryNotFoundException($"The path {value} was not found"); } - var settingsKey = this.hklm.OpenSubKey("Software\\Lithnet\\PasswordFilter", true); + var settingsKey = this.hklm.CreateSubKey("Software\\Lithnet\\PasswordFilter", true); settingsKey.SetValue("Store", value); } } @@ -29,7 +29,7 @@ public bool Enabled get => this.GetPolicyOrSetting("Disabled", 0) == 0; set { - var settingsKey = this.hklm.OpenSubKey("Software\\Lithnet\\PasswordFilter", true); + var settingsKey = this.hklm.CreateSubKey("Software\\Lithnet\\PasswordFilter", true); if (value) { settingsKey.DeleteValue("Disabled", false); @@ -49,7 +49,7 @@ public bool IsFilterRegistered var values = key?.GetValue("Notification Packages") as string[]; - return values.Contains("lithnetpwdf", StringComparer.OrdinalIgnoreCase); + return values?.Contains("lithnetpwdf", StringComparer.OrdinalIgnoreCase) ?? false; } } @@ -67,7 +67,7 @@ private bool IsPolicyEnforced(string setting) { var policyKey = this.hklm.OpenSubKey("Software\\Policies\\Lithnet\\PasswordFilter", false); - return policyKey.GetValue(setting, null) != null; + return policyKey?.GetValue(setting, null) != null; } private T GetPolicyOrSetting(string name, T defaultValue)