Exemple #1
0
        public static CognitoUser ValidateUser(string username)
        {
            var provider = new AmazonCognitoIdentityProviderClient(new Amazon.Runtime.AnonymousAWSCredentials());
            var userPool = new CognitoUserPool(PoolId, ClientAppId, provider, ClientSecret);
            var user     = new CognitoUser(username, ClientAppId, userPool, provider, ClientSecret);

            var initiateAuthRequest = new InitiateCustomAuthRequest
            {
                AuthParameters = new Dictionary <string, string>(StringComparer.Ordinal)
                {
                    {
                        CognitoConstants.ChlgParamUsername,
                        username
                    }
                },
                ClientMetadata = new Dictionary <string, string>()
            };

            if (!string.IsNullOrEmpty(ClientSecret))
            {
                initiateAuthRequest.AuthParameters.Add(CognitoConstants.ChlgParamSecretHash,
                                                       Util.GetUserPoolSecretHash(username, ClientAppId, ClientSecret));
            }

            AuthFlowResponse authResponse = user.StartWithCustomAuthAsync(initiateAuthRequest).ConfigureAwait(false)
                                            .GetAwaiter().GetResult();

            return(authResponse.AuthenticationResult != null ? user : null);
        }
Exemple #2
0
        private async Task <string> GetIdTokenViaCustomAuthAsync(CognitoUser user,
                                                                 InitiateCustomAuthRequest initiateAuthRequest)
        {
            var authFlowResponse = await user.StartWithCustomAuthAsync(initiateAuthRequest)
                                   .ConfigureAwait(false);

            return(authFlowResponse.AuthenticationResult.IdToken);
        }