软件的用户身份验证 [英] User authentication for a software

查看:85
本文介绍了软件的用户身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
我想制作一个在启动时对用户进行身份验证的软件.我正在使用Windows窗体和SQL Server.
我用用户名和密码制作了一个名为form1的表格,该表格在软件启动时运行.如果用户身份验证正确,则运行名为form2的下一个窗体,然后关闭form1,但关闭窗体时,应用程序将完全存在.我需要的是,出现form2并关闭表单1而不关闭应用程序.

因此,对于此plz的任何帮助或进行身份验证的任何想法.

在此先多谢.

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 .

推荐答案

可以使用不同的方法.

请勿将任何名称命名为"Form1","Label1"等.立即将所有创建的类型和成员重命名为一些语义名称.因此,您希望具有两种形式:AuthenticationFormMainForm.

一种方法是:
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:
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());
//...



由于表单AuthenticationForm以模式形式运行,因此显示了主表单,但在身份验证过程完成之前无法访问主表单,并且Application.Exit保证如果身份验证失败,将永远不会访问主表单. >
这几乎是标准方式. Kim刚刚发布了一个替代答案,其中从未显示主表格.它需要修改main方法(即输入表单).

Kim提供的解决方案需要修复(请参阅我对他的解决方案的评论).要解决此问题,可能是这样的:



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:

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




—SA


在Program.cs文件中,您可以执行以下操作:

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());
    }
  }
}



如果用户名和密码正确,则可以在AuthUserForm.cs中调用Program.UserAuth = true.



And in AuthUserForm.cs, you can call Program.UserAuth = true, if username and password is correct.


此处是有关登录Winforms应用程序的文章:

WinForm应用程序的用户登录 [ C#Apps中的多个后续主"表单 [ ^ ]
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[^]


这篇关于软件的用户身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆