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 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
-- App settings are trusted configuration. Keep a separate account_key/cache per local user.
DECLARE @app int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @app OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
EXEC sp_OASetProperty @app, 'Strict', 1
EXEC sp_OAMethod @app, 'LoadFile', @success OUT, 'qa_data/cognito/app.json'
IF @success <> 1
BEGIN
PRINT 'Could not load Cognito app settings.'
EXEC @hr = sp_OADestroy @app
RETURN
END
EXEC sp_OAMethod @app, 'JsonTypeOf', @iTmp0 OUT, 'region'
EXEC sp_OAMethod @app, 'StringOf', @sTmp0 OUT, 'region'
IF (@iTmp0 <> 1) or (@sTmp0 = '')
BEGIN
PRINT 'Missing or invalid region.'
EXEC @hr = sp_OADestroy @app
RETURN
END
EXEC sp_OAMethod @app, 'JsonTypeOf', @iTmp0 OUT, 'user_pool_id'
EXEC sp_OAMethod @app, 'StringOf', @sTmp0 OUT, 'user_pool_id'
IF (@iTmp0 <> 1) or (@sTmp0 = '')
BEGIN
PRINT 'Missing or invalid user_pool_id.'
EXEC @hr = sp_OADestroy @app
RETURN
END
EXEC sp_OAMethod @app, 'JsonTypeOf', @iTmp0 OUT, 'client_id'
EXEC sp_OAMethod @app, 'StringOf', @sTmp0 OUT, 'client_id'
IF (@iTmp0 <> 1) or (@sTmp0 = '')
BEGIN
PRINT 'Missing or invalid client_id.'
EXEC @hr = sp_OADestroy @app
RETURN
END
EXEC sp_OAMethod @app, 'JsonTypeOf', @iTmp0 OUT, 'account_key'
EXEC sp_OAMethod @app, 'StringOf', @sTmp0 OUT, 'account_key'
IF (@iTmp0 <> 1) or (@sTmp0 = '')
BEGIN
PRINT 'Missing or invalid account_key.'
EXEC @hr = sp_OADestroy @app
RETURN
END
DECLARE @clientId nvarchar(4000)
EXEC sp_OAMethod @app, 'StringOf', @clientId OUT, 'client_id'
DECLARE @endpoint int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @endpoint OUT
EXEC sp_OAMethod @endpoint, 'Append', @success OUT, 'https://cognito-idp.'
EXEC sp_OAMethod @app, 'StringOf', @sTmp0 OUT, 'region'
EXEC sp_OAMethod @endpoint, 'Append', @success OUT, @sTmp0
EXEC sp_OAMethod @endpoint, 'Append', @success OUT, '.amazonaws.com/'
DECLARE @http int
EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
EXEC sp_OASetProperty @http, 'RequireSslCertVerify', 1
EXEC sp_OASetProperty @http, 'FollowRedirects', 0
EXEC sp_OASetProperty @http, 'ConnectTimeout', 20
EXEC sp_OASetProperty @http, 'ReadTimeout', 60
DECLARE @resp int
EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT
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 @action nvarchar(4000)
SELECT @action = 'request'
IF (@action <> 'request') and (@action <> 'confirm')
BEGIN
PRINT 'Choose request or confirm.'
EXEC @hr = sp_OADestroy @app
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
RETURN
END
DECLARE @input int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @input OUT
EXEC sp_OASetProperty @input, 'Strict', 1
EXEC sp_OAMethod @input, 'LoadFile', @success OUT, 'qa_data/cognito/reset-input.json'
IF @success <> 1
BEGIN
PRINT 'Could not load qa_data/cognito/reset-input.json.'
EXEC @hr = sp_OADestroy @app
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @input
RETURN
END
EXEC sp_OAMethod @input, 'JsonTypeOf', @iTmp0 OUT, 'username'
EXEC sp_OAMethod @input, 'StringOf', @sTmp0 OUT, 'username'
IF (@iTmp0 <> 1) or (@sTmp0 = '')
BEGIN
PRINT 'Missing or invalid username.'
EXEC @hr = sp_OADestroy @app
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @input
RETURN
END
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'ClientId', @clientId
EXEC sp_OAMethod @input, 'StringOf', @sTmp0 OUT, 'username'
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'Username', @sTmp0
IF @action = 'request'
BEGIN
EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'X-Amz-Target', 'AWSCognitoIdentityProviderService.ForgotPassword'
EXEC sp_OAMethod @endpoint, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @http, 'HttpJson', @success OUT, 'POST', @sTmp0, @body, 'application/x-amz-json-1.0', @resp
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
IF @iTmp0 <> 200
BEGIN
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
PRINT 'HTTP status: ' + @iTmp0
EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
EXEC sp_OAMethod @result, 'Load', @success OUT, @sTmp0
IF @success = 1
BEGIN
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, '__type'
PRINT 'Error type: ' + @sTmp0
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'error'
PRINT 'OAuth error: ' + @sTmp0
END
-- Inspect detailed diagnostics privately. Do not log passwords, sessions, or tokens.
EXEC @hr = sp_OADestroy @app
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @input
RETURN
END
IF @success <> 1
BEGIN
PRINT 'The HTTP exchange did not complete.'
EXEC @hr = sp_OADestroy @app
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @input
RETURN
END
EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
EXEC sp_OAMethod @result, 'Load', @success OUT, @sTmp0
IF @success <> 1
BEGIN
PRINT 'Expected a JSON response.'
EXEC @hr = sp_OADestroy @app
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @input
RETURN
END
-- A public UI should not disclose whether an account exists.
PRINT 'If the account is eligible, a recovery code will be sent.'
END
ELSE
BEGIN
EXEC sp_OAMethod @input, 'JsonTypeOf', @iTmp0 OUT, 'code'
EXEC sp_OAMethod @input, 'StringOf', @sTmp0 OUT, 'code'
IF (@iTmp0 <> 1) or (@sTmp0 = '')
BEGIN
PRINT 'Missing or invalid code.'
EXEC @hr = sp_OADestroy @app
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @input
RETURN
END
EXEC sp_OAMethod @input, 'JsonTypeOf', @iTmp0 OUT, 'new_password'
EXEC sp_OAMethod @input, 'StringOf', @sTmp0 OUT, 'new_password'
IF (@iTmp0 <> 1) or (@sTmp0 = '')
BEGIN
PRINT 'Missing or invalid new_password.'
EXEC @hr = sp_OADestroy @app
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @input
RETURN
END
EXEC sp_OAMethod @input, 'StringOf', @sTmp0 OUT, 'code'
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'ConfirmationCode', @sTmp0
EXEC sp_OAMethod @input, 'StringOf', @sTmp0 OUT, 'new_password'
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'Password', @sTmp0
EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'X-Amz-Target', 'AWSCognitoIdentityProviderService.ConfirmForgotPassword'
EXEC sp_OAMethod @endpoint, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @http, 'HttpJson', @success OUT, 'POST', @sTmp0, @body, 'application/x-amz-json-1.0', @resp
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
IF @iTmp0 <> 200
BEGIN
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
PRINT 'HTTP status: ' + @iTmp0
EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
EXEC sp_OAMethod @result, 'Load', @success OUT, @sTmp0
IF @success = 1
BEGIN
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, '__type'
PRINT 'Error type: ' + @sTmp0
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'error'
PRINT 'OAuth error: ' + @sTmp0
END
-- Inspect detailed diagnostics privately. Do not log passwords, sessions, or tokens.
EXEC @hr = sp_OADestroy @app
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @input
RETURN
END
IF @success <> 1
BEGIN
PRINT 'The HTTP exchange did not complete.'
EXEC @hr = sp_OADestroy @app
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @input
RETURN
END
PRINT 'Password reset accepted. Sign in with the new password.'
END
EXEC @hr = sp_OADestroy @app
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @input
END
GO