Login Process - Is there a better way?
-
I have an application where a customers may want to restrict access to specific features so I want to have some kind of user database to control access. Now using this 'Login' is optional, the customers may not require/want to restrict access, so there is an option to 'show the login screen' (or not). The idea I had was, if the users do not login using the login screen, there is another option which can restrict the users access because the app logs them in as an 'anonymous' user if the login is not used and privelages can be assigned to the anonymous user. Now it appeared that as this is a windows forms app, the normal start-up form named 'MainForm' was not what I needed. I needed some kind of loop so that the login was shown (if required) and the main form was shown only if a valid user was logged in (this incudes anonymous). I rigged up a couple of test applications and have come up with the following sample code:
private static void Start() { //instanciate a new main menu object MainMenu mainMenu = new MainMenu(); //instanciate a new login object Login login = new Login(); //load the configuration Config.load(); //check if login screen must be used ShowLogin = Config.LoginRequired; //execute the loop if not shutting down while ( !Shutdown ) { //check if the login screen is required if ( ShowLogin ) { //show the login login.ShowDialog(); } else { //login user using anonymous profile UserName = "Anonymous"; } //check if a user was logged in if ( !Shutdown && UserName != null ) { //show the main menu mainMenu.ShowDialog(); } } } [STAThread] public static void Main(string[] args) { //start the application process loop Application.Start(); //start the message pump System.Windows.Forms.Application.Run(); }
Now, as you can see, by using
Application.Run()
I don't start the app by loading a form. Its theStart
method of myApplication
class which actually decides what should be shown. Now what I don't like is: 1) I have to useSystem.Windows.Forms.Application.Exit();
in my main menu and login form if I want to exit the app. 2) Its not very Object Oriented code, it is very procedural in style. My questions are: Is there a better way, perhaps more Object Oriented? Is it good practice to start and end an application in this