예제 #1
0
        public async Task <CognitoContext> SignUp(string userName, string password)
        {
            try
            {
                var result = await CognitoIdentityProviderClient.SignUpAsync(new SignUpRequest
                {
                    ClientId = Constants.COGNITO_CLIENT_ID,
                    Password = password,
                    Username = userName,
                });

                Console.WriteLine("Signed in.");

                return(new CognitoContext(CognitoResult.SignupOk));
            }
            catch (UsernameExistsException)
            {
                return(new CognitoContext(CognitoResult.UserNameAlreadyUsed));
            }
            catch (Amazon.CognitoIdentityProvider.Model.InvalidParameterException)
            {
                return(new CognitoContext(CognitoResult.PasswordRequirementsFailed));
            }
            catch (Exception e)
            {
                Console.WriteLine($"SignUp() threw an exception {e}");
            }
            return(new CognitoContext(CognitoResult.Unknown));
        }
예제 #2
0
        public async Task <SignInContext> SignIn(string userName, string password)
        {
            try
            {
                var         something = CognitoIdentityProviderClient.ToString();
                CognitoUser user      = new CognitoUser(userName, Constants.COGNITO_CLIENT_ID, CognitoUserPool, CognitoIdentityProviderClient);

                AuthFlowResponse context = await user.StartWithSrpAuthAsync(new InitiateSrpAuthRequest()
                {
                    Password = password
                }).ConfigureAwait(false);


                // TODO handle other challenges
                if (context.ChallengeName == ChallengeNameType.NEW_PASSWORD_REQUIRED)
                {
                    return new SignInContext(CognitoResult.PasswordChangeRequred)
                           {
                               //User = user,
                               SessionId = context.SessionID
                           }
                }
                ;
                else
                {
                    return(new SignInContext(CognitoResult.Ok)
                    {
                        User = user,
                        IdToken = context.AuthenticationResult?.IdToken,
                        RefreshToken = context.AuthenticationResult?.RefreshToken,
                        AccessToken = context.AuthenticationResult?.AccessToken,
                        TokenIssued = user.SessionTokens.IssuedTime,
                        Expires = user.SessionTokens.ExpirationTime,
                        SessionId = context.SessionID
                    });
                }
            }
            catch (Amazon.CognitoIdentityProvider.Model.NotAuthorizedException)
            {
                return(new SignInContext(CognitoResult.NotAuthorized));
            }
            catch (UserNotFoundException)
            {
                return(new SignInContext(CognitoResult.UserNotFound));
            }
            catch (UserNotConfirmedException)
            {
                return(new SignInContext(CognitoResult.NotConfirmed));
            }
            catch (Exception e)
            {
                Console.WriteLine($"SignIn() threw an exception {e}");
            }
            return(new SignInContext(CognitoResult.Unknown));
        }
예제 #3
0
        public async Task <CognitoContext> VerifyWithCode(string userName, string code)
        {
            try
            {
                var result = await CognitoIdentityProviderClient.ConfirmSignUpAsync(new ConfirmSignUpRequest
                {
                    ClientId         = Constants.COGNITO_CLIENT_ID,
                    Username         = userName,
                    ConfirmationCode = code
                });

                return(new CognitoContext(CognitoResult.Ok));
            }
            catch (Exception e)
            {
                Console.WriteLine($"VerifyWithCode() threw an exception {e}");
            }
            return(new CognitoContext(CognitoResult.Unknown));
        }