Merge pull request #12 from antiduh/pr/11

Pr/11 Integrate @SteveSyfuhs Thread.CurrentPrinciple changes
This commit is contained in:
Kevin Thompson
2018-04-01 18:10:28 -04:00
committed by GitHub
3 changed files with 30 additions and 5 deletions

View File

@@ -1,4 +1,6 @@
using System;
using System.Security.Principal;
using System.Threading;
namespace NSspi.Contexts
{

View File

@@ -1,5 +1,7 @@
using System;
using System.Runtime.CompilerServices;
using System.Security.Principal;
using System.Threading;
using NSspi.Buffers;
using NSspi.Credentials;
@@ -14,19 +16,25 @@ namespace NSspi.Contexts
private ContextAttrib finalAttribs;
private bool impersonating;
private bool impersonationSetsThreadPrinciple;
/// <summary>
/// Performs basic initialization of a new instance of the ServerContext class. The ServerContext
/// is not ready for message manipulation until a security context has been established with a client.
/// Performs basic initialization of a new instance of the ServerContext class. The
/// ServerContext is not ready for message manipulation until a security context has been
/// established with a client.
/// </summary>
/// <param name="cred"></param>
/// <param name="requestedAttribs"></param>
public ServerContext( Credential cred, ContextAttrib requestedAttribs ) : base( cred )
/// <param name="impersonationSetsThreadPrinciple">
/// If true, the `Thread.CurrentPrinciple` property will be modified by successful impersonation.
/// </param>
public ServerContext( Credential cred, ContextAttrib requestedAttribs, bool impersonationSetsThreadPrinciple = false ) : base( cred )
{
this.requestedAttribs = requestedAttribs;
this.finalAttribs = ContextAttrib.Zero;
this.impersonating = false;
this.impersonationSetsThreadPrinciple = impersonationSetsThreadPrinciple;
this.SupportsImpersonate = this.Credential.PackageInfo.Capabilities.HasFlag( SecPkgCapability.Impersonation );
}
@@ -220,7 +228,7 @@ namespace NSspi.Contexts
this.ContextHandle.DangerousRelease();
this.impersonating = true;
this.impersonating = status == SecurityStatus.OK;
}
}
@@ -237,6 +245,11 @@ namespace NSspi.Contexts
throw new SSPIException( "Failed to impersonate the client", status );
}
if( this.impersonating && this.impersonationSetsThreadPrinciple )
{
SetThreadPrinciple();
}
return handle;
}
@@ -299,5 +312,15 @@ namespace NSspi.Contexts
base.Dispose( disposing );
}
/// <summary>
/// Set the current thread security context to the impersonated identity.
/// </summary>
private void SetThreadPrinciple()
{
Thread.CurrentPrincipal = new WindowsPrincipal(
WindowsIdentity.GetCurrent( TokenAccessLevels.AllAccess )
);
}
}
}

View File

@@ -50,6 +50,6 @@ namespace NSspi.Credentials
{
Ansi = 1,
Unicode = 1
Unicode = 2
}
}