c# - ServiceBus 抛出 401 未授权错误

标签 c# asp.net-mvc-4 iis-7.5 servicebus

我正在使用 Windows Service Bus 1.0 Brokered 消息传递的简单实现来跟踪用户与特定 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 Unauthorized error”。

当我将应用程序池标识(在 IIS 中)更改为管理员帐户时,不会发生此错误。但是,当我们投入生产时,我绝对没有可以做到这一点。

我错过了什么吗?如果是这样,它是什么?任何帮助是极大的赞赏。

最佳答案

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

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

关于c# - ServiceBus 抛出 401 未授权错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18558299/

相关文章:

c# - 在后台运行任务但在 ASP MVC Web 应用程序中返回响应给客户端

c# - 在 LINQ 中使用条件

asp.net-mvc - angularjs 仅带有 cshtml 页面,而不是带有 web api 2 的 html 页面

IIS 7.5 和 Web API 2 PUT/DELETE 请求

c# - ASP.NET 网络应用程序 : Unable to read DLL

c# - C# 中的 Java Robot 类等效代码

c# - 如何以编程方式知道 IIS 是否已安装

c# - 为什么我们会使用 Directory.GetFiles() 而不是 Directory.EnumerateFiles()?

asp.net-mvc - 在不使用 ViewModel 对象的情况下,Razor View 是否可以拥有多个模型?

asp.net-mvc - ASP.NET MVC 4 Catch-all 只在本地触发,而不是远程请求