예제 #1
0
        public static string CreateContainerSasToken(
            string accountName,
            string containerName,
            string key,
            SasAccessType accessType,
            int validityTimeInDays)
        {
            string signedPermissions = string.Empty;

            switch (accessType)
            {
            case SasAccessType.Read:
                signedPermissions = "r";
                break;

            case SasAccessType.Write:
                signedPermissions = "wdl";
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(accessType), accessType, "Unrecognized value");
            }

            string signedStart           = DateTime.UtcNow.ToString("O");
            string signedExpiry          = DateTime.UtcNow.AddDays(validityTimeInDays).ToString("O");
            string canonicalizedResource = "/blob/" + accountName + "/" + containerName;
            string signedIdentifier      = string.Empty;
            string signedVersion         = StorageApiVersion;

            string stringToSign = ConstructServiceStringToSign(
                signedPermissions,
                signedVersion,
                signedExpiry,
                canonicalizedResource,
                signedIdentifier,
                signedStart);

            byte[] signatureBytes = Encoding.UTF8.GetBytes(stringToSign);
            string signature;

            using (HMACSHA256 hmacSha256 = new HMACSHA256(Convert.FromBase64String(key)))
            {
                signature = Convert.ToBase64String(hmacSha256.ComputeHash(signatureBytes));
            }

            string sasToken = string.Format(
                "?sv={0}&sr={1}&sig={2}&st={3}&se={4}&sp={5}",
                WebUtility.UrlEncode(signedVersion),
                WebUtility.UrlEncode("c"),
                WebUtility.UrlEncode(signature),
                WebUtility.UrlEncode(signedStart),
                WebUtility.UrlEncode(signedExpiry),
                WebUtility.UrlEncode(signedPermissions));

            return(sasToken);
        }
예제 #2
0
        public static string CreateContainerSasToken(
            string accountName, 
            string containerName, 
            string key, 
            SasAccessType accessType, 
            int validityTimeInDays)
        {
            string signedPermissions = string.Empty;
            switch (accessType)
            {
                case SasAccessType.Read:
                    signedPermissions = "r";
                    break;
                case SasAccessType.Write:
                    signedPermissions = "wdl";
                    break;
                default:
                    throw new ArgumentOutOfRangeException(nameof(accessType), accessType, "Unrecognized value");
            }

            string signedStart = DateTime.UtcNow.ToString("O");
            string signedExpiry = DateTime.UtcNow.AddDays(validityTimeInDays).ToString("O");
            string canonicalizedResource = "/blob/" + accountName + "/" + containerName;
            string signedIdentifier = string.Empty;
            string signedVersion = StorageApiVersion;

            string stringToSign = ConstructServiceStringToSign(
                signedPermissions, 
                signedVersion, 
                signedExpiry, 
                canonicalizedResource, 
                signedIdentifier, 
                signedStart);

            byte[] signatureBytes = Encoding.UTF8.GetBytes(stringToSign);
            string signature;
            using (HMACSHA256 hmacSha256 = new HMACSHA256(Convert.FromBase64String(key)))
            {
                signature = Convert.ToBase64String(hmacSha256.ComputeHash(signatureBytes));
            }

            string sasToken = string.Format(
                "?sv={0}&sr={1}&sig={2}&st={3}&se={4}&sp={5}", 
                WebUtility.UrlEncode(signedVersion), 
                WebUtility.UrlEncode("c"), 
                WebUtility.UrlEncode(signature), 
                WebUtility.UrlEncode(signedStart), 
                WebUtility.UrlEncode(signedExpiry), 
                WebUtility.UrlEncode(signedPermissions));

            return sasToken;
        }
        private static string GetFullFileName(SasAccessType sasAccessType)
        {
            var    fileName = Guid.NewGuid();
            string fileExtension;

            switch (sasAccessType)
            {
            case SasAccessType.Design:
            case SasAccessType.ProfileImage:
                fileExtension = ".jpg";
                break;

            case SasAccessType.Model:
                fileExtension = ".glb";
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(sasAccessType), sasAccessType, null);
            }

            return($"{fileName}{fileExtension}");
        }
        private static string GetBlobNamePath(SasAccessType sasAccessType)
        {
            var blobNamePath = "";

            switch (sasAccessType)
            {
            case SasAccessType.Design:
                blobNamePath = "images";
                break;

            case SasAccessType.Model:
                blobNamePath = "models";
                break;

            case SasAccessType.ProfileImage:
                blobNamePath = "profile_images";
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(sasAccessType), sasAccessType, null);
            }

            return(blobNamePath);
        }