Skip to content

Commit

Permalink
Fix impersonation not impersonating
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobsen9026 committed Feb 10, 2024
1 parent 8362a66 commit 91d44f3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
2 changes: 1 addition & 1 deletion BLAZAM/BLAZAM.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<ServerGarbageCollection>false</ServerGarbageCollection>
<AssemblyVersion>0.8.8</AssemblyVersion>
<Version>2024.02.10.2043</Version>
<Version>2024.02.10.2119</Version>
<IncludeSourceRevisionInInformationalVersion>false</IncludeSourceRevisionInInformationalVersion>
<RootNamespace>BLAZAM</RootNamespace>
<GenerateDocumentationFile>False</GenerateDocumentationFile>
Expand Down
23 changes: 10 additions & 13 deletions BLAZAMCommon/Data/WindowsImpersonation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);


Expand All @@ -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;


Expand All @@ -66,16 +62,17 @@ public WindowsImpersonation(WindowsImpersonationUser user)
{
impersonationUser = user;
}
public async Task<T> RunAsync<T>(Func<T> task) => await Task.Run(() => Run<T>(task));
public T Run<T>(Func<T> task)
public async Task<T?> RunAsync<T>(Func<T> task) => await Task.Run(() => Run<T>(task));
public T? Run<T>(Func<T> 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.
Expand All @@ -84,7 +81,7 @@ public T Run<T>(Func<T> task)


WindowsIdentity.RunImpersonated(
ImpersonatedToken,
impersonatedToken,
() =>
{
// Check the identity.
Expand Down

0 comments on commit 91d44f3

Please sign in to comment.