有人用过.NET中的Win32 API函数CredWrite吗?

发布于 2024-08-24 04:55:25 字数 2898 浏览 6 评论 0原文

我尝试使用 CredWrite,但收到 ERROR_INVALID_PARAMETER 87 (0x57) 错误。目的是有一个安全的位置来保存我的 .net WPF 应用程序的用户密码。

我的代码:

public class CredMan
{
    private const string TARGET_PREFIX = "myappname:";

    public static void SavePassword(string username, string password)
    {
        Win32CredMan.Credential cred = new Win32CredMan.Credential();
        cred.Flags = 0;
        cred.Type = Win32CredMan.CRED_TYPE.GENERIC;
        cred.TargetName = TARGET_PREFIX + username;

        var encoding = new System.Text.UTF8Encoding();
        cred.CredentialBlob = encoding.GetBytes(password);
        cred.Persist = Win32CredMan.CRED_PERSIST.LOCAL_MACHINE;
        cred.UserName = username;

        bool isGood = Win32CredMan.CredWrite(cred, 0);
        int lastError = Marshal.GetLastWin32Error();

    }
}

这是 win32 包装器:(主要是从 pinvoke.net 获取的)

internal class Win32CredMan
{
    [DllImport("Advapi32.dll", EntryPoint = "CredReadW", CharSet = CharSet.Unicode, SetLastError = true)]
    public static extern bool CredRead(string target, CRED_TYPE type, int reservedFlag,
                      [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(CredentialInMarshaler))]out Credential credential);

    [DllImport("Advapi32.dll", EntryPoint = "CredFreeW", CharSet = CharSet.Unicode, SetLastError = true)]
    public static extern void CredFree(IntPtr buffer);

    [DllImport("Advapi32.dll", SetLastError = true, EntryPoint = "CredWriteW", CharSet = CharSet.Unicode)]
    public static extern bool CredWrite([In] Credential userCredential, [In] UInt32 flags);

    public enum CRED_TYPE : uint
    {
        GENERIC = 1,
        DOMAIN_PASSWORD = 2,
        DOMAIN_CERTIFICATE = 3,
        DOMAIN_VISIBLE_PASSWORD = 4,
        GENERIC_CERTIFICATE = 5,
        DOMAIN_EXTENDED = 6,
        MAXIMUM = 7,      // Maximum supported cred type
        MAXIMUM_EX = (MAXIMUM + 1000),  // Allow new applications to run on old OSes
    }
    public enum CRED_PERSIST : uint
    {
        SESSION = 1,
        LOCAL_MACHINE = 2,
        ENTERPRISE = 3,
    }

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    public struct CREDENTIAL_ATTRIBUTE
    {
        string Keyword;
        uint Flags;
        uint ValueSize;
        IntPtr Value;
    }

    //This type is deliberately not designed to be marshalled.
    public class Credential
    {
        public UInt32 Flags;
        public CRED_TYPE Type;
        public string TargetName;
        public string Comment;
        public System.Runtime.InteropServices.ComTypes.FILETIME LastWritten;
        public byte[] CredentialBlob;
        public CRED_PERSIST Persist;
        public CREDENTIAL_ATTRIBUTE[] Attributes;
        public string TargetAlias;
        public string UserName;
    }
}

I'm trying to use CredWrite, but get an ERROR_INVALID_PARAMETER 87 (0x57) error. The intent is to have a secure place to save the user's password for my .net WPF application.

And my code:

public class CredMan
{
    private const string TARGET_PREFIX = "myappname:";

    public static void SavePassword(string username, string password)
    {
        Win32CredMan.Credential cred = new Win32CredMan.Credential();
        cred.Flags = 0;
        cred.Type = Win32CredMan.CRED_TYPE.GENERIC;
        cred.TargetName = TARGET_PREFIX + username;

        var encoding = new System.Text.UTF8Encoding();
        cred.CredentialBlob = encoding.GetBytes(password);
        cred.Persist = Win32CredMan.CRED_PERSIST.LOCAL_MACHINE;
        cred.UserName = username;

        bool isGood = Win32CredMan.CredWrite(cred, 0);
        int lastError = Marshal.GetLastWin32Error();

    }
}

This is the win32 wrapper: (mostly grabbed from pinvoke.net)

internal class Win32CredMan
{
    [DllImport("Advapi32.dll", EntryPoint = "CredReadW", CharSet = CharSet.Unicode, SetLastError = true)]
    public static extern bool CredRead(string target, CRED_TYPE type, int reservedFlag,
                      [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(CredentialInMarshaler))]out Credential credential);

    [DllImport("Advapi32.dll", EntryPoint = "CredFreeW", CharSet = CharSet.Unicode, SetLastError = true)]
    public static extern void CredFree(IntPtr buffer);

    [DllImport("Advapi32.dll", SetLastError = true, EntryPoint = "CredWriteW", CharSet = CharSet.Unicode)]
    public static extern bool CredWrite([In] Credential userCredential, [In] UInt32 flags);

    public enum CRED_TYPE : uint
    {
        GENERIC = 1,
        DOMAIN_PASSWORD = 2,
        DOMAIN_CERTIFICATE = 3,
        DOMAIN_VISIBLE_PASSWORD = 4,
        GENERIC_CERTIFICATE = 5,
        DOMAIN_EXTENDED = 6,
        MAXIMUM = 7,      // Maximum supported cred type
        MAXIMUM_EX = (MAXIMUM + 1000),  // Allow new applications to run on old OSes
    }
    public enum CRED_PERSIST : uint
    {
        SESSION = 1,
        LOCAL_MACHINE = 2,
        ENTERPRISE = 3,
    }

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    public struct CREDENTIAL_ATTRIBUTE
    {
        string Keyword;
        uint Flags;
        uint ValueSize;
        IntPtr Value;
    }

    //This type is deliberately not designed to be marshalled.
    public class Credential
    {
        public UInt32 Flags;
        public CRED_TYPE Type;
        public string TargetName;
        public string Comment;
        public System.Runtime.InteropServices.ComTypes.FILETIME LastWritten;
        public byte[] CredentialBlob;
        public CRED_PERSIST Persist;
        public CREDENTIAL_ATTRIBUTE[] Attributes;
        public string TargetAlias;
        public string UserName;
    }
}

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

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

发布评论

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

评论(1

明月夜 2024-08-31 04:55:25

我现在遇到了同样的问题。
我发现使用 DOMAIN_PASSWORD 选项作为凭据类型时会出现此问题。
事实证明 TargetName 包含不正确的值。

您应该只指定 dns 或 ip 地址(通配符可选),
但不包含完整的 url 或协议。
例如“*.microsoft.com”是正确的,但“http://www.microsoft.com/”是无效的

我将在此发布此内容以防其他人遇到此问题。我花了一段时间才找到它。

I ran into this same problem now.
I found that this issue occurred using the DOMAIN_PASSWORD option as credential type.
It turns out the TargetName contained an incorrect value.

you should only specify the dns or ip address (wildcard optional),
but NOT containing a full url or protocol.
e.g. "*.microsoft.com" is correct, but "http://www.microsoft.com/" is INVALID

I'll just post this here in case other people run into this issue. took me a while to find it.

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