Search Unity

Using REST api with HttpWebRequest to an Azure Blob store

Discussion in 'Scripting' started by mhobbs_GTDC, Feb 10, 2014.

  1. mhobbs_GTDC

    mhobbs_GTDC

    Joined:
    Feb 6, 2014
    Posts:
    6
    Hi All,

    I've been pulling my hair out over this for a few days now and I feel I'm close.

    I am trying to interact with an Azure Blob storage using the REST api via HttpWebRequest in Unity3D. My hope is that this will be bare bones enough to re-use on basically any platform but I'm stuck at getting it to work on my Win8.1 machine just in the free Unity3D editor.

    I had initially been getting issue trying to authenticate with the server but I think I've managed to resolve the authentication header using sharekeylite but now I'm getting a Tlsexception.

    When I run the code at the bottom I currently get the following error on line 54:
    Code (csharp):
    1. (HttpWebResponse)request.GetResponse()
    I looked this up and it seemed to be related to my azure storage missing a crossdomain.xml so I created one with utf-8 formatting and put it in a storage container called "$root" on my blob server with the following content (for testing - although I'm not sure what I should have in there for production?):

    Code (csharp):
    1.     <?xml version="1.0" ?>
    2.     <cross-domain-policy>
    3.     <allow-access-from domain="*" />
    4.     </cross-domain-policy>

    This is the complete code I'm testing with at the moment (security details altered):

    Code (csharp):
    1. using UnityEngine;
    2. using System.Security.Cryptography;
    3. using System.Xml.Linq;
    4. using System.Globalization;
    5. using System.Collections;
    6. using System.Text;
    7. using System.Net;
    8. using System.IO;
    9. using System;
    10.  
    11. public class DatabaseClient : MonoBehaviour {
    12.  
    13.     string accessKey = "myAccessKeyFromAzureStorageManageAccessKey==";
    14.     string accountName = "accountName";
    15.     string container = "test"; //Storage container created on azure portal called "test"
    16.  
    17.     // Use this for initialization
    18.     void Start () {
    19.         GetBlob_Test();
    20.     }
    21.    
    22.     // Update is called once per frame
    23.     void Update () {
    24.    
    25.     }
    26.  
    27.     // GetBlob_Test
    28.     void GetBlob_Test()
    29.     {
    30.         Debug.Log ("Attempting to GET from server");
    31.         DateTime dt = DateTime.UtcNow;
    32.  
    33.         string stringToSign = String.Format("GET\n"
    34.                                             + "\n" // content md5
    35.                                             + "\n" // content type
    36.                                             + "x-ms-date:" + dt.ToString("R") + "\nx-ms-version:2012-02-12\n" // headers
    37.                                             + "/{0}/{1}\ncomp:list\nrestype:container", accountName, container);
    38.  
    39.         string authorizationKey = SignThis(stringToSign, accessKey, accountName);
    40.         string method = "GET";
    41.         string urlPath = string.Format("https://{0}.blob.core.windows.net/{1}?restype=container&comp=list", accountName, container);
    42.         Uri uriTest = new Uri(urlPath);
    43.  
    44.         HttpWebRequest request = (HttpWebRequest)WebRequest.Create (uriTest);
    45.         request.Method = method;
    46.         request.Headers.Add("x-ms-date", dt.ToString("R"));
    47.         request.Headers.Add("x-ms-version", "2012-02-12");
    48.         request.Headers.Add("Authorization", authorizationKey);
    49.  
    50.         Debug.Log ("Authorization: " + authorizationKey);
    51.  
    52.         using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    53.         {
    54.             Debug.Log("Response = " + response);
    55.         }
    56.     }
    57.  
    58.     private static String SignThis(String StringToSign, string Key, string Account)
    59.     {
    60.         String signature = string.Empty;
    61.         byte[] unicodeKey = Convert.FromBase64String(Key);
    62.         using (HMACSHA256 hmacSha256 = new HMACSHA256(unicodeKey))
    63.         {
    64.             Byte[] dataToHmac = System.Text.Encoding.UTF8.GetBytes(StringToSign);
    65.             signature = Convert.ToBase64String(hmacSha256.ComputeHash(dataToHmac));
    66.         }
    67.        
    68.         String authorizationHeader = String.Format(
    69.             CultureInfo.InvariantCulture,
    70.             "{0} {1}:{2}",
    71.             "SharedKeyLite",
    72.             Account,
    73.             signature);
    74.        
    75.         return authorizationHeader;
    76.     }
    77. }
    Any help would be much appreciated.

    Thanks,
    Hobsie
     
  2. mhobbs_GTDC

    mhobbs_GTDC

    Joined:
    Feb 6, 2014
    Posts:
    6
    Turns out that the web address was slightly wrong (https instead of http).

    That brings me back to the server returning error: 403



    Edit:

    This issue was eventually resolved. Turns out it was simply a malformed verification header.

    I ended up using Fiddler to intercept the calls made by Unity to the server and looked at the error message which showed what it was expecting the verification header to be created as versus what it was actually given.
     
    Last edited: Feb 11, 2014
  3. angelsin

    angelsin

    Joined:
    Dec 29, 2012
    Posts:
    19
    I am wondering what was wrong with the header? I am trying to do this also and cant figure it out
     
  4. stephane-lallee

    stephane-lallee

    Joined:
    Feb 13, 2013
    Posts:
    10
    I am also interested... I am getting the same error. Impossible to figure out why.
    The same codes runs fine out of Unity from Visual Studio. I suppose it is something related to Mono ?
    Any help is welcomed...
     
  5. kor

    kor

    Joined:
    Oct 29, 2009
    Posts:
    122
    Last edited: May 29, 2016
    Sryall812 and stephane-lallee like this.
  6. stephane-lallee

    stephane-lallee

    Joined:
    Feb 13, 2013
    Posts:
    10
    Thanks @kor for the edit and the solution link :)
     
  7. deadlyfingers

    deadlyfingers

    Joined:
    Oct 15, 2014
    Posts:
    2
  8. Carterryan1990

    Carterryan1990

    Joined:
    Dec 29, 2016
    Posts:
    79
unityunity