How to implement an access control handler in NServiceBus

Just wanted to know if there is a way to implement the Access control message handler in NServiceBus.By "Access Control Handler". I mean that one handler should always be executed before other handlers and should control (or rather conditionally prevent another handler from executing).

Does anyone know how to implement this in NServiceBus?

I have defined priority handlers to execute in EndPointConfig like this

public class EndpointConfig : IConfigureThisEndpoint, AsA_Server, ISpecifyMessageHandlerOrdering
{
    #region ISpecifyMessageHandlerOrdering Members

    public void SpecifyOrder(Order order)
    {
        order.Specify<First<AccessControlHandler>>();
    }

    #endregion
}

Thanks in advance,

Vijay.

+3
source share
1 answer

You can create your AccessControlHandler as follows

 public class AccessControlHandler : IHandleMessages<IMessage>
{
    public IBus Bus { get; set; }

    public void Handle(IMessage message)
    {
        IDictionary<string, string> headers = Bus.CurrentMessageContext.Headers;
        string token;

        if (headers.TryGetValue("access_token", out token))
        {
            if (token == "MY_SECRET")
            {
                Console.WriteLine("User authenticated");
                return;
            }
        }

        Console.WriteLine("User not authenticated");
        Bus.DoNotContinueDispatchingCurrentMessageToHandlers();
    }

, , ,

0

Source: https://habr.com/ru/post/1777444/


All Articles