http://ventquery.com/file/2017-03-31/1490937485_2b0d99bd7b/1490937485_2b0d99bd7b ventquery.com | File Crawler

File Information

http://pinvoke.net/default.aspx/advapi32/CredRead.html?diff=y
file/2017-03-31/1490937485_2b0d99bd7b/1490937485_2b0d99bd7b pinvoke.net: CredRead (advapi32)
 
Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Support Forum
Download Visual Studio Add-In

Terms of Use
Privacy Policy
CredRead (advapi32)
 
.
Summary
Read the credential (generic, domain, etc) from the windows key ring.

C# Signature:

        [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);
       [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(CredentialInMarshaler))]out Credential credential);

VB Signature:

Declare Function CredRead Lib "advapi32.dll" (TODO) As TODO

User-Defined Types:

        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;
        }

        /// <summary>
        ///
        /// </summary>
        public class CredentialInMarshaler : ICustomMarshaler
        {
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
        private class NATIVECREDENTIAL
        {
            public UInt32 Flags;
            public CRED_TYPE Type;
            public string TargetName;
            public string Comment;
            public System.Runtime.InteropServices.ComTypes.FILETIME LastWritten;
            public UInt32 CredentialBlobSize;
            public IntPtr CredentialBlob;
            public CRED_PERSIST Persist;
            public UInt32 AttributeCount;
            public IntPtr Attributes;
            public string TargetAlias;
            public string UserName;
        }          

        public void CleanUpManagedData(object ManagedObj)
        {
            // Nothing to do since all data can be garbage collected.
        }

        public void CleanUpNativeData(IntPtr pNativeData)
        {
            if (pNativeData == IntPtr.Zero)
            {
            return;
            }
            CredFree(pNativeData);
        }

        public int GetNativeDataSize()
        {
            throw new NotImplementedException();
        }

        public IntPtr MarshalManagedToNative(object obj)
        {
            throw new NotImplementedException();
        }

        public object MarshalNativeToManaged(IntPtr pNativeData)
        {
            if (pNativeData == IntPtr.Zero)
            {
            return null;
            }

            NATIVECREDENTIAL lRawCredential = (NATIVECREDENTIAL)Marshal.PtrToStructure(pNativeData, typeof(NATIVECREDENTIAL));

            Credential lCredential = new Credential()
            {
            UserName = lRawCredential.UserName,
            TargetName = lRawCredential.TargetName,
            TargetAlias = lRawCredential.TargetAlias,
            Persist = lRawCredential.Persist,
            Comment = lRawCredential.Comment,
            Flags = lRawCredential.Flags,
            LastWritten = lRawCredential.LastWritten,
            Type = lRawCredential.Type,
            CredentialBlob = new byte[lRawCredential.CredentialBlobSize],
            Attributes = new CREDENTIAL_ATTRIBUTE[lRawCredential.AttributeCount]
            };

            Marshal.Copy(lRawCredential.CredentialBlob, lCredential.CredentialBlob, 0, (int)lRawCredential.CredentialBlobSize);

            return lCredential;
        }

        public static ICustomMarshaler GetInstance(string cookie)
        {
            return new CredentialInMarshaler();
        }
        }

Alternative Managed API:

Do you know one? Please contribute it!

Notes:

Attributes are not properly supported at this time.

Attributes are bit properly supported at this time.

An alternate approach to this can be found at http://blogs.msdn.com/peerchan/pages/487834.aspx. However as presented that technique requires more manual marshalling and more work for the calling code.

Custom Marshalling provide a self contained and caller transparent solution to this scenario, the returned Credentials object is fully managed without the need to worry about object lifetimes.

Tips & Tricks:

Use System.Text.Encoding.Unicode.GetString to convert the CredentialBlob into a password string.

Sample Code:

Documentation
CredRead on MSDN

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions


Eco-coloring and activity books

This packet of books for youth contains 3 coloring and activity books: (1)COLORS OF HEALTH ....(2) DO and LEARN ECO-FUN BOOK....and (3) AMAZING ANCESTORS. There are many pages of fun activities and coloring pages which are also educational.

RFP's, Proposals, and Contracts for Web Designers and Agencies

This full 31 video course you can learn how to increase you income, land more clients, and protect yourself with contracts. 15 bonus down-loadable documents included.

server hosting cell phone accessory discount electronic laptop computer tablet computer game bluetooth monitor headset hard drive

Site Map

Site Map 0  Site Map 1  Site Map 2  Site Map 3  Site Map 4  Site Map 5  Site Map 6  Site Map 7  Site Map 8  Site Map 9  Site Map 10  Site Map 11  Site Map 12  Site Map 13  Site Map 14  Site Map 15  Site Map 16  Site Map 17  Site Map 18  Site Map 19  Site Map 20  Site Map 21  Site Map 22  Site Map 23  Site Map 24  Site Map 25  Site Map 26  Site Map 27  Site Map 28  Site Map 29  Site Map 30  Site Map 31  Site Map 32  Site Map 33  Site Map 34  Site Map 35  Site Map 36  Site Map 37  Site Map 38  Site Map 39  Site Map 40  Site Map 41  Site Map 42  Site Map 43  Site Map 44  Site Map 45  Site Map 46  Site Map 47  Site Map 48  Site Map 49  Site Map 50  Site Map 51  Site Map 52  Site Map 53  Site Map 54  Site Map 55  Site Map 56  Site Map 57  Site Map 58  Site Map 59  Site Map 60  Site Map 61  Site Map 62  Site Map 63  Site Map 64  Site Map 65  Site Map 66  Site Map 67  Site Map 68  Site Map 69  Site Map 70  Site Map 71  Site Map 72  Site Map 73  Site Map 74  Site Map 75  Site Map 76  Site Map 77  Site Map 78  Site Map 79  Site Map 80  Site Map 81  Site Map 82  Site Map 83  Site Map 84  Site Map 85  Site Map 86  Site Map 87  Site Map 88  Site Map 89  Site Map 90  Site Map 91  Site Map 92  Site Map 93  Site Map 94  Site Map 95  Site Map 96  Site Map 97  Site Map 98  Site Map 99  Site Map 100  Site Map 101  Site Map 102  Site Map 103  Site Map 104  Site Map 105  Site Map 106  Site Map 107  Site Map 108  Site Map 109  Site Map 110  Site Map 111  Site Map 112  Site Map 113  Site Map 114  Site Map 115  Site Map 116  Site Map 117  Site Map 118  Site Map 119  Site Map 120  Site Map 121  Site Map 122  Site Map 123  Site Map 124  Site Map 125  Site Map 126  Site Map 127  Site Map 128  Site Map 129  Site Map 130  Site Map 131  Site Map 132  Site Map 133  Site Map 134  Site Map 135  Site Map 136  Site Map 137  Site Map 138  Site Map 139  Site Map 140  Site Map 141  Site Map 142  Site Map 143  Site Map 144  Site Map 145  Site Map 146  Site Map 147  Site Map 148  Site Map 149  Site Map 150  Site Map 151  Site Map 152  Site Map 153  Site Map 154  Site Map 155  Site Map 156  Site Map 157  Site Map 158  Site Map 159  Site Map 160  Site Map 161  Site Map 162  Site Map 163  Site Map 164  Site Map 165  Site Map 166  Site Map 167  Site Map 168  Site Map 169  Site Map 170  Site Map 171  Site Map 172  Site Map 173  Site Map 174  Site Map 175  Site Map 176  Site Map 177  Site Map 178  Site Map 179  Site Map 180  Site Map 181  Site Map 182  Site Map 183  Site Map 184  Site Map 185  Site Map 186  Site Map 187