English 中文(简体)
使用用户名和密码时出现RabbitMQ C#连接问题
原标题:RabbitMQ C# connection trouble when using a username and password

我在这里不知所措,所以我向集体知识伸出援手,希望奇迹发生。

我已经使用默认值在Linux上安装了RabbitMQ。

当我使用这段代码(以及默认的RabbitMQ安装配置)时,一切都很好。

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

但是,当我将用户添加到RabbitMQ并尝试使用以下代码时(用户名和密码已更改以保护无辜者。:))

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

connectionFactory.CreateConnection()方法抛出以下异常:

BrokerUnreachableException    
None of the specified endpoints were reachable

检查RabbitMQ日志文件,我可以看到它抱怨凭据:

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

问题是,我对用户名和密码很有信心,因为热爱编码,我无法在任何地方找到解决方案。

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

最佳回答

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();

谢谢你的聆听,也许这至少对其他人有用。:)

问题回答

以下是如何创建一个名为agent的用户,密码为agent,将其设置为1administrator并允许其readwrite访问vhost中的所有队列/

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

接受的答案(在Windows上)对我不起作用。

我必须安装管理工具:

rabbitmq-plugins enable rabbitmq_management

注意:rabbitmq-plugins位于C:程序文件(x86)rabbitmq服务器中

然后,重新启动RabbitMQ服务。

然后,我在Visual Studio的包管理器中安装了EasyNetQ:

install-package easynetq

安装此软件后,我可以使用位于以下位置的管理网站:

http://localhost:15672

注意:默认用户名和密码为:guest

从这里,我选择了“管理”选项卡,原因在屏幕顶部以黄色清晰显示:

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

为了解决这个问题,我只按下了同一屏幕上的设置权限按钮,瞧

注意:要做到这一点,您需要使用rabbitmqctl add_user username password或类似的方法添加用户(rabbitmqctl也在上面的目录中)。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...