user181261
user181261

Reputation:

RabbitMQ C# connection trouble when using a username and password

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.

Upvotes: 34

Views: 52295

Answers (3)

Mihail Shishkov
Mihail Shishkov

Reputation: 15877

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 ".*" ".*" ".*"

Upvotes: 9

Robbie Dee
Robbie Dee

Reputation: 1977

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).

Upvotes: 7

user181261
user181261

Reputation:

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. :)

Upvotes: 39

Related Questions