Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用linq to sql在C#中创建登录表单并验证输入_C# - Fatal编程技术网

使用linq to sql在C#中创建登录表单并验证输入

使用linq to sql在C#中创建登录表单并验证输入,c#,C#,我对C#和.NET编程相当陌生。我正在处理一个应用程序,希望创建一个登录表单,如果用户输入其凭据,它将检查数据库中是否存在用户详细信息,然后允许用户访问该应用程序。您现在想知道什么 创建登录表单 让用户输入他的凭据 连接到数据库 检查输入的数据是否正确 如果4为真,则允许访问应用程序,否则拒绝访问 在“登录”按钮中,单击使用: if(IsvalidUser(txtUserName.Text,txtPassword.Text) { //User is valid } 这将为您进行验证

我对C#和.NET编程相当陌生。我正在处理一个应用程序,希望创建一个登录表单,如果用户输入其凭据,它将检查数据库中是否存在用户详细信息,然后允许用户访问该应用程序。

您现在想知道什么

  • 创建登录表单
  • 让用户输入他的凭据
  • 连接到数据库
  • 检查输入的数据是否正确
  • 如果4为真,则允许访问应用程序,否则拒绝访问
  • 在“登录”按钮中,单击使用:

    if(IsvalidUser(txtUserName.Text,txtPassword.Text)
    {
          //User is valid
    }
    

    这将为您进行验证。

    首先,您必须使用登录表单创建项目,您必须在其中连接到数据库,然后,我将在数据库中创建一个过程,该过程将获取参数(用户凭据),并返回用户是否为。
    在我的登录表单中,我将创建一个方法login,该方法将执行该过程。此方法将在LoginClick上使用

    我将开始研究标准认证和授权方法。看一看使用AzMan之类的东西来存储用户、密码和他们的角色等。这可以在xml文件中最初完成,您可以进行加密等。在dbs中存储用户名和密码是值得怀疑的

    我很感激你对点网络(以及一般的编程?)还不熟悉,但你可能也想看看把你的应用程序拆分一下。理想情况下,表单只需显示控件并调用其他组件来实现逻辑和安全性等。请查看UI MOEL,如MVP、MVC等。至少,将安全性逻辑封装到自己的类中:

    public SecurityManager
    {
    
               public static bool UserIsValid(UserDetails user)
               {
                   //Check here in AzMan or your db
               }
    
               public static bool UserIsInRole(string role, UserDetail user)
               {
                   //Check if user is in role (again, in your store or db)
               }
    
    
    
    }
    
    您的表单只需呼叫安全经理即可

    private void Login_OnClick(object sender, EventArgs e)
    {
          UserDetails user = new UserDetails(txtusername.Text,txtPassword.Text);
          if(SecurityManager.IsValiduser(user))
          {
               ///Ok let them in;;;
          }
    
    }
    
    在一个理想的世界里,你会引入一个进行安全检查的预网器


    我希望这有帮助。

    安全应用程序实际上并不存储密码。使用这样的代码来散列密码,这样即使那些有权访问数据库的人也无法确定原始密码

    bool ValidateLogin(DataClasses1DataContext context, string user, string password)
    {
       byte[] providedPasswordHash = hashPassword(password);
       byte[] expectedPasswordHash = context.Users.Where(u => u.Name == user).Single().PasswordHash;
       if (providedPasswordHash.Length != expectedPasswordHash.Length)
          return false;
       for(int i = 0; i < providedPasswordHash.Length; i++)
          if (providedPasswordHash[i] != expectedPasswordHash[i])
             return false;
       return true;
    }
    
    byte[] hashPassword(string password)
    {
       System.Security.Cryptography.SHA1CryptoServiceProvider hasher =
          new System.Security.Cryptography.SHA1CryptoServiceProvider();
       return hasher.ComputeHash(System.Text.Encoding.UTF8.GetBytes(password));
    }
    
    bool ValidateLogin(DataClasses1DataContext上下文、字符串用户、字符串密码)
    {
    字节[]providedPasswordHash=hashPassword(密码);
    byte[]expectedPasswordHash=context.Users.Where(u=>u.Name==user.Single().PasswordHash;
    if(providedPasswordHash.Length!=expectedPasswordHash.Length)
    返回false;
    for(int i=0;i
    你可以试试我的代码

    private void login()
    {
        if (IsvalidUser(txtUsuario.Text, txtPassword.Text))
        {
            //MessageBox.Show("listo");
            Menu ir = new Menu();
            ir.lblUsuario.Text = txtUsuario.Text;
            this.Hide();
            ir.ShowDialog();   
        }
        else
        {
            MessageBox.Show("Incorrecto, verifique sus datos", "Cecom",MessageBoxButtons.OK,MessageBoxIcon.Error); 
        }
    }
    private bool IsvalidUser(string userName, string password)
    {
        DatosDataContext context = new DatosDataContext();
        var q = from p in context.Usuarios
                where p.Usuarios1 == txtUsuario.Text
                && p.Password == txtPassword.Text
                select p;
    
        if (q.Any())
        {
            return true;
        }
        else
        {
            return false;               
        }
    }
    

    你能写下所有这些的来源吗?马吕斯,我想他在给你提供一份工作。请给他报你的小时工资。哪一部分?我的意思是这是整个项目,你对哪个部分感兴趣?欢迎来到StackOverflow。这个问题太宽泛了。请缩小范围。我们不是免费的编码服务。
    private void login()
    {
        if (IsvalidUser(txtUsuario.Text, txtPassword.Text))
        {
            //MessageBox.Show("listo");
            Menu ir = new Menu();
            ir.lblUsuario.Text = txtUsuario.Text;
            this.Hide();
            ir.ShowDialog();   
        }
        else
        {
            MessageBox.Show("Incorrecto, verifique sus datos", "Cecom",MessageBoxButtons.OK,MessageBoxIcon.Error); 
        }
    }
    private bool IsvalidUser(string userName, string password)
    {
        DatosDataContext context = new DatosDataContext();
        var q = from p in context.Usuarios
                where p.Usuarios1 == txtUsuario.Text
                && p.Password == txtPassword.Text
                select p;
    
        if (q.Any())
        {
            return true;
        }
        else
        {
            return false;               
        }
    }