/// <summary>
        /// Generates a shared access signature that can be used to authenticate with a topic.
        /// The signature can be used as the input to the <see cref="AzureSasCredential(string)"/> constructor.
        /// This credential can then be passed to the <see cref="EventGridPublisherClient(Uri, AzureSasCredential, EventGridPublisherClientOptions)"/> constructor.
        /// </summary>
        /// <param name="key">The <see cref="AzureKeyCredential"/> to use to authenticate with the service
        /// when generating the shared access signature.</param>
        /// <returns>A shared access signature that can be used to authenticate with an Event Grid topic.</returns>
        public string GenerateSas(AzureKeyCredential key)
        {
            Argument.AssertNotNull(key, nameof(key));
            const char Resource   = 'r';
            const char Expiration = 'e';
            const char Signature  = 's';

            var uriBuilder = new RequestUriBuilder();

            uriBuilder.Reset(Endpoint);
            uriBuilder.AppendQuery("api-version", ApiVersion.GetVersionString(), true);
            string encodedResource      = HttpUtility.UrlEncode(uriBuilder.ToString());
            var    encodedExpirationUtc = HttpUtility.UrlEncode(ExpiresOn.ToString(CultureInfo.CreateSpecificCulture("en-US")));

            string unsignedSas = $"{Resource}={encodedResource}&{Expiration}={encodedExpirationUtc}";

            using (var hmac = new HMACSHA256(Convert.FromBase64String(key.Key)))
            {
                string signature        = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(unsignedSas)));
                string encodedSignature = HttpUtility.UrlEncode(signature);
                string signedSas        = $"{unsignedSas}&{Signature}={encodedSignature}";

                return(signedSas);
            }
        }
示例#2
0
 /// <summary>
 /// To the JWT dictionary.
 /// </summary>
 /// <returns></returns>
 public Dictionary <string, object> ToJwtDictionary()
 {
     return(new Dictionary <string, object>()
     {
         { "userId", UserId.ToString() },
         { "initOn", InitOn.ToString("yyyyMMddHHmmss") },
         { "expiresOn", ExpiresOn.ToString("yyyyMMddHHmmss") }
     });
 }
示例#3
0
        /// <summary>
        /// Builds up the UserDelegationKey portion of the SAS query parameter string.
        /// </summary>
        public void AppendProperties(StringBuilder stringBuilder)
        {
            if (!string.IsNullOrWhiteSpace(ObjectId))
            {
                stringBuilder.AppendQueryParameter(Constants.Sas.Parameters.KeyObjectId, ObjectId);
            }

            if (!string.IsNullOrWhiteSpace(TenantId))
            {
                stringBuilder.AppendQueryParameter(Constants.Sas.Parameters.KeyTenantId, TenantId);
            }

            if (StartsOn != DateTimeOffset.MinValue)
            {
                stringBuilder.AppendQueryParameter(Constants.Sas.Parameters.KeyStart, WebUtility.UrlEncode(StartsOn.ToString(Constants.SasTimeFormatSeconds, CultureInfo.InvariantCulture)));
            }

            if (ExpiresOn != DateTimeOffset.MinValue)
            {
                stringBuilder.AppendQueryParameter(Constants.Sas.Parameters.KeyExpiry, WebUtility.UrlEncode(ExpiresOn.ToString(Constants.SasTimeFormatSeconds, CultureInfo.InvariantCulture)));
            }

            if (!string.IsNullOrWhiteSpace(Service))
            {
                stringBuilder.AppendQueryParameter(Constants.Sas.Parameters.KeyService, Service);
            }

            if (!string.IsNullOrWhiteSpace(Version))
            {
                stringBuilder.AppendQueryParameter(Constants.Sas.Parameters.KeyVersion, Version);
            }
        }