AcquireCredentialsHandle win32 api 函数需要 C# 签名

发布于 2024-08-02 02:22:44 字数 725 浏览 6 评论 0原文

我以某种方式为这个 api 调用提供了签名,但是该调用没有按预期方式工作。 一些重要的数据结构没有正确填充,因此我没有得到预期的输出。 我使用的签名是:

[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);

请忽略评论。

我用于参考的基于c的函数调用可以找到 在这里。 我想知道我做错了什么...

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时光清浅 2024-08-09 02:22:44

您是否在 pinvoke.net 上尝试过此操作

Did you tried this at pinvoke.net?

走走停停 2024-08-09 02:22:44

上面的结构似乎不正确,因为 API1 文档指出

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

这意味着上面的代码将在 32 位上运行,但应该

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

在 32 位和 64 位模式上运行。

其余部分将与以前相同

[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

您还应该小心释放句柄,因为它是非托管的。

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.

沙与沫 2024-08-09 02:22:44

来自 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文