Added support for querying a credential's name.

This commit is contained in:
antiduh
2014-06-19 03:07:34 +00:00
parent f96ef74e9b
commit f0f057d3bb
5 changed files with 116 additions and 4 deletions

View File

@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.DirectoryServices.AccountManagement;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
@@ -101,9 +102,33 @@ namespace NSspi
}
}
public string GetName()
public string Name
{
return null;
get
{
NativeMethods.QueryNameAttribCarrier carrier = new NativeMethods.QueryNameAttribCarrier();
SecurityStatus status;
string name = null;
status = NativeMethods.QueryCredentialsAttribute_Name(
ref this.credHandle,
CredentialQueryAttrib.Names,
ref carrier
);
if ( status == SecurityStatus.Success )
{
name = Marshal.PtrToStringUni( carrier.Name );
NativeMethods.FreeContextBuffer( carrier.Name );
}
else
{
throw new SSPIException( "Failed to query credential name", status );
}
return name;
}
}
public long CredentialHandle

23
CredentialQueryAttrib.cs Normal file
View File

@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NSspi
{
/*
#define SECPKG_CRED_ATTR_NAMES 1
#define SECPKG_CRED_ATTR_SSI_PROVIDER 2
#define SECPKG_CRED_ATTR_KDC_PROXY_SETTINGS 3
#define SECPKG_CRED_ATTR_CERT 4
*/
public enum CredentialQueryAttrib : uint
{
Names = 1,
SsiProvider = 2,
KdcProxySettings = 3,
Cert = 4
}
}

View File

@@ -46,6 +46,7 @@
<ItemGroup>
<Compile Include="Credential.cs" />
<Compile Include="CredentialPackage.cs" />
<Compile Include="CredentialQueryAttrib.cs" />
<Compile Include="CredentialType.cs" />
<Compile Include="CredentialUse.cs" />
<Compile Include="NativeMethods.cs" />

View File

@@ -73,5 +73,55 @@ namespace NSspi
ref long credentialHandle
);
/*
SECURITY_STATUS SEC_Entry FreeContextBuffer(
_In_ PVOID pvContextBuffer
);
*/
[DllImport(
"Secur32.dll",
EntryPoint = "FreeContextBuffer",
CallingConvention = CallingConvention.Winapi,
CharSet = CharSet.Unicode,
SetLastError = true
)]
public static extern SecurityStatus FreeContextBuffer( IntPtr buffer );
/*
SECURITY_STATUS SEC_Entry QueryCredentialsAttributes(
_In_ PCredHandle phCredential,
_In_ ULONG ulAttribute,
_Out_ PVOID pBuffer
);
*/
/// <summary>
/// The overload of the QueryCredentialsAttribute method that is used for querying the name attribute.
/// In this call, it takes a void* to a structure that contains a wide char* pointer.
/// </summary>
/// <param name="credentialHandle"></param>
/// <param name="attributeName"></param>
/// <param name="name"></param>
/// <returns></returns>
[DllImport(
"Secur32.dll",
EntryPoint = "QueryCredentialsAttributes",
CallingConvention = CallingConvention.Winapi,
CharSet = CharSet.Unicode,
SetLastError = true
)]
public static extern SecurityStatus QueryCredentialsAttribute_Name(
ref long credentialHandle,
CredentialQueryAttrib attributeName,
ref QueryNameAttribCarrier name
);
[StructLayout( LayoutKind.Sequential )]
public struct QueryNameAttribCarrier
{
public IntPtr Name;
}
}
}

View File

@@ -10,9 +10,22 @@ namespace NSspi
{
public static void Main( string[] args )
{
Credential cred = new Credential( SecurityPackage.Negotiate, CredentialType.Client );
cred.Dispose();
Credential cred = null;
try
{
cred = new Credential( SecurityPackage.Negotiate, CredentialType.Client );
string name = cred.GetName();
Console.Out.WriteLine( name );
Console.Out.Flush();
}
finally
{
if ( cred != null )
{
cred.Dispose();
}
}
}
}
}