Click here to Skip to main content
15,909,440 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I want to make a software that authenticates the user on its start up. I am using Windows Forms and SQL server .
I make a form called form1 with user name and password and this form runs on start up of the software. if user authentication is correct the next form called form2 runs and form1 close but when it closes the application exists totally. what I need is that form2 appears and form 1 closes without closing the application.

so any help with this plz or any ideas to make the authentication.

many thanks in advance .
Posted

There are different ways to do it.

Please never name anything "Form1", "Label1", etc.; renamed all created types and members to some semantic names immediately. So, you want to have two forms: AuthenticationForm and MainForm.

One way is this:
C#
partial class AuthenticationForm {
    internal bool Authenticated { get { /* returns true on success */ } }
} //class AuthenticationForm

//...

partial class MainForm {
    protected override void OnShown(EventArgs e) {
        AuthenticationForm af = new AuthenticationForm();
        af.ShowDialog();
        if (!af.Authenticated)
           Application.Exit();
    } //OnShown
    //...
} //class MainForm

//...
//just to show that MainForm is really a main one:
Application.Run(new MainForm());
//...


As the form AuthenticationForm is run in a modal form, the main form is shown but not accessible before the authentication procedure is complete, and Application.Exit guarantees that the main form will never be accessed if authentication is not successful.

This is pretty much standard way. Kim just posted an alternative answer where the main form is never shown. It requires modification of the main method (which is entry form).

The solution provided by Kim needs a fix (please see my comment to his solution). On way to fix it may be this:

C#
partial class AuthenticationForm {
    internal bool Authenticated { get { /* returns true on success */ } }
} //class AuthenticationForm

//...

static class Program {
    [System.STAThread]
    static void Main() {
        AuthenticationForm af = new AuthenticationForm();
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(af);
        if (!af.Authenticated) 
            exit;
        Application.Run(new MainForm());
    } //Main
} //class Program



—SA
 
Share this answer
 
v3
Comments
Kim Togo 14-May-11 14:37pm    
Good alternative answer SA. 5 from me.
Sergey Alexandrovich Kryukov 14-May-11 14:43pm    
Thank you, Kim. See, I've added a fixed version of your alternative.
Teamwork!
--SA
Sergey Alexandrovich Kryukov 14-May-11 15:30pm    
Not exactly. You need to authenticate separately. It is not a part of your question, so I thought you know how.

You need to program Authenticated get. It should simply return some Boolean field, let's say "fAuth" which should be false until the user presses "Log in" button. When it happens, you system should check up credential the way you should know. It should close your AuthenticationForm, but the value of "fAuth" to true when and only when authentication was successful.

The modal form is already closed, but its instance is still available with the only purpose: to hold Authentication property.

--SA
Gonzalo Cao 14-May-11 15:35pm    
Not to be picky, but if you are creating a modal form, why no use the DialogResult property?? This way, if af.DialogResult == DialogResult.OK you can proceed, if he chose not (or was unable to) authenticate, then you close the application.
khaled377 14-May-11 16:53pm    
thank u for this idea it helped me :D
In your Program.cs file, you can make something like:

static class Program
{
  // Mark UserAuth public, AuthUserForm.cs can now change value of UserAuth.
  public static bool UserAuth = false;

  static void Main(string[] args)
  {
    // Check username and password
    Application.Run(new AuthUserForm());

    if (UserAuth)
    {
      // Username and password correct
      Application.Run(new MainForm());
    }
  }
}


And in AuthUserForm.cs, you can call Program.UserAuth = true, if username and password is correct.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 14-May-11 14:35pm    
Good answer, my 4 (I'll explain your problem below; it's minor, but will need a fix). I just wanted to post exact same solution as alternative to the one I posted, which is more standard. Your method is more clean.

Unfortunately, you did not shows how UserAuth is assigned. It won't work as is, because this static variable is private, AuthUserForm cannot access it. There can be different ways to fix it, but OP may need it.

Please see my solution, it works well but shows main form (which cannot be operated before authentication).
--SA
Kim Togo 15-May-11 1:03am    
Thanks SA. And yes, I forgot to make UserAuth public.
Here's an article about logging in to a Winforms app:

User Login For WinForm Applications[^]

Here's a tip/trick about subsequent main forms in a winforms app:

Multiple Subsequent "Main" Forms in C# Apps[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900