0

I have to store at least 3 user credentials(username, password) into the DynamoDB table, but I cannot find how to create and store multiple user credentials with this code. I created just one credential using this code and my goal is to add multiple credentials into the same table that I created. Thank you in advance.

public async void InsertItem()
        {
            PutItemRequest request = new PutItemRequest
            {
                TableName = tableName,
                Item = new Dictionary<string, AttributeValue>
                {
                    {"Username", new AttributeValue{S="[email protected]"} },
                    {"Password", new AttributeValue{S="1234"} }
                }
            };

            try
            {
                var response = await client.PutItemAsync(request);
                if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) MessageBox.Show("Item added successfully!");
            }
            catch (InternalServerErrorException iee)
            {
                MessageBox.Show("An error occurred on the server side" + iee.Message);
            }
            catch (ResourceNotFoundException ex)
            {
                MessageBox.Show("The operation tried to access a nonexistent table or index.");
            }
        }

        
        public async void GetItem()
        {
            GetItemRequest request = new GetItemRequest
            {
                TableName = tableName,
                Key = new Dictionary<string, AttributeValue>
                {
                    {"Username", new AttributeValue{S="[email protected]"} },
                    {"Password", new AttributeValue{S="1234"} }
                }
            };
            try
            {
                var response = await client.GetItemAsync(request);
                if(response.HttpStatusCode == System.Net.HttpStatusCode.OK)
                {
                    if (response.Item.Count > 0)
                    {
                        MessageBox.Show("Item(s) retrieved successfully");
                        foreach (var item in response.Item) ;
                            
                    }
                }
            }
            catch (InternalServerErrorException iee)
            {
                MessageBox.Show("An error occurred on the server side" + iee.Message);
            }
            catch (ResourceNotFoundException ex)
            {
                MessageBox.Show("The operation tried to access a nonexistent table or index.");
            }
        }
        

1 Answer 1

1

Before anything, if this is for a demo project it's okay but do not store any unhashed passwords anytime, anywhere for anyone in your database - security of user data is #1 priority.

Refer to OWASP's Password Storage cheat sheet for more information.


Change InsertItem() and GetItem() to take in 2 string parameters for username and password, which will allow you to reuse the method.

Also change the hardcoded username & password values in the Dictionary<string, AttributeValue> to use your parameters:

public async void InsertItem(string username, string password)
PutItemRequest request = new PutItemRequest
{
    TableName = tableName,
    Item = new Dictionary<string, AttributeValue> {
      { "Username", new AttributeValue { S = username } },
      { "Password", new AttributeValue { S = password } }
    }
};
...

Then call the InsertItem() multiple times with different usernames & passwords to insert multiple items.


If you're just adding 3 users, this is fine but if you are constantly doing batch writes to the database, take a look at the BatchWriteItem method which will be faster.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, it is just for the demo, thanks for your answer!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.