3
votes

Just wanted to know , if there is a way to implement an Access control Message handler in NServiceBus.By 'Access Control Handler' i mean One handler should always execute before other handlers and should control (or rather prevent conditionally the execution of the other handler).

Does someone know how to implement this in NServiceBus?

I have specified the Priority of the handlers to get executed in the EndPointConfig as 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.

1

1 Answers

0
votes

You can by creating your AccessControlHandler like the following

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

The last line is an important one as this tells the bus the message has succeeded but does not pass the message further down the pipeline