Windows API Reference

Credentials

The Credentials API provides functions for managing user and machine credentials on the Windows operating system. These functions allow applications to obtain, store, and validate authentication credentials, such as usernames, passwords, and security tokens.

Overview

Managing credentials securely is crucial for application security and user authentication. The Credentials API abstracts many of the complexities involved in handling sensitive authentication data.

Key Concepts

Functions

Credential Management Functions

CredEnumerate
Enumerates the credentials stored on the local computer.
CredWrite
Writes a credential to the credential store.
CredRead
Reads a credential from the credential store.
CredDelete
Deletes a credential from the credential store.

Credential Prompt Functions

CredPromptForCredentials
Prompts the user to enter credentials.
CredUIPromptForCredentials
Provides a user interface for prompting for credentials.

Structures

CREDENTIAL
Represents a credential.
CREDENTIAL_ATTRIBUTE
Represents an attribute of a credential.

Error Codes

Common error codes for the Credentials API include:

Example Usage

The following C++ code snippet demonstrates how to read a credential:


#include <windows.h>
#include <wincred.h>
#include <iostream>

#pragma comment(lib, "credui.lib")

int main() {
    PCREDENTIAL pcred = NULL;
    LPCWSTR targetName = L"MyTargetServer";

    if (CredRead(targetName, CRED_TYPE_GENERIC, 0, &pcred) == TRUE) {
        std::wcout << L"Username: " << pcred->UserName << std::endl;
        // Process password or other credential data securely
        CredFree(pcred);
    } else {
        std::wcerr << L"Failed to read credential for " << targetName << L". Error: " << GetLastError() << std::endl;
    }

    return 0;
}
        

Related Topics