Alex Rivera | Logout

Login Wpf ...is it right?

Asked 2009-06-09T03:47:09.783
11

i have a WPF Application with a LoginWindow to access,so i create a Splash Screen for this Login window as follow :

- in App.xaml

< Application x:Class="WPF.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Startup="Application_Startup"
    />

-in App.xaml.cs:

      private void Application_Startup(object sender, StartupEventArgs e)
  {
         Login login = new Login();
        login.Show();
  }

-and in Login.xaml.cs if the log in is succesful :

PrimaryWindow mainWindow= new PrimaryWindow ();

Application.Current.MainWindow = mainWindow;

this.Close();

mainWindow.Show();

.This code is right but sincerely with my poor knowledge i don't know that's a good method to apply for a Login Window or not and i don't know if this method can be "dangerous" for my application that store data from a database and has many features , so i ask you if my way is good or not and if you have a better way can you suggest or show me that?

Thanks for your attention.

Have a lucky day.

Edit
Report

2 Answers

5

Im using this one

    void App_Startup(object sender, StartupEventArgs e)
    {
        this.MainWindow = new MainWindow();

        LoginWindow loginWindow = new LoginWindow();
        if (loginWindow.ShowDialog() ?? false)
        {
            this.MainWindow.Show();
        }
        else
        {
            this.Shutdown();
        }
    }

Or this in case when MainWindow must be created after checking credentials.

    void App_Startup(object sender, StartupEventArgs e)
    {
        this.ShutdownMode = ShutdownMode.OnExplicitShutdown;

        LoginWindow loginWindow = new LoginWindow();
        if (loginWindow.ShowDialog() ?? false)
        {
            this.ShutdownMode = ShutdownMode.OnMainWindowClose;
            this.MainWindow = new MainWindow();
            this.MainWindow.Show();
        }
        else
        {
            this.Shutdown();
        }
    }
answered 2011-04-01T10:41:24.933
0

Looks fine to me - this code by itself isn't 'dangerous'. What might be dangerous is how you collect and process the user's credentials in your login window, but you haven't posted that code so I can't comment.

You could optimize this code a little by using StartupUri="Login.xaml" in your Application tag instead of pointing to an event handler, it would achieve the same effect with less code.

answered 2009-06-09T07:52:57.040

Your Answer