Sample code for 30+ languages & platforms
Unicode C Requires Chilkat v11.6.0+

Amazon Cognito: Reset a Forgotten Password

See more Amazon Cognito Examples

Choose request to send a recovery code, then confirm after the user supplies it. Private reset-input.json contains username for request and username, code, new_password for confirm. Keep codes as strings. The user must be eligible under the pool's account-recovery settings and have an available verified delivery attribute. MFA settings can affect which recovery methods are available.

Live operation: sends a recovery message or changes a password. It does not authenticate the user; perform a new sign-in afterward. Enable PreventUserExistenceErrors on the app client and use neutral UI wording on both success and failure. Do not expose detailed AWS errors as an account-enumeration mechanism.

Uses direct Chilkat.Http requests to the regional Cognito User Pools JSON API. Public app-client operations do not use IAM signing. These examples use a public app client without a client secret; do not add administrative AWS credentials. For a confidential app client, implement the operation-specific SecretHash/SECRET_HASH or ClientSecret requirements on a trusted system.

Create qa_data/cognito/app.json with string fields region (for example us-west-2), user_pool_id, client_id, and account_key (your local account identifier). OAuth examples also require domain, the trusted HTTPS base URL of your Cognito domain, without a trailing slash. The region and user pool must match the app client. These examples use commercial AWS endpoints.

Sample cache/input files contain secrets. Use an access-controlled directory outside web content and source control, separate each local user/account, and serialize operations that modify the same cache. In a real application obtain passwords and codes through private UI instead of leaving them in files. Cache writes retain a .bak copy; protect and remove obsolete backups and challenge files after the flow. Production storage should provide atomic replacement and OS-backed secret protection. Never restore an old refresh token automatically after rotation. These examples do not require a hosted web server.

Checks transport completion, HTTP status, and required response fields before using results. Cognito often reports throttling, invalid codes, and authentication failures as HTTP 400. Present generic sign-in/recovery errors to users; keep detailed diagnostics private. Retry throttled reads with bounded backoff, but reconcile uncertain writes and refresh rotations before repeating them.

AWS documentation 1 | AWS documentation 2

Chilkat Unicode C Downloads

Unicode C
#include <C_CkJsonObjectW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkHttpW.h>
#include <C_CkHttpResponseW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkJsonObjectW app;
    const wchar_t *clientId;
    HCkStringBuilderW endpoint;
    HCkHttpW http;
    HCkHttpResponseW resp;
    HCkJsonObjectW body;
    HCkJsonObjectW result;
    const wchar_t *action;
    HCkJsonObjectW input;

    success = FALSE;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    success = FALSE;
    //  App settings are trusted configuration. Keep a separate account_key/cache per local user.
    app = CkJsonObjectW_Create();
    CkJsonObjectW_putStrict(app,TRUE);
    success = CkJsonObjectW_LoadFile(app,L"qa_data/cognito/app.json");
    if (success != TRUE) {
        wprintf(L"Could not load Cognito app settings.\n");
        CkJsonObjectW_Dispose(app);
        return;
    }

    if ((CkJsonObjectW_JsonTypeOf(app,L"region") != 1) || (CkJsonObjectW_stringOf(app,L"region") == L"")) {
        wprintf(L"Missing or invalid region.\n");
        CkJsonObjectW_Dispose(app);
        return;
    }

    if ((CkJsonObjectW_JsonTypeOf(app,L"user_pool_id") != 1) || (CkJsonObjectW_stringOf(app,L"user_pool_id") == L"")) {
        wprintf(L"Missing or invalid user_pool_id.\n");
        CkJsonObjectW_Dispose(app);
        return;
    }

    if ((CkJsonObjectW_JsonTypeOf(app,L"client_id") != 1) || (CkJsonObjectW_stringOf(app,L"client_id") == L"")) {
        wprintf(L"Missing or invalid client_id.\n");
        CkJsonObjectW_Dispose(app);
        return;
    }

    if ((CkJsonObjectW_JsonTypeOf(app,L"account_key") != 1) || (CkJsonObjectW_stringOf(app,L"account_key") == L"")) {
        wprintf(L"Missing or invalid account_key.\n");
        CkJsonObjectW_Dispose(app);
        return;
    }

    clientId = CkJsonObjectW_stringOf(app,L"client_id");
    endpoint = CkStringBuilderW_Create();
    CkStringBuilderW_Append(endpoint,L"https://cognito-idp.");
    CkStringBuilderW_Append(endpoint,CkJsonObjectW_stringOf(app,L"region"));
    CkStringBuilderW_Append(endpoint,L".amazonaws.com/");
    http = CkHttpW_Create();
    CkHttpW_putRequireSslCertVerify(http,TRUE);
    CkHttpW_putFollowRedirects(http,FALSE);
    CkHttpW_putConnectTimeout(http,20);
    CkHttpW_putReadTimeout(http,60);
    resp = CkHttpResponseW_Create();
    body = CkJsonObjectW_Create();
    result = CkJsonObjectW_Create();
    CkJsonObjectW_putStrict(result,TRUE);
    action = L"request";
    if ((action != L"request") && (action != L"confirm")) {
        wprintf(L"Choose request or confirm.\n");
        CkJsonObjectW_Dispose(app);
        CkStringBuilderW_Dispose(endpoint);
        CkHttpW_Dispose(http);
        CkHttpResponseW_Dispose(resp);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        return;
    }

    input = CkJsonObjectW_Create();
    CkJsonObjectW_putStrict(input,TRUE);
    success = CkJsonObjectW_LoadFile(input,L"qa_data/cognito/reset-input.json");
    if (success != TRUE) {
        wprintf(L"Could not load qa_data/cognito/reset-input.json.\n");
        CkJsonObjectW_Dispose(app);
        CkStringBuilderW_Dispose(endpoint);
        CkHttpW_Dispose(http);
        CkHttpResponseW_Dispose(resp);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        CkJsonObjectW_Dispose(input);
        return;
    }

    if ((CkJsonObjectW_JsonTypeOf(input,L"username") != 1) || (CkJsonObjectW_stringOf(input,L"username") == L"")) {
        wprintf(L"Missing or invalid username.\n");
        CkJsonObjectW_Dispose(app);
        CkStringBuilderW_Dispose(endpoint);
        CkHttpW_Dispose(http);
        CkHttpResponseW_Dispose(resp);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(result);
        CkJsonObjectW_Dispose(input);
        return;
    }

    CkJsonObjectW_UpdateString(body,L"ClientId",clientId);
    CkJsonObjectW_UpdateString(body,L"Username",CkJsonObjectW_stringOf(input,L"username"));
    if (action == L"request") {
        CkHttpW_SetRequestHeader(http,L"X-Amz-Target",L"AWSCognitoIdentityProviderService.ForgotPassword");
        success = CkHttpW_HttpJson(http,L"POST",CkStringBuilderW_getAsString(endpoint),body,L"application/x-amz-json-1.0",resp);
        if (CkHttpResponseW_getStatusCode(resp) != 200) {
            wprintf(L"HTTP status: %d\n",CkHttpResponseW_getStatusCode(resp));
            success = CkJsonObjectW_Load(result,CkHttpResponseW_bodyStr(resp));
            if (success == TRUE) {
                wprintf(L"Error type: %s\n",CkJsonObjectW_stringOf(result,L"__type"));
                wprintf(L"OAuth error: %s\n",CkJsonObjectW_stringOf(result,L"error"));
            }

            //  Inspect detailed diagnostics privately. Do not log passwords, sessions, or tokens.
            CkJsonObjectW_Dispose(app);
            CkStringBuilderW_Dispose(endpoint);
            CkHttpW_Dispose(http);
            CkHttpResponseW_Dispose(resp);
            CkJsonObjectW_Dispose(body);
            CkJsonObjectW_Dispose(result);
            CkJsonObjectW_Dispose(input);
            return;
        }

        if (success != TRUE) {
            wprintf(L"The HTTP exchange did not complete.\n");
            CkJsonObjectW_Dispose(app);
            CkStringBuilderW_Dispose(endpoint);
            CkHttpW_Dispose(http);
            CkHttpResponseW_Dispose(resp);
            CkJsonObjectW_Dispose(body);
            CkJsonObjectW_Dispose(result);
            CkJsonObjectW_Dispose(input);
            return;
        }

        success = CkJsonObjectW_Load(result,CkHttpResponseW_bodyStr(resp));
        if (success != TRUE) {
            wprintf(L"Expected a JSON response.\n");
            CkJsonObjectW_Dispose(app);
            CkStringBuilderW_Dispose(endpoint);
            CkHttpW_Dispose(http);
            CkHttpResponseW_Dispose(resp);
            CkJsonObjectW_Dispose(body);
            CkJsonObjectW_Dispose(result);
            CkJsonObjectW_Dispose(input);
            return;
        }

        //  A public UI should not disclose whether an account exists.
        wprintf(L"If the account is eligible, a recovery code will be sent.\n");
    }
    else {
        if ((CkJsonObjectW_JsonTypeOf(input,L"code") != 1) || (CkJsonObjectW_stringOf(input,L"code") == L"")) {
            wprintf(L"Missing or invalid code.\n");
            CkJsonObjectW_Dispose(app);
            CkStringBuilderW_Dispose(endpoint);
            CkHttpW_Dispose(http);
            CkHttpResponseW_Dispose(resp);
            CkJsonObjectW_Dispose(body);
            CkJsonObjectW_Dispose(result);
            CkJsonObjectW_Dispose(input);
            return;
        }

        if ((CkJsonObjectW_JsonTypeOf(input,L"new_password") != 1) || (CkJsonObjectW_stringOf(input,L"new_password") == L"")) {
            wprintf(L"Missing or invalid new_password.\n");
            CkJsonObjectW_Dispose(app);
            CkStringBuilderW_Dispose(endpoint);
            CkHttpW_Dispose(http);
            CkHttpResponseW_Dispose(resp);
            CkJsonObjectW_Dispose(body);
            CkJsonObjectW_Dispose(result);
            CkJsonObjectW_Dispose(input);
            return;
        }

        CkJsonObjectW_UpdateString(body,L"ConfirmationCode",CkJsonObjectW_stringOf(input,L"code"));
        CkJsonObjectW_UpdateString(body,L"Password",CkJsonObjectW_stringOf(input,L"new_password"));
        CkHttpW_SetRequestHeader(http,L"X-Amz-Target",L"AWSCognitoIdentityProviderService.ConfirmForgotPassword");
        success = CkHttpW_HttpJson(http,L"POST",CkStringBuilderW_getAsString(endpoint),body,L"application/x-amz-json-1.0",resp);
        if (CkHttpResponseW_getStatusCode(resp) != 200) {
            wprintf(L"HTTP status: %d\n",CkHttpResponseW_getStatusCode(resp));
            success = CkJsonObjectW_Load(result,CkHttpResponseW_bodyStr(resp));
            if (success == TRUE) {
                wprintf(L"Error type: %s\n",CkJsonObjectW_stringOf(result,L"__type"));
                wprintf(L"OAuth error: %s\n",CkJsonObjectW_stringOf(result,L"error"));
            }

            //  Inspect detailed diagnostics privately. Do not log passwords, sessions, or tokens.
            CkJsonObjectW_Dispose(app);
            CkStringBuilderW_Dispose(endpoint);
            CkHttpW_Dispose(http);
            CkHttpResponseW_Dispose(resp);
            CkJsonObjectW_Dispose(body);
            CkJsonObjectW_Dispose(result);
            CkJsonObjectW_Dispose(input);
            return;
        }

        if (success != TRUE) {
            wprintf(L"The HTTP exchange did not complete.\n");
            CkJsonObjectW_Dispose(app);
            CkStringBuilderW_Dispose(endpoint);
            CkHttpW_Dispose(http);
            CkHttpResponseW_Dispose(resp);
            CkJsonObjectW_Dispose(body);
            CkJsonObjectW_Dispose(result);
            CkJsonObjectW_Dispose(input);
            return;
        }

        wprintf(L"Password reset accepted. Sign in with the new password.\n");
    }



    CkJsonObjectW_Dispose(app);
    CkStringBuilderW_Dispose(endpoint);
    CkHttpW_Dispose(http);
    CkHttpResponseW_Dispose(resp);
    CkJsonObjectW_Dispose(body);
    CkJsonObjectW_Dispose(result);
    CkJsonObjectW_Dispose(input);

    }