コード例 #1
0
        public virtual async Task <AuthEventEnum> RefreshUserDetailsAsync()
        {
            if (CognitoUser == null)
            {
                return(AuthEventEnum.Alert_NeedToBeSignedIn);
            }

            try
            {
                // Get the current user attributes from the server
                // and set UserEmail and IsUserEmailVerified
                GetUserResponse getUserResponse = await CognitoUser.GetUserDetailsAsync().ConfigureAwait(false);

                foreach (AttributeType item in getUserResponse.UserAttributes)
                {
                    if (item.Name.Equals("email"))
                    {
                        email = item.Value;
                    }

                    if (item.Name.Equals("email_verified"))
                    {
                        IsEmailVerified = item.Value.Equals("true");
                    }
                }
                return(AuthEventEnum.Alert_RefreshUserDetailsDone);
            }
            catch (Exception e)
            {
                Debug.WriteLine($"RefreshUserDetails threw an exception {e}");
                return(AuthEventEnum.Alert_Unknown);
            }
        }
コード例 #2
0
        public async Task <bool> Connected()
        {
            try
            {
                GetUserResponse res = await User.GetUserDetailsAsync();

                if (res != null)
                {
                    Debug.Log("Connection is alive");
                    return(true);
                }
            }
            catch (NotAuthorizedException e)
            {
                Debug.Log("Authentication Error");
                Debug.Log(e);
            }
            return(false);
        }
コード例 #3
0
        //internal async Task<CognitoUser> ResetPassword(string username)
        //{
        //    AmazonCognitoIdentityProviderClient provider = new AmazonCognitoIdentityProviderClient(new Amazon.Runtime.AnonymousAWSCredentials());

        //    CognitoUserPool userPool = new CognitoUserPool(this.POOL_ID, this.CLIENTAPP_ID, provider);

        //    CognitoUser user = new CognitoUser(username, this.CLIENTAPP_ID, userPool, provider);
        //    await user.ForgotPasswordAsync();
        //    return user;
        //}

        //internal async Task<CognitoUser> UpdatePassword(string username, string code, string newpassword)
        //{
        //    AmazonCognitoIdentityProviderClient provider = new AmazonCognitoIdentityProviderClient(new Amazon.Runtime.AnonymousAWSCredentials());

        //    CognitoUserPool userPool = new CognitoUserPool(this.POOL_ID, this.CLIENTAPP_ID, provider);

        //    CognitoUser user = new CognitoUser(username, this.CLIENTAPP_ID, userPool, provider);
        //    await user.ConfirmForgotPasswordAsync(code, newpassword);
        //    return user;
        //}

        public async Task ValidateUser(string username, string password)
        {
            try
            {
                AmazonCognitoIdentityProviderClient provider = new AmazonCognitoIdentityProviderClient(new Amazon.Runtime.AnonymousAWSCredentials(), RegionEndpoint.EUCentral1);
                CognitoUserPool userPool = new CognitoUserPool(this.USERPOOL_ID, this.CLIENTAPP_ID, provider);
                CognitoUser     user     = new CognitoUser(username, this.CLIENTAPP_ID, userPool, provider);

                InitiateSrpAuthRequest authRequest = new InitiateSrpAuthRequest();
                authRequest.Password = password;

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

                if (authFlowResponse.AuthenticationResult == null)
                {
                    throw new Exception("Cognito authentication error");
                }

                GetUserResponse userDetails = await user.GetUserDetailsAsync();

                string idtoken = authFlowResponse.AuthenticationResult.IdToken;

                //CognitoAWSCredentials creds = user.GetCognitoAWSCredentials(this.IDENTITYPOOL_ID, RegionEndpoint.EUCentral1);
                CognitoAWSCredentials creds = new CognitoAWSCredentials(this.IDENTITYPOOL_ID, RegionEndpoint.EUCentral1);
                creds.Clear();
                //creds.CurrentLoginProviders.SetValue(idtoken, 0);
                //creds.CurrentLoginProviders.SetValue(idtoken, 1);
                creds.AddLogin("cognito-idp." + RegionEndpoint.EUCentral1.SystemName + ".amazonaws.com/" + this.USERPOOL_ID, idtoken);

                UserInfo.Clear();
                UserInfo.Credentials = creds;
                UserInfo.UserId      = userDetails.UserAttributes.Find(x => x.Name.ToLower() == "sub").Value;
                UserInfo.UserName    = user.Username;
                UserInfo.Email       = userDetails.UserAttributes.Find(x => x.Name.ToLower() == "email").Value;
                UserInfo.Picture     = "userphoto";
            }
            catch (Exception ex)
            {
                Console.WriteLine("ValidateUser ERROR: " + ex.Message);
                throw ex;
            }
        }