Search code examples
servicebus

Why have I UnauthorizedAccessException when I use Service Bus MessagingFactory?


I have configured authorization via certificate. When I use NamespaceManager to GetSubscription works fine, but when I try execute any method from MessagingFactory I receive System.UnauthorizedAccessException.

string connectionString = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");

var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);
SubscriptionDescription subscriptionDescription = namespaceManager.GetSubscription("testTopic", "testSubscription"); // executed properly

var messagingFactory = MessagingFactory.CreateFromConnectionString(connectionString);
var exist = namespaceManager.TopicExists("TestTopic"); // throws exception

Stack trace:

System.UnauthorizedAccessException was unhandled
  HResult=-2147024891
  Message=The remote server returned an error: (401) Unauthorized. Authorization failed for specified action: Manage..TrackingId:d8648c5a-5185-41c8-b787-72332403b7d9_*******,TimeStamp:2014-07-14 08:34:22
  Source=Microsoft.ServiceBus

What's the matter?


Solution

  • For use NamespaceManager you must be in ManageUsers in Service Bus Namespace. To add user to existing namespace you can use ps Set-SBNamespace