Need some help with AWS Cognito Pre-Authentication lambda trigger to be written in .Net Core 3. I am able to pass the correct attributes as part of 'validationData' attribute in the cognito request. When the validation attribute fails to satisfy the condition, I need an 'UnauthorizedResponse' sent back to my front end. However, when I try to return an error object from my .net core handler, I am ending up in 'InvalidLambdaResponse' exception. Any help or sample code shall help in great regards.

Here is a code snippet from my end -

public object FunctionHandler(JObject request, ILambdaContext context)
    {
        LambdaLogger.Log($"JObject: {request}");
        var input = request;
        var userId = input["userName"];
        LambdaLogger.Log($"Calling function: {context.FunctionName}\\n");
        LambdaLogger.Log($"Input userid: {userId}\\n");
        string strUserId = ((Newtonsoft.Json.Linq.JValue)userId).Value.ToString();
        var status = Util.FetchCognitoUser(strUserId); \\call to fetch user properties
        LambdaLogger.Log($"Pre auth execution complete");
        if (status)
        {
            return request;
        }
        else
        {
            request["response"]["statuscode"] = "400";
            request["response"]["statusmessage"] = "NotAuthorizedException";
            return request;
        }
    }
1

There are 1 best solutions below

0
On

Some trial and errors finally helped me. It is not as difficult as it looked. You achieve the needed functionality by simply throwing an exception. :)

I simply threw an exception back from the else part of the sample code posted above. And I was able to capture 'PreAuth failed exception'

else
    {
        throw new AmazonCognitoIdentityException("PreAuthentication Failure");
    }