public override void ExecuteCmdlet()
        {
            try
            {
                LocalResourceIdentifier identifier = new LocalResourceIdentifier(AuthorizationRuleId);
                string resourceUri = string.Empty, strPolicyName = string.Empty, sakey = string.Empty;

                PSListKeysAttributes listkeys;
                if (identifier.ParentResource1 != null && AuthorizationRuleId.Contains("topics"))
                {
                    listkeys = Client.GetTopicKey(identifier.ResourceGroupName, identifier.ParentResource, identifier.ParentResource1, identifier.ResourceName);
                }
                else if (identifier.ParentResource1 != null && AuthorizationRuleId.Contains("queues"))
                {
                    listkeys = Client.GetQueueKey(identifier.ResourceGroupName, identifier.ParentResource, identifier.ParentResource1, identifier.ResourceName);
                }
                else
                {
                    listkeys = Client.GetNamespaceListKeys(identifier.ResourceGroupName, identifier.ParentResource, identifier.ResourceName);
                }

                string[] connectionstring = KeyType == "Primary" ? listkeys.PrimaryConnectionString.Split(';') : listkeys.SecondaryConnectionString.Split(';');

                switch (connectionstring.Length)
                {
                case 4:
                {
                    resourceUri   = connectionstring[0].Replace("Endpoint=sb://", "") + connectionstring[3].Replace("EntityPath=", "");
                    strPolicyName = connectionstring[1].Replace("SharedAccessKeyName=", "");
                    sakey         = connectionstring[2].Replace("SharedAccessKey=", "");
                    break;
                }

                case 3:
                {
                    resourceUri   = connectionstring[0].Replace("Endpoint=sb://", "");
                    strPolicyName = connectionstring[1].Replace("SharedAccessKeyName=", "");
                    sakey         = connectionstring[2].Replace("SharedAccessKey=", "");
                    break;
                }

                default:
                {
                    break;
                }
                }

                string     stringToSign = StartTime.HasValue ? StartTime.ToString() + "\n" + System.Web.HttpUtility.UrlEncode(resourceUri) + "\n" + ExpiryTime.ToString() : System.Web.HttpUtility.UrlEncode(resourceUri) + "\n" + ExpiryTime.ToString();
                HMACSHA256 hmac         = new HMACSHA256(System.Text.Encoding.UTF8.GetBytes(sakey));
                var        signature    = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)));
                string     sasToken     = String.Format(CultureInfo.InvariantCulture, "SharedAccessSignature sr={0}&sig={1}&se={2}&skn={3}", HttpUtility.UrlEncode(resourceUri), HttpUtility.UrlEncode(signature), ExpiryTime, KeyType);
                PSSharedAccessSignatureAttributes psSastoken = new PSSharedAccessSignatureAttributes(sasToken);
                WriteObject(psSastoken, true);
            }
            catch (Management.ServiceBus.Models.ErrorResponseException ex)
            {
                WriteError(ServiceBusClient.WriteErrorforBadrequest(ex));
            }
        }
Esempio n. 2
0
        public override void ExecuteCmdlet()
        {
            try
            {
                LocalResourceIdentifier identifier = new LocalResourceIdentifier(AuthorizationRuleId);
                string   resourceUri = string.Empty, strPolicyName = string.Empty, sakey = string.Empty;
                DateTime EpochTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);

                PSListKeysAttributes listkeys;
                if (identifier.ParentResource1 != null)
                {
                    listkeys = Client.GetEventHubListKeys(identifier.ResourceGroupName, identifier.ParentResource, identifier.ParentResource1, identifier.ResourceName);
                }
                else
                {
                    listkeys = Client.GetNamespaceListKeys(identifier.ResourceGroupName, identifier.ParentResource, identifier.ResourceName);
                }

                string[] connectionstring = KeyType == "Primary" ? listkeys.PrimaryConnectionString.Split(';') : listkeys.SecondaryConnectionString.Split(';');

                switch (connectionstring.Length)
                {
                case 4:
                {
                    resourceUri   = connectionstring[0].Replace("Endpoint=sb://", "") + connectionstring[3].Replace("EntityPath=", "");
                    strPolicyName = connectionstring[1].Replace("SharedAccessKeyName=", "");
                    sakey         = connectionstring[2].Replace("SharedAccessKey=", "");
                    break;
                }

                case 3:
                {
                    resourceUri   = connectionstring[0].Replace("Endpoint=sb://", "");
                    strPolicyName = connectionstring[1].Replace("SharedAccessKeyName=", "");
                    sakey         = connectionstring[2].Replace("SharedAccessKey=", "");
                    break;
                }

                default:
                {
                    break;
                }
                }

                TimeSpan   secondsFromBaseTime = ExpiryTime.Value.Subtract(EpochTime);
                long       seconds             = Convert.ToInt64(secondsFromBaseTime.TotalSeconds, CultureInfo.InvariantCulture);
                string     stringToSign        = StartTime.HasValue ? StartTime.ToString() + "\n" + System.Web.HttpUtility.UrlEncode(resourceUri) + "\n" + seconds : System.Web.HttpUtility.UrlEncode(resourceUri) + "\n" + seconds;
                HMACSHA256 hmac      = new HMACSHA256(System.Text.Encoding.UTF8.GetBytes(sakey));
                var        signature = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)));
                string     sasToken  = String.Format(CultureInfo.InvariantCulture, "SharedAccessSignature sr={0}&sig={1}&se={2}&skn={3}", HttpUtility.UrlEncode(resourceUri), HttpUtility.UrlEncode(signature), seconds, KeyType);
                PSSharedAccessSignatureAttributes psSastoken = new PSSharedAccessSignatureAttributes(sasToken);
                WriteObject(psSastoken, true);
            }
            catch (Management.EventHub.Models.ErrorResponseException ex)
            {
                WriteError(Eventhub.EventHubsClient.WriteErrorforBadrequest(ex));
            }
        }