Course contentsShow
← C#
Lesson 1603 of 1,90814. Background Work and MessagingFree lesson

Securing Inter-Service Communication

Implement mTLS or token-based authentication for all background service message traffic.

In this lesson, you will learn how to enforce identity and transport security for your background messaging traffic using mTLS or token-based authentication.

When background workers consume messages from a broker, the "internal" nature of the network is often treated as a trusted perimeter—a dangerous assumption in modern distributed systems. To secure this traffic, we must verify both the identity of the sender and the consumer.

Mutual TLS (mTLS)

mTLS ensures that both the message broker and the worker verify each other's certificates. This prevents unauthorized workers from reading from a queue or malicious actors from injecting unauthorized tasks.

In a .NET environment, you configure this at the connection factory level (e.g., when using RabbitMQ or Kafka clients). You provide a X509CertificateCollection to your client configuration:

var factory = new ConnectionFactory();
var certs = new X509CertificateCollection { new X509Certificate2("worker.pfx", "password") };
factory.Ssl.Certificates = certs;
factory.Ssl.Enabled = true;
factory.Ssl.ServerName = "broker.internal.net";

Token-Based Authentication

If your infrastructure relies on a centralized identity provider, you can use short-lived JWTs (JSON Web Tokens). In this pattern, your background service requests a token using its managed identity (like Azure Managed Identity or AWS IAM roles) and attaches it to the connection metadata.

The broker validates the signature and the claims (such as sub or roles) before allowing the connection. This is often preferred when certificates are difficult to rotate across hundreds of microservices, as tokens can be refreshed automatically by your background service’s HTTP client or service bus SDK.

Which to choose?

  • mTLS provides the strongest transport-level security, ensuring that even if an attacker gains access to the network, they cannot intercept or spoof the communication without the private key.
  • Token-based auth offers better granularity for application-level access control, allowing you to restrict a specific worker to only specific queues based on claims within the token.

For highly sensitive background workloads, implementing both—mTLS for transport encryption and tokens for authorization—is the gold standard.

Key Takeaway: Never rely on network isolation; always enforce mutual identity verification between your background workers and message brokers using mTLS or claims-based tokens.