1

I have somehow come out with a signature for this api call, but the call does not work in the expected fashion. Some vital data structures are not get populated properly hence I am not getting intended output. The signature I've used is:

[DllImport("secur32.dll", SetLastError = true)]
    static extern ulong AcquireCredentialsHandle(
        string pszPrincipal,
        string pszPackage,
        ulong fCredentialsUse,
        IntPtr pvLogonID,
        ref SEC_WINNT_AUTH_IDENTITY pAuthData, 
        //IntPtr pAuthData,
        IntPtr pGetKeyFn,
        IntPtr pGetArgumentKey,
        //ref SecHandle phCredential,
        IntPtr phCredential,
        ref TimeStamp ptsExpiry);

Please ignore the comments.

The c-based function call I used for reference can be found here. I want to know what did I do wrong...

Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317
deostroll
  • 11,661
  • 21
  • 90
  • 161

3 Answers3

2

Did you tried this at pinvoke.net?

Oliver
  • 43,366
  • 8
  • 94
  • 151
2

The struct above doesn't seem correct as the API1 the documentation states

typedef struct _SecHandle {
  ULONG_PTR       dwLower;
  ULONG_PTR       dwUpper;
} SecHandle, * PSecHandle;

That means that the code above will work on 32bit but should be

[StructLayout(LayoutKind.Sequential)]
public struct SECURITY_HANDLE
{
    public IntPtr LowPart;
    public IntPtr HighPart;
    public SECURITY_HANDLE(int dummy)
    {
        LowPart = HighPart = IntPtr.Zero;
    }
};

This will work on both 32bit and 64bit modes.

The rest will be the same as previously

[StructLayout(LayoutKind.Sequential)]
public struct SECURITY_INTEGER
{
    public uint LowPart;
    public int HighPart;
    public SECURITY_INTEGER(int dummy)
    {
        LowPart = 0;
        HighPart = 0;
    }
};

[DllImport("secur32.dll", SetLastError=true)]
  static extern int AcquireCredentialsHandle(
    string pszPrincipal, //SEC_CHAR*
    string pszPackage, //SEC_CHAR* //"Kerberos","NTLM","Negotiative"
    int fCredentialUse,
    IntPtr PAuthenticationID,//_LUID AuthenticationID,//pvLogonID,//PLUID
    IntPtr pAuthData,//PVOID
    int pGetKeyFn, //SEC_GET_KEY_FN
    IntPtr pvGetKeyArgument, //PVOID
    ref SECURITY_HANDLE phCredential, //SecHandle //PCtxtHandle ref
    ref SECURITY_INTEGER ptsExpiry); //PTimeStamp //TimeStamp ref

You should also be careful to free the handle as it is unmanaged.

Drawaes
  • 194
  • 2
  • 8
1

From pInvoke.net

[StructLayout(LayoutKind.Sequential)]
public struct SECURITY_INTEGER
{
    public uint LowPart;
    public int HighPart;
    public SECURITY_INTEGER(int dummy)
    {
    LowPart = 0;
    HighPart = 0;
    }
};

[StructLayout(LayoutKind.Sequential)]
public struct SECURITY_HANDLE
{
    public uint LowPart;
    public uint HighPart;
    public SECURITY_HANDLE(int dummy)
    {
    LowPart = HighPart = 0;
    }
};


[DllImport("secur32.dll", SetLastError=true)]
  static extern int AcquireCredentialsHandle(
    string pszPrincipal, //SEC_CHAR*
    string pszPackage, //SEC_CHAR* //"Kerberos","NTLM","Negotiative"
    int fCredentialUse,
    IntPtr PAuthenticationID,//_LUID AuthenticationID,//pvLogonID, //PLUID
    IntPtr pAuthData,//PVOID
    int pGetKeyFn, //SEC_GET_KEY_FN
    IntPtr pvGetKeyArgument, //PVOID
    ref SECURITY_HANDLE phCredential, //SecHandle //PCtxtHandle ref
    ref SECURITY_INTEGER ptsExpiry); //PTimeStamp //TimeStamp ref
blowdart
  • 55,577
  • 12
  • 114
  • 149