开发者

Has anyone used the Win32 API function CredWrite in .NET?

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 W开发者_如何学运维PF 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;
    }
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜