コード例 #1
0
        static void GrantOperationRight(string operation, string path, string name)
        {
            AccessControlSettings settings = new AccessControlSettings(@namespace, managementKey);
            Uri uri = ServiceBusEnvironment.CreateServiceUri("http", @namespace, path);
            AccessControlList list = NamespaceAccessControl.GetAccessControlList(uri, settings);
            IdentityReference identityReference = IdentityReference.CreateServiceIdentityReference(name);

            if (operation.Equals("Send", StringComparison.OrdinalIgnoreCase))
            {
                AccessControlRule existing = list.FirstOrDefault((r) => r.Condition.Equals(identityReference) && r.Right.Equals(ServiceBusRight.Send));
                if (existing == null)
                {
                    list.AddRule(identityReference, ServiceBusRight.Send);
                    list.SaveChanges();
                }
                else
                {
                    Console.Error.WriteLine("The right '{0}' on '{1}' has already been granted to identity '{2}'", operation, path, name);
                }
            }
            else if (operation.Equals("Listen", StringComparison.OrdinalIgnoreCase))
            {
                AccessControlRule existing = list.FirstOrDefault((r) => r.Condition.Equals(identityReference) && r.Right.Equals(ServiceBusRight.Listen));
                if (existing == null)
                {
                    list.AddRule(identityReference, ServiceBusRight.Listen);
                    list.SaveChanges();
                }
                else
                {
                    Console.Error.WriteLine("The right '{0}' on '{1}' has already been granted to identity '{2}'", operation, path, name);
                }
            }
            else if (operation.Equals("Manage", StringComparison.OrdinalIgnoreCase))
            {
                AccessControlRule existing = list.FirstOrDefault((r) => r.Condition.Equals(identityReference) && r.Right.Equals(ServiceBusRight.Manage));
                if (existing == null)
                {
                    list.AddRule(identityReference, ServiceBusRight.Manage);
                    list.SaveChanges();
                }
                else
                {
                    Console.Error.WriteLine("The right '{0}' on '{1}' has already been granted to identity '{2}'", operation, path, name);
                }
            }
            else
            {
                Console.Error.WriteLine("Unknown operation '{0}'", operation);
            }
        }