Finished implementing GetRemoteIdentity.
This commit is contained in:
@@ -118,15 +118,52 @@ namespace NSspi.Contexts
|
|||||||
this.Disposed = true;
|
this.Disposed = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns the identity of the remote entity.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
public IIdentity GetRemoteIdentity()
|
public IIdentity GetRemoteIdentity()
|
||||||
{
|
{
|
||||||
|
IIdentity result = null;
|
||||||
|
|
||||||
using( var tokenHandle = GetContextToken() )
|
using( var tokenHandle = GetContextToken() )
|
||||||
{
|
{
|
||||||
return new WindowsIdentity(
|
bool gotRef = false;
|
||||||
tokenHandle.DangerousGetHandle(),
|
|
||||||
this.Credential.SecurityPackage
|
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()
|
private SafeTokenHandle GetContextToken()
|
||||||
|
|||||||
27
NSspi/Contexts/SafeTokenHandle.cs
Normal file
27
NSspi/Contexts/SafeTokenHandle.cs
Normal file
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user