Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Login Process - Is there a better way?

Login Process - Is there a better way?

Scheduled Pinned Locked Moved C#
databasewinformsoopquestionworkspace
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    Wayne Phipps
    wrote on last edited by
    #1

    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 the Start method of my Application class which actually decides what should be shown. Now what I don't like is: 1) I have to use System.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

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups