As others have said, the Authentication Process is not a very good example for an async command and for CQRS in general. In practice sync request/response is probably a better solution for this.
Now to do this (or any other example) with async commands, you would need to send the command and eventualy query for the result of that command.
The part of sending the command is "simple". I would not inject the handler in the controller, but instead inject something capable of dispatching that command like a Bus that allows you to .Send() the command. The implementation of the bus is irrelevant and could vary from in memory direct dispatcher to the handler to real service bus implementation. To keep track of the command it might be a good idea do generate an identifier in the constructor ( like Guid.NewGuid()) and return that as a sign that the command has been dispatched successfully.
Now from the actual handler of the command you need to publish an event, either UserAuthenticated of AuthenticationFailed that contain the CommandId and any other data that might be relevant, like user name, user roles etc.
In the asp.net application you need to have a subscriber for each of the two events.
Here you have two options:
- store the result of the command and wait for the js code to query for it
- use something like SignalR to notify the client code of the event.
After submitting the login information, the client code will get back the command Id. Depending on the option you chose, you can either pool for the login status from javascript every X seconds ( option 1 ) or wait to be notified by a mechanism like SignalR ( option 2 ).
Sometimes option 1 is better, sometimes option 2 is better. If you can estimate a value for the pooling interval that will be fast enough and in most of the cases return the result in the first call then option 1 is best. Option 2 is usually best when in most cases the command is considered to be successful ( like order submission ) and you are only interested in the rare occasions when the command fails.
This may sound complicated for the Authentication process, and it truly is, but in general when you design your process and commands for success, and only have very few failing commands this actually works very well.
For other cases you might need to use a combination of the two options - use option 2 to get a notification that the command has been executed and then query a view model for relevant data.
There is one additional option involving and AsyncController, where you send the command and asynchronously wait for the handler to receive the message. I have not used async controllers for this, so the following is just an opinion, but i just feel that if you need to use an async controller you might be better of with a request-response implementation that uses the async controller to minimise the impact on the webserver.
I must warn you that i've seen CQRS abused and forced upon a process that is designed to be sync request-response ( like CRUD operations ) and the results were systems that dealt mode with the actual infrastructure processing of the events then with the actual business logic.
In the end I highly recommend Udi Dahan's writings on the subject of CQRS and SOA, especially the ones where he warns about the misuse os CQRS.
Hope this helps you step in to the great world of CQRS :)
(this ended up to be a lot longer than expected)