Amazon Cognito: Update User Attributes as an Administrator
See more Amazon Cognito Examples
Live operation: update an existing user's email with AdminUpdateUserAttributes. Uses IAM administrator authorization, not the user's access token. The user-facing equivalent is UpdateUserAttributes. Use the canonical Username returned by Cognito. Only mutable attributes can be changed; custom attributes must already exist in the schema and use a custom: prefix.
The example intentionally omits email_verified. Setting this attribute to the string "true" overrides verification; do that only when an authorized process has independently verified ownership. When the pool's AttributesRequireVerificationBeforeUpdate includes email, the replacement remains pending until verified; otherwise the change can take effect immediately. Updating an address may send a verification message and affect sign-in/recovery. A 200 response has no attribute payload (normally {}); acceptance is not proof of email verification. An empty attribute value requests deletion where permitted, not an instruction to leave it unchanged.
Uses the Amazon Cognito User Pools regional JSON API with Chilkat.Rest and AuthAws (Signature Version 4, service cognito-idp). It does not use Identity Pools, the OAuth domain, or a Cognito bearer token. Set the region to match the pool; this example uses the standard AWS commercial endpoint. Other partitions have their own endpoint suffixes.
Supply a private qa_data/aws/cognito.json with string fields access_key, secret_key, and, for temporary IAM/STS credentials, session_token. Prefer short-lived role credentials and refresh them before expiry. The example reads this file explicitly; it does not automatically discover an AWS profile or role. The STS session token is sent in the documented X-Amz-Security-Token query parameter and included in the signature; keep request URLs and diagnostics private. Keep credentials outside public web content and source control. IAM administration can run in a controlled backoffice desktop application or a backend; never embed administrative AWS keys in publicly distributed software.
Required IAM action: cognito-idp:AdminUpdateUserAttributes. Scope access to the intended user-pool ARN.
Checks both transport completion and HTTP 200, and validates JSON when response data is expected. AWS error details use __type and message; inspect sensitive diagnostics privately. An HTTP 400 TooManyRequestsException needs bounded exponential backoff with jitter; expired temporary credentials need renewal. Check IAM permission and region for authorization/resource errors. For an uncertain write, inspect the existing resource before retrying. This sample does not automatically retry writes.
AWS AdminUpdateUserAttributes reference
Chilkat SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @iTmp0 int
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
SELECT @success = 0
-- Private configuration, not a file in public web content or source control.
-- Load current IAM credentials; these are NOT Cognito app-client credentials or JWTs.
DECLARE @credentials int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @credentials OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
EXEC sp_OASetProperty @credentials, 'Strict', 1
EXEC sp_OAMethod @credentials, 'LoadFile', @success OUT, 'qa_data/aws/cognito.json'
IF @success <> 1
BEGIN
PRINT 'Could not load private AWS credentials.'
EXEC @hr = sp_OADestroy @credentials
RETURN
END
EXEC sp_OAMethod @credentials, 'JsonTypeOf', @iTmp0 OUT, 'access_key'
EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'access_key'
IF (@iTmp0 <> 1) or (@sTmp0 = '')
BEGIN
PRINT 'Missing or invalid access_key.'
EXEC @hr = sp_OADestroy @credentials
RETURN
END
EXEC sp_OAMethod @credentials, 'JsonTypeOf', @iTmp0 OUT, 'secret_key'
EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'secret_key'
IF (@iTmp0 <> 1) or (@sTmp0 = '')
BEGIN
PRINT 'Missing or invalid secret_key.'
EXEC @hr = sp_OADestroy @credentials
RETURN
END
-- Set this once to the region containing the user pool.
DECLARE @region nvarchar(4000)
SELECT @region = 'us-west-2'
DECLARE @endpoint int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @endpoint OUT
EXEC sp_OAMethod @endpoint, 'Append', @success OUT, 'cognito-idp.'
EXEC sp_OAMethod @endpoint, 'Append', @success OUT, @region
EXEC sp_OAMethod @endpoint, 'Append', @success OUT, '.amazonaws.com'
DECLARE @aws int
EXEC @hr = sp_OACreate 'Chilkat.AuthAws', @aws OUT
EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'access_key'
EXEC sp_OASetProperty @aws, 'AccessKey', @sTmp0
EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'secret_key'
EXEC sp_OASetProperty @aws, 'SecretKey', @sTmp0
EXEC sp_OASetProperty @aws, 'Region', @region
EXEC sp_OASetProperty @aws, 'ServiceName', 'cognito-idp'
EXEC sp_OASetProperty @aws, 'SignatureVersion', 4
DECLARE @rest int
EXEC @hr = sp_OACreate 'Chilkat.Rest', @rest OUT
EXEC sp_OASetProperty @rest, 'ConnectTimeoutMs', 20000
EXEC sp_OASetProperty @rest, 'IdleTimeoutMs', 60000
EXEC sp_OAMethod @rest, 'SetAuthAws', @success OUT, @aws
IF @success <> 1
BEGIN
PRINT 'Could not configure AWS signing.'
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
RETURN
END
-- Temporary IAM/STS credentials require their session token on every signed request.
-- AddQueryParam includes it in SigV4's canonical query. Never log the request URL.
-- Leave session_token absent or empty only for credentials that do not have one.
EXEC sp_OAMethod @credentials, 'HasMember', @iTmp0 OUT, 'session_token'
IF @iTmp0 = 1
BEGIN
EXEC sp_OAMethod @credentials, 'JsonTypeOf', @iTmp0 OUT, 'session_token'
IF @iTmp0 <> 1
BEGIN
PRINT 'session_token must be a string.'
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
RETURN
END
EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'session_token'
IF @sTmp0 <> ''
BEGIN
EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'session_token'
EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'X-Amz-Security-Token', @sTmp0
IF @success <> 1
BEGIN
PRINT 'Could not set the session-token parameter.'
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
RETURN
END
END
END
-- Supply a Socket to explicitly enable TLS certificate verification.
DECLARE @connection int
EXEC @hr = sp_OACreate 'Chilkat.Socket', @connection OUT
EXEC sp_OASetProperty @connection, 'RequireSslCertVerify', 1
EXEC sp_OASetProperty @connection, 'MaxReadIdleMs', 60000
EXEC sp_OASetProperty @connection, 'MaxSendIdleMs', 60000
EXEC sp_OAMethod @endpoint, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @connection, 'Connect', @success OUT, @sTmp0, 443, 1, 20000
IF @success <> 1
BEGIN
PRINT 'TLS connection failed; inspect diagnostics privately.'
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
RETURN
END
EXEC sp_OAMethod @rest, 'UseConnection', @success OUT, @connection, 1
IF @success <> 1
BEGIN
PRINT 'Could not attach the TLS connection.'
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
RETURN
END
EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Content-Type', 'application/x-amz-json-1.0'
IF @success <> 1
BEGIN
PRINT 'Could not set the content type.'
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
RETURN
END
DECLARE @body int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @body OUT
DECLARE @result int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @result OUT
EXEC sp_OASetProperty @result, 'Strict', 1
DECLARE @requestBody int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @requestBody OUT
DECLARE @responseBody int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @responseBody OUT
EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'X-Amz-Target', 'AWSCognitoIdentityProviderService.AdminUpdateUserAttributes'
IF @success <> 1
BEGIN
PRINT 'Could not set the Cognito operation.'
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @requestBody
EXEC @hr = sp_OADestroy @responseBody
RETURN
END
DECLARE @userPoolId nvarchar(4000)
SELECT @userPoolId = 'us-west-2_EXAMPLE'
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'UserPoolId', @userPoolId
-- Use the returned Cognito Username, not an assumed email alias.
DECLARE @username nvarchar(4000)
SELECT @username = 'example-user'
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'Username', @username
-- Live operation: change an existing user's email address.
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'UserAttributes[0].Name', 'email'
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'UserAttributes[0].Value', 'new-address@example.com'
-- Deliberately omit email_verified. Setting it to "true" bypasses verification.
-- A mutable custom attribute uses a name such as custom:department and must exist in the schema.
-- EmitSb appends, so clear the buffer before each page/request.
EXEC sp_OAMethod @requestBody, 'Clear', NULL
EXEC sp_OAMethod @body, 'EmitSb', @success OUT, @requestBody
IF @success <> 1
BEGIN
PRINT 'Could not encode the request.'
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @requestBody
EXEC @hr = sp_OADestroy @responseBody
RETURN
END
-- Chilkat signs this POST, including the current headers and JSON body.
EXEC sp_OAMethod @rest, 'FullRequestSb', @success OUT, 'POST', '/', @requestBody, @responseBody
IF @success <> 1
BEGIN
-- A failed exchange can leave the outcome of a write unknown. Do not blindly retry it.
PRINT 'Request did not complete; inspect LastErrorText privately.'
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @requestBody
EXEC @hr = sp_OADestroy @responseBody
RETURN
END
EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
IF @iTmp0 <> 200
BEGIN
EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
PRINT 'Cognito HTTP status: ' + @iTmp0
-- AWS commonly reports throttling and expired credentials as HTTP 400, not 429/401.
EXEC sp_OAMethod @result, 'LoadSb', @success OUT, @responseBody
IF @success = 1
BEGIN
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, '__type'
PRINT 'AWS error type: ' + @sTmp0
-- Inspect message and response headers privately; they can contain account/user data.
END
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @requestBody
EXEC @hr = sp_OADestroy @responseBody
RETURN
END
-- AWS documents an empty success body and also shows {}. Neither needs parsing.
PRINT 'Attribute update accepted.'
-- If the pool requires verification before update, the user must complete verification
-- before the replacement email becomes active. Accepted does not mean verified.
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @requestBody
EXEC @hr = sp_OADestroy @responseBody
END
GO