Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to add MFA (Multi Factor Authentication) in my application, where i am going to store object in aws S3 buckets. Please provide any sample code for authenticate the amazon aws service through MFA while uploading the objects/image to s3 bucket using asp.net mvc c#.

Here is the below working code snippet without MFA,

var awsCredentials = new BasicAWSCredentials(accessKey, secretKey);

_client = new AmazonS3Client(awsCredentials, Amazon.RegionEndpoint.USEast1);

var putRequest = new PutObjectRequest
{
BucketName = ConfigurationManager.AppSettings["S3BucketName"],
Key = fileName,
FilePath = localFilePath,
ContentType = "image/" + Path.GetExtension(fileName),
CannedACL = S3CannedACL.PublicRead
};
var req = JsonConvert.SerializeObject(putRequest);
Thanks,

What I have tried:

var awsCredentials = new BasicAWSCredentials(accessKey, secretKey);

_client = new AmazonS3Client(awsCredentials, Amazon.RegionEndpoint.USEast1);

var putRequest = new PutObjectRequest
{
BucketName = ConfigurationManager.AppSettings["S3BucketName"],
Key = fileName,
FilePath = localFilePath,
ContentType = "image/" + Path.GetExtension(fileName),
CannedACL = S3CannedACL.PublicRead
};
var req = JsonConvert.SerializeObject(putRequest);
Posted
Comments
David_Wimbley 23-Mar-19 3:25am    
What have you tried? What is your issue? Are you just asking someone to write multi factor authentication integration for you? A simple google search on using say, google mfa would yield plenty of results to get started.
RakeshAlajipur 26-Mar-19 5:58am    
Hi, I have tried using above code by enabling the MFA in console, but through code how to send MFA authentication key using C#, as am getting access error while running the above code.
David_Wimbley 26-Mar-19 19:39pm    
Have you tried searching on google? how to use MFA to authenticate to AWS S3 returns a ton of results.

One of the first links looks to be exactly what you are looking for.

https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_mfa_configure-api-require.html

What i am trying to do is encourage you to think for yourself rather than relying on others to solve your problems for you. From the way it looks, you tried one thing (if that) and because it didn't work. Now you are asking me to show you code on how to do it in C#.

Another link

https://aws.amazon.com/blogs/security/how-to-enable-mfa-protection-on-your-aws-api-calls/
RakeshAlajipur 27-Mar-19 3:19am    
Hi David, Thanks for response. The shared links contains CLI, actually i wanted to do from server side to save the images in S3 bucket with MFA.

I have tried with GetSessionToken() method, from this i am getting sessionToken credentials which iam passing as paremeter while creating AmazonS3Client object, this object am using to upload image to S3 bucket. Please refer my below code here, Please advice if this code is sufficient enough to pass MFA programmatically.

var client = new AmazonSecurityTokenServiceClient(new BasicAWSCredentials("AK", "SK"),Amazon.RegionEndpoint.USEast1);

var sessionTokenResult = client.GetSessionToken(); Credentials sessionCredentials = sessionTokenResult.Credentials; AmazonS3Client s3Client = new AmazonS3Client(sessionCredentials, Amazon.RegionEndpoint.USEast1);

var putRequest = new PutObjectRequest { //Parameters };
var response = s3Client.PutObject(putRequest)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900