コード例 #1
0
        public static async void GetCredsChallengesAsync()
        {
            AmazonCognitoIdentityProviderClient provider =
                new AmazonCognitoIdentityProviderClient(new Amazon.Runtime.AnonymousAWSCredentials());
            CognitoUserPool        userPool    = new CognitoUserPool("poolID", "clientID", provider);
            CognitoUser            user        = new CognitoUser("username", "clientID", userPool, provider);
            InitiateSrpAuthRequest authRequest = new InitiateSrpAuthRequest()
            {
                Password = "******"
            };

            AuthFlowResponse authResponse = await user.StartWithSrpAuthAsync(authRequest).ConfigureAwait(false);

            while (authResponse.AuthenticationResult == null)
            {
                if (authResponse.ChallengeName == ChallengeNameType.NEW_PASSWORD_REQUIRED)
                {
                    Console.WriteLine("Enter your desired new password:"******"Enter the MFA Code sent to your device:");
                    string mfaCode = Console.ReadLine();

                    AuthFlowResponse mfaResponse = await user.RespondToSmsMfaAuthAsync(new RespondToSmsMfaRequest()
                    {
                        SessionID = authResponse.SessionID,
                        MfaCode   = mfaCode
                    }).ConfigureAwait(false);

                    accessToken = authResponse.AuthenticationResult.AccessToken;
                }
                else
                {
                    Console.WriteLine("Unrecognized authentication challenge.");
                    accessToken = "";
                    break;
                }
            }

            if (authResponse.AuthenticationResult != null)
            {
                Console.WriteLine("User successfully authenticated.");
            }
            else
            {
                Console.WriteLine("Error in authentication process.");
            }
        }
コード例 #2
0
        public virtual async Task <AuthEventEnum> VerifyCodeAsync(string code)
        {
            if (CurrentAuthProcess == AuthProcessEnum.None)
            {
                return(AuthEventEnum.Alert_NoActiveAuthProcess);
            }

            if (CurrentChallenge != AuthChallengeEnum.Code)
            {
                return(AuthEventEnum.Alert_VerifyCalledButNoChallengeFound);
            }

            try
            {
                switch (CurrentAuthProcess)
                {
                case AuthProcessEnum.None:
                    return(AuthEventEnum.Alert_InternalProcessError);

                case AuthProcessEnum.ResettingPassword:
                    await CognitoUser.ConfirmForgotPasswordAsync(code, newPassword).ConfigureAwait(false);

                    AuthChallengeList.Remove(AuthChallengeEnum.Code);
                    return(await NextChallenge());

                case AuthProcessEnum.SigningUp:
                    var result = await providerClient.ConfirmSignUpAsync(
                        new ConfirmSignUpRequest
                    {
                        ClientId         = clientId,
                        Username         = login,
                        ConfirmationCode = code
                    }).ConfigureAwait(false);

                    IsCodeVerified = true;
                    AuthChallengeList.Remove(AuthChallengeEnum.Code);
                    return(await NextChallenge());

                case AuthProcessEnum.SigningIn:
                    if (authFlowResponse == null)     // authFlowResponse set during VerifyPassword
                    {
                        return(AuthEventEnum.Alert_InternalSignInError);
                    }

                    authFlowResponse = await CognitoUser.RespondToSmsMfaAuthAsync(
                        new RespondToSmsMfaRequest()
                    {
                        SessionID = authFlowResponse.SessionID,
                        MfaCode   = code
                    }
                        ).ConfigureAwait(false);

                    AuthChallengeList.Remove(AuthChallengeEnum.Code);
                    return(await NextChallenge());

                case AuthProcessEnum.UpdatingEmail:
                    await CognitoUser.VerifyAttributeAsync("email", code).ConfigureAwait(false);

                    IsCodeVerified = true;
                    AuthChallengeList.Remove(AuthChallengeEnum.Code);
                    return(await NextChallenge());

                case AuthProcessEnum.UpdatingPhone:
                    return(AuthEventEnum.Alert_InternalProcessError);

                default:
                    return(AuthEventEnum.Alert_InternalProcessError);
                }
            }
            catch (InvalidPasswordException) { return(AuthEventEnum.Alert_PasswordFormatRequirementsFailed); }
            catch (TooManyRequestsException) { return(AuthEventEnum.Alert_TooManyAttempts); }
            catch (TooManyFailedAttemptsException) { return(AuthEventEnum.Alert_TooManyAttempts); }
            catch (NotAuthorizedException) { return(AuthEventEnum.Alert_NotAuthorized); }
            catch (UserNotFoundException) { return(AuthEventEnum.Alert_UserNotFound); }
            catch (UserNotConfirmedException) { return(AuthEventEnum.Alert_NotConfirmed); }
            catch (CodeMismatchException) { return(AuthEventEnum.Alert_VerifyFailed); }
            catch (AliasExistsException) { return(AuthEventEnum.Alert_AccountWithThatEmailAlreadyExists); }
            catch (Exception e)
            {
                Debug.WriteLine($"VerifyCode() threw an exception {e}");
                CognitoUser = null;
                return(AuthEventEnum.Alert_Unknown);
            }
        }