diff --git a/src/libraries/System.DirectoryServices/src/System/DirectoryServices/ActiveDirectory/TrustHelper.cs b/src/libraries/System.DirectoryServices/src/System/DirectoryServices/ActiveDirectory/TrustHelper.cs index 9d4d86d6438dd7..a59dc4f3c7ba5b 100644 --- a/src/libraries/System.DirectoryServices/src/System/DirectoryServices/ActiveDirectory/TrustHelper.cs +++ b/src/libraries/System.DirectoryServices/src/System/DirectoryServices/ActiveDirectory/TrustHelper.cs @@ -28,7 +28,6 @@ internal static class TrustHelper internal static unsafe bool GetTrustedDomainInfoStatus(DirectoryContext context, string? sourceName, string targetName, Interop.Advapi32.TRUST_ATTRIBUTE attribute, bool isForest) { - SafeLsaPolicyHandle? handle = null; IntPtr buffer = (IntPtr)0; bool impersonated = false; IntPtr target = (IntPtr)0; @@ -44,7 +43,7 @@ internal static unsafe bool GetTrustedDomainInfoStatus(DirectoryContext context, try { // get the policy handle first - handle = Utils.GetPolicyHandle(serverName); + using var handle = Utils.GetPolicyHandle(serverName); // get the target name global::Interop.UNICODE_STRING trustedDomainName; @@ -123,7 +122,6 @@ internal static unsafe bool GetTrustedDomainInfoStatus(DirectoryContext context, internal static unsafe void SetTrustedDomainInfoStatus(DirectoryContext context, string? sourceName, string targetName, Interop.Advapi32.TRUST_ATTRIBUTE attribute, bool status, bool isForest) { - SafeLsaPolicyHandle? handle = null; IntPtr buffer = (IntPtr)0; IntPtr newInfo = (IntPtr)0; bool impersonated = false; @@ -139,7 +137,7 @@ internal static unsafe void SetTrustedDomainInfoStatus(DirectoryContext context, try { // get the policy handle first - handle = Utils.GetPolicyHandle(serverName); + using var handle = Utils.GetPolicyHandle(serverName); // get the target name global::Interop.UNICODE_STRING trustedDomainName; @@ -251,7 +249,6 @@ internal static unsafe void SetTrustedDomainInfoStatus(DirectoryContext context, internal static unsafe void DeleteTrust(DirectoryContext sourceContext, string? sourceName, string? targetName, bool isForest) { - SafeLsaPolicyHandle? policyHandle = null; bool impersonated = false; IntPtr target = (IntPtr)0; string? serverName = null; @@ -266,7 +263,7 @@ internal static unsafe void DeleteTrust(DirectoryContext sourceContext, string? try { // get the policy handle - policyHandle = Utils.GetPolicyHandle(serverName); + using var policyHandle = Utils.GetPolicyHandle(serverName); // get the target name global::Interop.UNICODE_STRING trustedDomainName; @@ -327,7 +324,6 @@ internal static unsafe void DeleteTrust(DirectoryContext sourceContext, string? internal static void VerifyTrust(DirectoryContext context, string? sourceName, string? targetName, bool isForest, TrustDirection direction, bool forceSecureChannelReset, string? preferredTargetServer) { - SafeLsaPolicyHandle? policyHandle = null; int win32Error = 0; IntPtr data = (IntPtr)0; IntPtr ptr = (IntPtr)0; @@ -346,7 +342,7 @@ internal static void VerifyTrust(DirectoryContext context, string? sourceName, s try { // get the policy handle - policyHandle = Utils.GetPolicyHandle(policyServerName); + using var policyHandle = Utils.GetPolicyHandle(policyServerName); // get the target name global::Interop.UNICODE_STRING trustedDomainName; @@ -446,7 +442,6 @@ internal static void CreateTrust(DirectoryContext sourceContext, string? sourceN IntPtr unmanagedPassword = (IntPtr)0; IntPtr info = (IntPtr)0; IntPtr domainHandle = (IntPtr)0; - SafeLsaPolicyHandle? policyHandle = null; IntPtr unmanagedAuthData = (IntPtr)0; bool impersonated = false; string? serverName = null; @@ -517,7 +512,7 @@ internal static void CreateTrust(DirectoryContext sourceContext, string? sourceN // do impersonation and get policy handle impersonated = Utils.Impersonate(sourceContext); - policyHandle = Utils.GetPolicyHandle(serverName); + using var policyHandle = Utils.GetPolicyHandle(serverName); uint result = Interop.Advapi32.LsaCreateTrustedDomainEx(policyHandle, tdi, AuthInfoEx, TRUSTED_SET_POSIX | TRUSTED_SET_AUTH, out domainHandle); if (result != 0) @@ -560,7 +555,6 @@ internal static void CreateTrust(DirectoryContext sourceContext, string? sourceN internal static unsafe string UpdateTrust(DirectoryContext context, string? sourceName, string? targetName, string password, bool isForest) { - SafeLsaPolicyHandle? handle = null; IntPtr buffer = (IntPtr)0; IntPtr newBuffer = (IntPtr)0; bool impersonated = false; @@ -581,7 +575,7 @@ internal static unsafe string UpdateTrust(DirectoryContext context, string? sour try { // get the policy handle first - handle = Utils.GetPolicyHandle(serverName); + using var handle = Utils.GetPolicyHandle(serverName); // get the target name global::Interop.UNICODE_STRING trustedDomainName; @@ -691,7 +685,6 @@ internal static unsafe string UpdateTrust(DirectoryContext context, string? sour internal static unsafe void UpdateTrustDirection(DirectoryContext context, string? sourceName, string? targetName, string password, bool isForest, TrustDirection newTrustDirection) { - SafeLsaPolicyHandle? handle = null; IntPtr buffer = (IntPtr)0; IntPtr newBuffer = (IntPtr)0; bool impersonated = false; @@ -711,7 +704,7 @@ internal static unsafe void UpdateTrustDirection(DirectoryContext context, strin try { // get the policy handle first - handle = Utils.GetPolicyHandle(serverName); + using var handle = Utils.GetPolicyHandle(serverName); // get the target name global::Interop.UNICODE_STRING trustedDomainName; @@ -1016,6 +1009,8 @@ private static IntPtr GetTrustedDomainInfo(DirectoryContext targetContext, strin } finally { + if (policyHandle != null) + policyHandle.Dispose(); if (impersonated) Utils.Revert(); }