ServiceBus引发401未经授权的错误

本文关键字:授权 错误 引发 ServiceBus | 更新日期: 2023-09-27 18:29:51

我正在使用Windows Service Bus 1.0代理消息传递的简单实现来跟踪用户与特定web应用程序的交互。

每次有东西被保存到数据库中的"敏感"表中时,我都会设置存储库层,并发送这样的消息:

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

然后它将串行化该实体并从中创建一个消息

我的MessageQueue类是这样的。

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;
        }
    }
}

当我在本地调试它时,它可以完美地工作。但是,当我在IIS中发布网站并运行时,namespaceManager.QueueExists(QueueName)调用失败,并显示一个异常,显示"401未经授权的错误"。

当我将应用程序池标识(在IIS中)更改为管理员帐户时,不会发生此错误。然而,当我们开始生产时,我绝对没有办法做到这一点。

我是不是错过了什么?如果是,那是什么?非常感谢您的帮助。

ServiceBus引发401未经授权的错误

Chameera,你读过文档中的安全部分吗?http://msdn.microsoft.com/en-us/library/windowsazure/jj193003(v=azure.10).aspx

你似乎使用默认的安全设置运行,这意味着你只有管理员帐户授权。查看文档部分,并授予您要在产品中使用的帐户必要的权限。