diff --git a/BLAZAM/BLAZAM.csproj b/BLAZAM/BLAZAM.csproj index 87e898e5..7e08cdd9 100644 --- a/BLAZAM/BLAZAM.csproj +++ b/BLAZAM/BLAZAM.csproj @@ -6,7 +6,7 @@ enable false 0.8.8 - 2024.02.10.2043 + 2024.02.10.2119 false BLAZAM False diff --git a/BLAZAMCommon/Data/WindowsImpersonation.cs b/BLAZAMCommon/Data/WindowsImpersonation.cs index 33033188..203167e1 100644 --- a/BLAZAMCommon/Data/WindowsImpersonation.cs +++ b/BLAZAMCommon/Data/WindowsImpersonation.cs @@ -19,13 +19,10 @@ public static SafeAccessTokenHandle ImpersonatedToken { get { - if (safeAccessTokenHandle == null) - { - // Call LogonUser to obtain a handle to an access token. - - + //Use interactive logon + bool returnValue = LogonUser(impersonationUser.Username, impersonationUser.FQDN!=null?impersonationUser.FQDN:"", impersonationUser.Password.ToPlainText(), - LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, + LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, out safeAccessTokenHandle); @@ -42,16 +39,15 @@ public static SafeAccessTokenHandle ImpersonatedToken throw new AuthenticationException(exception.Message); } } - } return safeAccessTokenHandle; } - set => safeAccessTokenHandle = value; } const int LOGON32_PROVIDER_DEFAULT = 0; //This parameter causes LogonUser to create a primary token. const int LOGON32_LOGON_INTERACTIVE = 2; + const int LOGON32_LOGON_NETWORK = 9; @@ -66,16 +62,17 @@ public WindowsImpersonation(WindowsImpersonationUser user) { impersonationUser = user; } - public async Task RunAsync(Func task) => await Task.Run(() => Run(task)); - public T Run(Func task) + public async Task RunAsync(Func task) => await Task.Run(() => Run(task)); + public T? Run(Func task) { - T result = default; + T? result = default; try { - if (ImpersonatedToken==null || ImpersonatedToken.IsInvalid) throw new ApplicationException("The impersonation user is invalid. Check settings."); + var impersonatedToken = ImpersonatedToken; + if (impersonatedToken == null || impersonatedToken.IsInvalid) throw new ApplicationException("The impersonation user is invalid. Check settings."); //Console.WriteLine("Did LogonUser Succeed? " + (returnValue ? "Yes" : "No")); // Check the identity. @@ -84,7 +81,7 @@ public T Run(Func task) WindowsIdentity.RunImpersonated( - ImpersonatedToken, + impersonatedToken, () => { // Check the identity.