CoderForHire
CoderForHire

Reputation: 455

WPF Prism Login Window

I'm using Prism 6 in a WPF 6.0 Core app.

I want to show a Login window, and, upon successful login, hide that and show the main window. If the user clicks 'Log Out', then the process should repeat.

My app uses an authentication service that accepts a credentials entity, and raised a LoginCompleted prism event.

The problem is that when login is complete, and I hide the login window, then the main window briefly flashes and the app closes. I checked App.Current.MainWindow, and it's an instance of the main window. If I don't hide the main window, then the App.Current.MainWindow DOES show, but Login is still up.

Here's what I have so far. See my comments in line.

public IAppSecurity AppSecurity { get; private set; }

protected override Window CreateShell()
{
    return Container.Resolve<MainWindow>();
}


protected override async void OnInitialized()
{
    await TryLogin();
}

protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
    _container = containerRegistry.GetContainer();

    var ea = _container.Resolve<IEventAggregator>();
    ea.GetEvent<LoginCompletedPubSubEvent>().Subscribe(LoginCompleted);

    _container.RegisterInstance<IAppSecurity>(new AppSecurity(ea));

    var regionManager = _container.Resolve<IRegionManager>();
    AppSecurity = _container.Resolve<IAppSecurity>();
}


/// <summary>
/// Called when the Login attempt process has completed
/// </summary>
/// <param name="payload"></param>
private void LoginCompleted(LoginCompletedPubSubEventPayload payload)
{
    switch (payload.Results)
    {
        case LoginResults.Success:

            App.Current.Dispatcher.Invoke(() => 
            {
                _loginWindow.Hide();

                App.Current.MainWindow.Show();
            });
            break;
    }
}

/// <summary>
/// Prompts the user with the Login dialog
/// </summary>
private void PromptUserToLogin()
{
    // Hide the main window before the login window comes up
    Application.Current.MainWindow.Hide();

    // If the login window has not yet been created...
    if (_loginWindow == null)
    {
        // Get dependencies...
        var eventAggregator = _container.Resolve<IEventAggregator>();
        var appSecurity = _container.Resolve<IAppSecurity>();

        // Create and show the login window. The LoginViewModel calls AppSecurity.LoginAsync();
        var vm = new LoginViewModel(eventAggregator, appSecurity);
        _loginWindow.Show();
    }

    // SHow the Login window
    _loginWindow.ShowDialog();

    // While the Login window is up, the user is given 3 chances to
    // login in. If they were not successful, the quit the app
    if (!_isLoggedIn)
    {
        Shutdown();
    }
}

/// <summary>
/// TryLogin attempts to log the user in on app start. The 
/// Authentication service raises a prism event called 
/// LoginComplete, with a LoginResults enum
/// </summary>
private async Task TryLogin()
{
    // If the user has previosuly checked the Remember Me checkbox...
    if (Settings.Default.IsRememberMeChecked)
    {
        // Get the application security service
        AppSecurity = _container.Resolve<IAppSecurity>();
        await Task.Run(() =>
        {
            // Attempt to log the user in
            AppSecurity.LoginAsync();
        });
    }
    else
    {
        // If here, the Remember Me value is False
        PromptUserToLogin(); 
    }
}

Upvotes: 0

Views: 20

Answers (0)

Related Questions