c# - How to authenticate users across microservices using Identity and RabbitMQ? - Stack Overflow

admin2025-04-17  2

I'm working on app using Microservices. I have to use Microservices and RabbitMq. I have to make 2 microservices Auth and Products. I want to make Auth Microservices using built in Identity. I also have to use RabbitMq. I set up Auth Microservices with working endpoints and Products Microservices with working endpoints. And now i dont know whats next.

After someone login do i have to send a message to a Queue and then read this message on the server? Do i have to send a JWT token to a Queue and then somehow validate it in Products?

How steps should i follow and what logic to make it work? Best explanation is step by step and as simple as possible; So far i have written a function that can add something to the queue.

 public async Task SendMessageAsync(string message)
    {
        var factory = new ConnectionFactory { HostName = "localhost" };
        await using IConnection connection = await factory.CreateConnectionAsync();
        await using IChannel channel = await connection.CreateChannelAsync(); 
        await channel.QueueDeclareAsync(queue: "RabbitMqTest", durable: false, exclusive: false, autoDelete: false, arguments: null);
        byte[] body = Encoding.UTF8.GetBytes(message);
        await channel.BasicPublishAsync(exchange: string.Empty, routingKey: "RabbitMqTest", body: body);
    }

I'm working on app using Microservices. I have to use Microservices and RabbitMq. I have to make 2 microservices Auth and Products. I want to make Auth Microservices using built in Identity. I also have to use RabbitMq. I set up Auth Microservices with working endpoints and Products Microservices with working endpoints. And now i dont know whats next.

After someone login do i have to send a message to a Queue and then read this message on the server? Do i have to send a JWT token to a Queue and then somehow validate it in Products?

How steps should i follow and what logic to make it work? Best explanation is step by step and as simple as possible; So far i have written a function that can add something to the queue.

 public async Task SendMessageAsync(string message)
    {
        var factory = new ConnectionFactory { HostName = "localhost" };
        await using IConnection connection = await factory.CreateConnectionAsync();
        await using IChannel channel = await connection.CreateChannelAsync(); 
        await channel.QueueDeclareAsync(queue: "RabbitMqTest", durable: false, exclusive: false, autoDelete: false, arguments: null);
        byte[] body = Encoding.UTF8.GetBytes(message);
        await channel.BasicPublishAsync(exchange: string.Empty, routingKey: "RabbitMqTest", body: body);
    }
Share Improve this question asked Jan 30 at 21:32 user28273827332user28273827332 1239 bronze badges 1
  • I think you need to use OAUTH2 which is a two step authentication. Step 1 is to get a token/cookie. Step 2 is to make a query using the token. See rabbitmq.com/docs/oauth2 – jdweng Commented Jan 30 at 22:50
Add a comment  | 

1 Answer 1

Reset to default 0

As far as I know, if you want to communicate (like inter-process communication) between the microservices you need the message broker like RabbitMQ. For example, if you have 2 micro services named stock and sales, whenever a sale is over (in sales service) the stock needs to be updated. In this case the RabbitMQ needed. So the sales service put the message in the RabbitMQ queue which is consumed by Stock service to adjust in it. So AFAIK, in your case RabbitMQ is not needed. Also, the authentication related stuff can be handled in "APIGateway" which is a central point located between the client and the microservices. For APIGateway, you can consider to use Ocelot, Yarp etc. Let me know if you have any further questions. Hope this helps!

转载请注明原文地址:http://anycun.com/QandA/1744889419a89060.html