0

I want to access data from azure table storage ,but i couldn't access using Net core .But it's possible using .Net Framework.

Below code was written in .Net Framework

var sharedKey = Convert.FromBase64String("AccountKey");
        var request = WebRequest.Create("http://accountname.table.core.windows.net/tablename");
        request.ContentLength = 0;
        request.Headers.Add("x-ms-date", DateTime.UtcNow.ToString("R", CultureInfo.InvariantCulture));
        var resource = request.RequestUri.PathAndQuery;
        if (resource.Contains("?"))
        {
            resource = resource.Substring(0, resource.IndexOf("?"));
        }

        string stringToSign = string.Format("{0}\n/{1}{2}",
                request.Headers["x-ms-date"],
                 "accountname",
                resource
            );
        var hasher = new HMACSHA256(sharedKey);
        string signedSignature = Convert.ToBase64String(hasher.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)));
        string authorizationHeader = string.Format("{0} {1}:{2}", "SharedKeyLite", "accountname", signedSignature);
        request.Headers.Add("Authorization", authorizationHeader);
        var response = request.GetResponse();
        return response;
2

1 Answer 1

3

As far as I know, there are some differences between .Net Core and .Net Standard when using REST API.

  1. In .NET 4.5 there are some properties you have to set on the HttpWebRequest object and you can’t just set them in the headers. Well, in core they decided to reverse course and you have to use the header collection.

    .NET Core: request.Headers["x-ms-version"] = "2015-04-05";

    .NET 4.5: request.Headers.Add("x-ms-version", "2015-04-05");

  2. .NET Core only support WebResponse.GetResponseAsync() method, so you could only use the async way to get the result.

    Like below: Task<WebResponse> response = request.GetResponseAsync(); HttpWebResponse responseresult = (HttpWebResponse)response.Result;

More details, you could refer to follow codes:

string storageAccount = "storageAccount";
        string accessKey = "accessKey";
        string resourcePath = "TableSample()";
        string uri = @"https://" + storageAccount + ".table.core.windows.net/" + resourcePath;
        // Web request 
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
        request.Method = "GET";
        request.ContentType = "application/json";
        request.Accept = "application/json;odata=nometadata";
        request.Headers["x-ms-date"] = DateTime.UtcNow.ToString("R", System.Globalization.CultureInfo.InvariantCulture);
        request.Headers["x-ms-version"] = "2015-04-05";
        string stringToSign = request.Headers["x-ms-date"] + "\n"; 
        int query = resourcePath.IndexOf("?");
        if (query > 0)
        {
            resourcePath = resourcePath.Substring(0, query);
        }
        stringToSign += "/" + storageAccount + "/" + resourcePath;
        System.Security.Cryptography.HMACSHA256 hasher = new System.Security.Cryptography.HMACSHA256(Convert.FromBase64String(accessKey));
        string strAuthorization = "SharedKeyLite " + storageAccount + ":" + System.Convert.ToBase64String(hasher.ComputeHash(System.Text.Encoding.UTF8.GetBytes(stringToSign)));


        request.Headers["Authorization"] = strAuthorization;

        Task<WebResponse> response = request.GetResponseAsync();
        HttpWebResponse responseresult = (HttpWebResponse)response.Result;

            using (System.IO.StreamReader r = new System.IO.StreamReader(responseresult.GetResponseStream()))
            {
                string jsonData = r.ReadToEnd();
                Console.WriteLine(jsonData);
           }
        Console.ReadLine();

Result: enter image description here

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

2 Comments

the above code is not working in .Net Core ,because of System.Security.Cryptography.HMACSHA256 reference not found in .Net Core
As far as I know, the .Net Core will automatic installed the package System.Security.Cryptography.Algorithms. I suggest you could follow below way to add refernce: Right click the Application name --> Manage Nuget Packages ---> Search the System.Security.Cryptography.Algorithms and install it.

Your Answer

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