예제 #1
0
        public static SasTokenParameters Parse(IList <KeyValuePair <string, string> > args)
        {
            var additionalList = new Dictionary <string, string>();

            foreach (var kvp in args.Where(x => !new[] { "skn", "sr", "se", "sig", "nonce" }.Contains(x.Key)))
            {
                additionalList.Add(kvp.Key, kvp.Value);
            }
            var token = new SasTokenParameters
            {
                SigningKeyName   = args.FirstOrDefault(q => q.Key == "skn").Value,
                SharedResource   = args.FirstOrDefault(q => q.Key == "sr").Value,
                Expiry           = GetInt(args.FirstOrDefault(q => q.Key == "se").Value),
                Signature        = args.FirstOrDefault(q => q.Key == "sig").Value,
                Nonce            = args.FirstOrDefault(q => q.Key == "nonce").Value,
                AdditionalValues = additionalList
            };

            token.Invalid = (string.IsNullOrEmpty(token.SigningKeyName) ||
                             string.IsNullOrEmpty(token.SharedResource) ||
                             string.IsNullOrEmpty(token.Signature) ||
                             token.Expiry <= 0);
            return(token);
        }
예제 #2
0
        /// <summary>
        ///     Create signed sastoken
        /// </summary>
        /// <param name="sharedResourceName">Name of the shared resource.</param>
        /// <param name="timeoutInSeconds">The timeout for the token in seconds.</param>
        /// <param name="signingKeyName">The name for the signing key.</param>
        /// <param name="additionalValues">The additional values that should be included.</param>
        /// <param name="useNonce">if set to <c>true</c> then use the nonce in the signature.</param>
        /// <param name="hashType">Type of the hash.</param>
        /// <param name="sharedSecret">if set to a valid string then the calculation is executed immediately.</param>
        /// <param name="includeAdditionalValues">if set to <c>true</c> then use the additional values in the signature.</param>
        /// <param name="includeAdditionalKeys">if set to <c>true</c> then use the additional keys in the signature.</param>
        /// <returns>SASToken.</returns>
        private static SasTokenParameters ExecuteCreate(
            string sharedResourceName,
            int timeoutInSeconds,
            string signingKeyName,
            IDictionary <string, string> additionalValues,
            bool useNonce,
            HashType hashType,
            string sharedSecret)
        {
            if (string.IsNullOrEmpty(sharedSecret))
            {
                throw new ArgumentNullException("sharedSecret", "Invalid signing key");
            }
            var epochCurrent = DateTime.UtcNow - new DateTime(1970, 1, 1);
            var expiry       = (int)epochCurrent.TotalSeconds + timeoutInSeconds;

            var sasToken = new SasTokenParameters
            {
                SharedResource   = sharedResourceName,
                Expiry           = expiry,
                SigningKeyName   = signingKeyName,
                AdditionalValues = additionalValues,
                Nonce            = ""
            };

            if (useNonce)
            {
                sasToken.Nonce = CreateNewNonce(12);
            }
            sasToken.Signature = sasToken.CalcSignature(
                sharedSecret,
                useNonce,
                hashType,
                additionalValues?.Keys);
            return(sasToken);
        }