AnsweredAssumed Answered

HS3 API for DotNet applications using Network credentials(Windows Auth)

Question asked by Praveen b KP on May 29, 2018

I created a sample app using Amazon S3 nuget package to access HCP as shown below.

 

using Amazon.S3;
using Amazon.S3.Model;
using System;
using System.Net;
using System.Security.Cryptography;
using System.Text;

namespace HS3_Normal
{
    class Program
    {
        private static string accessKeyId = string.Empty;
        private static string secretAccessKey = string.Empty;
        private static string bucketName = "s3-test";

        static void Main(string[] args)
        {
            var config = new AmazonS3Config();
            config.ServiceURL = "https://abc.def.ghi.net";
            config.UseHttp = false;
           
            MD5 md = System.Security.Cryptography.MD5.Create();
            accessKeyId = Convert.ToBase64String(Encoding.UTF8.GetBytes("username"));
            secretAccessKey = BitConverter.ToString(md.ComputeHash(Encoding.UTF8.GetBytes("password"))).Replace("-", String.Empty).ToLower();

            try
            {
                using (var client = new AmazonS3Client(accessKeyId, secretAccessKey, config))
                {
                    Console.WriteLine("client initialized");

                    ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
                    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

                    ListObjectsRequest lstreq = new ListObjectsRequest
                    {
                        BucketName = bucketName,
                        MaxKeys = 2
                    };

                    do
                    {
                        var response = client.ListObjectsAsync(lstreq).Result;

                        foreach (var obj in response.S3Objects)
                        {
                            Console.WriteLine($"key {obj.Key} size {obj.Size}");
                        }

                        if (response.IsTruncated)
                        {
                            lstreq.Marker = response.NextMarker;
                        }
                        else
                        {
                            lstreq = null;
                        }
                    } while (lstreq != null);
                }
            }
            catch (AmazonS3Exception e)
            {
                Console.WriteLine(e);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            Console.ReadLine();
        }
    }
}

 

This program works as expected. I'm passing HCP user creds to Amazon S3 client to Authenticate.

 

I had earlier created a program that used the REST API endpoint, the client used was HttpClient of System.Net.Http, i passed the sytem logged credentials(Windows Auth) to the client object as shown below. I was able to access storage objects using this client as the system logged cred was a AD group cred and this AD group was added in HCP tenant portal Security > Groups access to the Namespaces.

 

    HttpClient _client = new HttpClient(new HttpClientHandler { Credentials = CredentialCache.DefaultNetworkCredentials });

 

My questions is how do i pass same creds to the Amazon S3 client object. If so, a sample of it would be helpful.

Outcomes