How do I handle Login/Logout in Caliburn.Micro?

Topics: Bootstrappers & IoC, Getting Started, UI Architecture
Aug 17, 2012 at 8:32 AM

** This is a cross-post from **

I'm new to Caliburn.Micro and I'm wondering what is the best way to handle user Login/Logout cycles in my application. I saw some suggestions online to implement this using an empty Shell-View which switches between the LoginView and the main application view, each with a custom ViewModel of course.

I don't really like this solution, because for me these are 2 separate windows with very different properties (Title, Icon, Size) and it seems an unclean solution two change one window to look like the other. Another problem is, that the Login Window comes from an utility library which I don't control and which doesn't use Caliburn.Micro, it's a plain old Window which gives me an event when the user clicks "Login".

I also saw suggestions to display this Dialog in the Bootstrapper startup method, but the problem I see with that is that the user can choose to "Logout" of the application which should display the Login dialog again. It seems wrong to me to handle the switching between the Views in the Bootstrapper.

What I would like is to have some sort of ApplicationViewModel or ApplicationController which works like a Caliburn Conductor, but instead of switching between Views inside a Window, it should switch between the LoginWindow and the MainWindow and should also handle Closing of the whole application (which also requires a Logout). On Activation it would show the LoginWindow, handle the Login event and then switch to the Main Window (Shell). If the user chooses to "LogOut", the event should bubble up to the ApplicationViewModel/Controller again which would deactivate/close the MainWindow, perform the Logout and then show the LoginDialog again. Similar a Close event would do the Logout, but then Shutdown the whole application.

So my questions are:

1. What do you think about this solution and do you have another/better one?
2. How do I implement this? ;-)

Thanks a lot!

Aug 17, 2012 at 10:56 PM
Edited Aug 17, 2012 at 11:00 PM

Hi, I'm also new to Caliburn.Micro but this is something I've been wondering how it would work so I've had a bit of a look at what's available. Have you come across the WindowManager in Caliburn.Micro yet?  I think that this might be able to do what you want it to though having to use a plain Window will main it a bit harder.

By 'plain old Window' do you at least mean a WPF window? or is it a Windows Forms form?  I think in either case you might need to make a sub class of WindowManager and override CreateWindow to do some custom work to create the login window.

I'm going to have a go at creating a demo with this type of setup, If I have any luck I'll post the code.


Just realised I didn't directly answer the questions:

1. My first thought if it was pure WPF with Caliburn.Micro app would be a MainWindow that contains a LoginViewModel.  The WindowManager would then be used to show the login dialog. I still think it might be possible in your design by making the MainWindow invisible by default.

2. I'll come back to you on this one ^_^