예제 #1
0
        public void TestSignatureGetWithContentType()
        {
            req.RequestUri = new System.Uri("https://pocdlgen2.dfs.core.windows.net/root?directory=%2F&recursive=false&resource=filesystem&timeout=60");
            req.Method     = HttpMethod.Get;
            //req.Headers.IfNoneMatch.Add(EntityTagHeaderValue.Any);
            req.Headers.Add(Constants.HEADER_CLIENT_REQUEST_ID, "9e18c0a0-b377-410d-8536-bb04df6df016");
            req.Headers.Add(Constants.HEADER_VERSION, "2018-11-09");
            req.Headers.Add(Constants.HEADER_DATE, "Wed, 03 Jul 2019 17:59:01 GMT");
            req.Content = new StringContent("", Encoding.UTF8, "application/json");
            string expected = "pocdlgen2:jK/nDdlhBjNhIGQMQ73P3t10CSIZnPBIPEYb4Dlmec0=";

            Assert.AreEqual(
                expected,
                SASHelper.GetAuthorizationHeader(req, ACCOUNTNAME, ACCOUNTKEY)
                );
        }
예제 #2
0
        public void TestPutSignature()
        {
            req.RequestUri = new System.Uri("https://pocdlgen2.dfs.core.windows.net/root/test2.txt?action=append&position=0&timeout=901");
            req.Method     = HttpMethod.Put;
            req.Headers.Add(Constants.HEADER_CLIENT_REQUEST_ID, "f65e7182-4ee2-4ba5-47b7-65bd5a88ddf2");
            req.Headers.Add(Constants.HEADER_VERSION, "2018-11-09");
            req.Headers.Add(Constants.HEADER_DATE, "Wed, 03 Jul 2019 18:10:45 GMT");
            req.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));
            req.Content = new StringContent("");
            req.Content.Headers.ContentLength = 3125;
            req.Content.Headers.ContentType   = null;
            string expected = "pocdlgen2:CF/+ZvLYvSOy1oMtPt9r5Qu2H5Ob/OX8X6E5JU4vaTc=";

            Assert.AreEqual(
                expected,
                SASHelper.GetAuthorizationHeader(req, ACCOUNTNAME, ACCOUNTKEY)
                );
        }
        public async Task <HttpResponseMessage> Send(HttpMethod method, string path,
                                                     Dictionary <string, string> queryParams = null,
                                                     Dictionary <string, string> headers     = null,
                                                     byte[] content     = null,
                                                     string contentType = null)
        {
            if (headers == null)
            {
                headers = new Dictionary <string, string>();
            }
            client.DefaultRequestHeaders.Accept.Clear();
            var req = new HttpRequestMessage(method, buildURL(path, queryParams));

            DictionnaryHelper.AddDefaultValue(headers, Constants.HEADER_VERSION, Constants.VERSION);
            DictionnaryHelper.AddDefaultValue(headers, Constants.HEADER_DATE, DateTime.UtcNow.ToString("r", CultureInfo.InvariantCulture));
            DictionnaryHelper.AddDefaultValue(headers, Constants.HEADER_CLIENT_REQUEST_ID, Guid.NewGuid().ToString());

            foreach (var item in headers)
            {
                req.Headers.Add(item.Key, item.Value);
            }
            if (content == null)
            {
                req.Content = new StringContent(string.Empty);
                req.Content.Headers.ContentLength = 0;
                req.Content.Headers.ContentType   = null;
            }
            else
            {
                req.Content = new ByteArrayContent(content);
                req.Content.Headers.ContentLength = content.Length;
                req.Content.Headers.ContentType   = new MediaTypeHeaderValue(contentType);
            }

            if (null != this.AzureADSettings)
            {
                // cf. https://stackoverflow.com/a/54256816
                await ApplicationTokenProvider.LoginSilentAsync(
                    AzureADSettings.TenantId,
                    AzureADSettings.ServicePrincipalId,
                    AzureADSettings.ServicePrincipalSecret,
                    AzureADServiceSettings,
                    TokenCache.DefaultShared);

                var token = TokenCache.DefaultShared.ReadItems()
                            .Where(t => t.ClientId == AzureADSettings.ServicePrincipalId)
                            .OrderByDescending(t => t.ExpiresOn)
                            .First();
                req.Headers.Authorization = new AuthenticationHeaderValue(
                    "Bearer",
                    token.AccessToken);
            }
            else
            {
                req.Headers.Authorization = new AuthenticationHeaderValue(
                    SASHelper.SCHEME,
                    SASHelper.GetAuthorizationHeader(req, AccountName, AccountKey));
            }

            showHeaders(req);
            return(await client.SendAsync(req));
        }