c++windowsvisual-studio-2013credential-manager

why return nothing when i want to read password( CredentialBlob ) from credential manager in windows with type CRED_TYPE_DOMAIN_PASSWORD


i cant read password( CredentialBlob ) from credential manager with type CRED_TYPE_DOMAIN_PASSWORD in windows , i can show user name but return nothing for show password( CredentialBlob ) and size CredentialBlob equal zero . i use visual studio 2013. my code for read :

`

void credReadWindows(){
    string targetName;

    cout << "enter target name" << endl;
    cin >> targetName;

    LPCSTR targetNameLP = targetName.c_str();
    DWORD flags = 0;
    PCREDENTIALA readCred;

    //CRED_TYPE_DOMAIN_PASSWORD
    BOOL result = CredReadA(targetNameLP, CRED_TYPE_DOMAIN_PASSWORD, flags, &readCred);

    if (result) {
        string userName(readCred->UserName);


        string password(reinterpret_cast<char*>(readCred->CredentialBlob),
            static_cast<size_t>(readCred->CredentialBlobSize));
        cout << "size CredentialBlob : " << sizeof(readCred->CredentialBlob) << endl;
        cout << "size : " << readCred->CredentialBlobSize << endl;
        cout << "Username: " << userName << endl;
        cout << "Password: " << password << endl;

        CredFree(readCred);
    }
    else {
        DWORD errorCode = GetLastError();
        cout << "Credential read failed with error code: " << errorCode << endl;
    }

}

and i write credential with below code :

void credWriteWindows() {
string targetName ;
string userName ;
string password ;
string targetAlias;

cout << "enter target name :"<<endl;
cin >> targetName;
cout << "enter user name  :" << endl;
cin >> userName;
cout << "enter password" << endl;
cin >> password;
cout << "enter target Alias:" << endl;
cin >> targetAlias;


CREDENTIALA writeCred = {};
writeCred.Flags = 0;
writeCred.Type = CRED_TYPE_DOMAIN_PASSWORD;
writeCred.TargetName = const_cast<char*>(targetName.c_str());
writeCred.CredentialBlobSize = static_cast<DWORD>(password.length());
writeCred.CredentialBlob = reinterpret_cast<LPBYTE>(const_cast<char*>(password.c_str()));
writeCred.Persist = CRED_PERSIST_LOCAL_MACHINE;
writeCred.UserName = const_cast<char*>(userName.c_str());
writeCred.TargetAlias = const_cast<char*>(targetAlias.c_str());


BOOL result = CredWriteA(&writeCred, 0);

if (result) {
    cout << "Credential write successful" << endl;

}
else {
    DWORD errorCode = GetLastError();
    cout << "Credential write failed with error code: " << errorCode << endl;
}

}


Solution

  • From the documentation (emphasis mine):

    CredentialBlob

    Secret data for the credential. The CredentialBlob member can be both read and written.

    If the Type member is CRED_TYPE_DOMAIN_PASSWORD, this member contains the plaintext Unicode password for UserName. The CredentialBlob and CredentialBlobSize members do not include a trailing zero character. Also, for CRED_TYPE_DOMAIN_PASSWORD, this member can only be read by the authentication packages.