ServiceBus throws 401 Unauthorized Error

大憨熊 提交于 2019-12-08 02:15:32

问题


I'm using a simple implementation of the Windows Service Bus 1.0 Brokered messaging to keep track of the user interactions with a particular web application.

Every time something is saved to a "sensitive" table in the database, I have setup the repository layer send a message like so:

ServiceBus.MessageQueue<T>.PushAsync(entity);

which will then serialize the entity and create a message out of it.

My MessageQueue class is something like this.

public static class MessageQueue<T>
{
    static string ServerFQDN;
    static int HttpPort = 9355;
    static int TcpPort = 9354;
    static string ServiceNamespace = "ServiceBusDefaultNamespace";

    public static void PushAsync(T msg)
    {
        ServerFQDN = System.Net.Dns.GetHostEntry(string.Empty).HostName;

        //Service Bus connection string
        var connBuilder = new ServiceBusConnectionStringBuilder { ManagementPort = HttpPort, RuntimePort = TcpPort };
        connBuilder.Endpoints.Add(new UriBuilder() { Scheme = "sb", Host = ServerFQDN, Path = ServiceNamespace }.Uri);
        connBuilder.StsEndpoints.Add(new UriBuilder() { Scheme = "https", Host = ServerFQDN, Port = HttpPort, Path = ServiceNamespace}.Uri);

        //Create a NamespaceManager instance (for management operations) and a MessagingFactory instance (for sending and receiving messages)
        MessagingFactory messageFactory = MessagingFactory.CreateFromConnectionString(connBuilder.ToString());
        NamespaceManager namespaceManager = NamespaceManager.CreateFromConnectionString(connBuilder.ToString());

        if (namespaceManager == null)
        {
            Console.WriteLine("\nUnexpected Error");
            return;
        }

        //create a new queue
        string QueueName = "ServiceBusQueueSample";
        if (!namespaceManager.QueueExists(QueueName))
        {
            namespaceManager.CreateQueue(QueueName);
        }

        try
        {            
            QueueClient myQueueClient = messageFactory.CreateQueueClient(QueueName);

            string aaa = JsonConvert.SerializeObject(msg, Formatting.Indented,
                            new JsonSerializerSettings()
                            {
                                ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                                ContractResolver = new NHibernateContractResolver()
                            });

            BrokeredMessage sendMessage1 = new BrokeredMessage(aaa);
            sendMessage1.Properties.Add("UserName",Thread.CurrentPrincipal.Identity.Name); 
            sendMessage1.Properties.Add("TimeStamp", ApplicationDateTime.Now);
            sendMessage1.Properties.Add("Type", msg.GetType().Name);


            myQueueClient.Send(sendMessage1);

        }
        catch (Exception e)
        {
            var l = new Logger();
            l.Log(LogEventEnum.WebrequestFailure, e.Message);
            Console.WriteLine("Unexpected exception {0}", e.ToString());
            throw;
        }

    }
}

This works flawlessly when I debug this locally. But when I publish the site in IIS and run, the namespaceManager.QueueExists(QueueName) call fails with an exception which says "401 Unauthorized error".

When I change the Application pool identity (in IIS) to an admin account this error does not occur. However, there is absolutely no way that I can make this happen when we go production.

Am I missing something? If so, what is it? Any help is greatly appreciated.


回答1:


Did you read the security section in the docs, Chameera? http://msdn.microsoft.com/en-us/library/windowsazure/jj193003(v=azure.10).aspx

You seem to be running with the default security settings, meaning you only have admin accounts authorized. Review the documentation section and grant the requisite rights to the accounts you want to use in prod.



来源:https://stackoverflow.com/questions/18558299/servicebus-throws-401-unauthorized-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!