RabbitMQ C# connection trouble when using a username and password

左心房为你撑大大i 提交于 2019-12-31 08:45:05

问题


I am at a loss here so I'm reaching out to the collective knowledge in hope of a miracle.

I have installed RabbitMQ on a Linux box using the defaults.

When I use this code (and the default RabbitMQ installation configuration) everything works nice.

var connectionFactory = new ConnectionFactory();
connectionFactory.HostName = "192.168.0.12";
IConnection connection = connectionFactory.CreateConnection();

But when I add a user to RabbitMQ and try to use the following code (username and password has been changed to protect the innocent. :) )

var connectionFactory = new ConnectionFactory();
connectionFactory.HostName = "192.168.0.12";
connectionFactory.UserName = "user";
connectionFactory.Password = "password";
IConnection connection = connectionFactory.CreateConnection();

the connectionFactory.CreateConnection() method throws the following exception:

BrokerUnreachableException    
None of the specified endpoints were reachable

Checking the RabbitMQ logfile I can see it complaining about the credentials:

{amqp_error,access_refused,
"PLAIN login refused: user 'user' - invalid credentials",
'connection.start_ok'}}

The thing is that I am confident about the username and password and I cannot for the love of coding find a solution to this anywhere.

I must be missing something obvious but I can't figure out what it is. I would be grateful for any helpful pointers.


回答1:


It seems that I have found a solution to my own problem. The following code works:

ConnectionFactory factory = new ConnectionFactory();
factory.UserName = "user";
factory.Password = "password";
factory.VirtualHost = "/";
factory.Protocol = Protocols.FromEnvironment();
factory.HostName = "192.168.0.12";
factory.Port = AmqpTcpEndpoint.UseDefaultPort;
IConnection conn = factory.CreateConnection();

Thanks for listening and perhaps this at least could be useful to someone else. :)




回答2:


The accepted answer didn't work for me (on Windows).

I had to install the management tools:

rabbitmq-plugins enable rabbitmq_management

N.B. rabbitmq-plugins is in C:\Program Files (x86)\RabbitMQ Server\rabbitmq_server-3.3.1\sbin

Then, restart the RabbitMQ service.

I then installed EasyNetQ in Visual Studio in the package manager:

install-package easynetq

With this installed, I could use the admin web site located at:

http://localhost:15672

N.B. The default username and password is: guest

From here, I selected the Admin tab and the cause was clearly displayed in yellow at the top of the screen:

This user does not have permission to access any virtual hosts.
Use "Set Permission" below to grant permission to access virtual hosts. 

To fix the issue I just pressed the Set permission button on the same screen et voila

N.B. for this to have worked you need to have added the user using rabbitmqctl add_user username password or similar (rabbitmqctl is also in the directory above).




回答3:


Here is how to create a user called agent with password agent, set it to be administrator and give it read and write access to all queues in the vhost /

rabbitmqctl add_user agent agent
rabbitmqctl set_user_tags agent administrator
rabbitmqctl set_permissions -p / agent ".*" ".*" ".*"


来源:https://stackoverflow.com/questions/4987438/rabbitmq-c-sharp-connection-trouble-when-using-a-username-and-password

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