Amazon Cognito: Register and Confirm a User
See more Amazon Cognito Examples
Choose signup, confirm, or resend. A private signup-input.json supplies username, password and email for signup, or username and code for confirmation. Confirmation is a later user action; do not reuse a placeholder code or automatically confirm the user as an administrator. Resend requests another delivery. Self-registration must be enabled, and this password-based example must supply all required schema attributes.
Live operation: creates an account and can send email/SMS. Confirmation can establish a verified alias, so ForceAliasCreation=false prevents moving an existing alias from someone else. SignUp can return LimitExceeded even after creating an UNCONFIRMED user; investigate/resend rather than repeatedly creating accounts. Successful submission does not prove inbox delivery.
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 | AWS documentation 3
Chilkat Unicode C Downloads
#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);
// Run one step per invocation; collect a real code from the recipient before "confirm".
action = L"signup";
if ((action != L"signup") && (action != L"confirm") && (action != L"resend")) {
wprintf(L"Choose signup, confirm, or resend.\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/signup-input.json");
if (success != TRUE) {
wprintf(L"Could not load qa_data/cognito/signup-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"signup") {
if ((CkJsonObjectW_JsonTypeOf(input,L"password") != 1) || (CkJsonObjectW_stringOf(input,L"password") == L"")) {
wprintf(L"Missing or invalid password.\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"email") != 1) || (CkJsonObjectW_stringOf(input,L"email") == L"")) {
wprintf(L"Missing or invalid email.\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"Password",CkJsonObjectW_stringOf(input,L"password"));
CkJsonObjectW_UpdateString(body,L"UserAttributes[0].Name",L"email");
CkJsonObjectW_UpdateString(body,L"UserAttributes[0].Value",CkJsonObjectW_stringOf(input,L"email"));
// Add any additional attributes required by your pool before sending.
CkHttpW_SetRequestHeader(http,L"X-Amz-Target",L"AWSCognitoIdentityProviderService.SignUp");
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;
}
if ((CkJsonObjectW_JsonTypeOf(result,L"UserSub") != 1) || (CkJsonObjectW_stringOf(result,L"UserSub") == L"")) {
wprintf(L"Missing or invalid UserSub.\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"User sub: %s\n",CkJsonObjectW_stringOf(result,L"UserSub"));
wprintf(L"Confirmed: %d\n",CkJsonObjectW_BoolOf(result,L"UserConfirmed"));
wprintf(L"Delivery medium: %s\n",CkJsonObjectW_stringOf(result,L"CodeDeliveryDetails.DeliveryMedium"));
}
if (action == L"confirm") {
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;
}
CkJsonObjectW_UpdateString(body,L"ConfirmationCode",CkJsonObjectW_stringOf(input,L"code"));
CkJsonObjectW_UpdateBool(body,L"ForceAliasCreation",FALSE);
CkHttpW_SetRequestHeader(http,L"X-Amz-Target",L"AWSCognitoIdentityProviderService.ConfirmSignUp");
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"Registration confirmed. The user can now sign in.\n");
}
if (action == L"resend") {
CkHttpW_SetRequestHeader(http,L"X-Amz-Target",L"AWSCognitoIdentityProviderService.ResendConfirmationCode");
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;
}
wprintf(L"Confirmation code requested; use the most recent code.\n");
}
CkJsonObjectW_Dispose(app);
CkStringBuilderW_Dispose(endpoint);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(body);
CkJsonObjectW_Dispose(result);
CkJsonObjectW_Dispose(input);
}