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

5

我正在尝试使用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;
    }
}

请看这里... http://stackoverflow.com/questions/2337672/credwrite-returns-win32-error-code-2-error-invalid-function-incorrect-function/2337753#2337753 - t0mm13b
1
刚刚重新阅读了代码,看起来凭据类没有被序列化(根据注释)...呃! :) - mlsteeves
4
有完整的解决方案,而且它有效。 - mlsteeves
@mlsteeves的链接现在位于https://web.archive.org/web/20100901074259/http://blogs.msdn.com/b/peerchan/archive/2005/11/01/487834.aspx。 - andrensairr
1个回答

3

我现在遇到了同样的问题。 我发现这个问题是使用DOMAIN_PASSWORD选项作为凭证类型引起的。 事实证明,TargetName包含了一个不正确的值。

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

我在这里发帖,以防其他人遇到这个问题。花了我一段时间才找到它。


FYI:指定域的限制与用于密钥链服务的OSX SecKeychainItemCreateFromContent函数类似。 - Jason Harrison

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接