[go: up one dir, main page]

Skip to content

Commit

Permalink
Finished implementing GetRemoteIdentity.
Browse files Browse the repository at this point in the history
  • Loading branch information
antiduh committed Aug 2, 2019
1 parent 16bd8b2 commit 1a7f60d
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
45 changes: 41 additions & 4 deletions NSspi/Contexts/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,52 @@ protected virtual void Dispose( bool disposing )
this.Disposed = true;
}

/// <summary>
/// Returns the identity of the remote entity.
/// </summary>
/// <returns></returns>
public IIdentity GetRemoteIdentity()
{
IIdentity result = null;

using( var tokenHandle = GetContextToken() )
{
return new WindowsIdentity(
tokenHandle.DangerousGetHandle(),
this.Credential.SecurityPackage
);
bool gotRef = false;

RuntimeHelpers.PrepareConstrainedRegions();
try
{
tokenHandle.DangerousAddRef( ref gotRef );
}
catch( Exception )
{
if( gotRef )
{
tokenHandle.DangerousRelease();
gotRef = false;
}

throw;
}
finally
{
try
{
result = new WindowsIdentity(
tokenHandle.DangerousGetHandle(),
this.Credential.SecurityPackage
);
}
finally
{
// Make sure we release the handle, even if the allocation for
// WindowsIdentity fails.
tokenHandle.DangerousRelease();
}
}
}

return result;
}

private SafeTokenHandle GetContextToken()
Expand Down
27 changes: 27 additions & 0 deletions NSspi/Contexts/SafeTokenHandle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Runtime.InteropServices;

namespace NSspi.Contexts
{
public class SafeTokenHandle : SafeHandle
{
public SafeTokenHandle() : base( IntPtr.Zero, true )
{
}

public override bool IsInvalid
{
get
{
return handle == IntPtr.Zero || handle == new IntPtr( -1 );
}
}

protected override bool ReleaseHandle()
{
NativeMethods.CloseHandle( this.handle );

return true;
}
}
}

0 comments on commit 1a7f60d

Please sign in to comment.