本文介绍了为什么每次我使用正确的用户名和密码登录时,我仍然会收到登录消息“无效的密码或用户名”)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是代码:



Heres the codes:

If txtpassword.Text <> pass Or txtusername.Text <> username Then

                    MsgBox("Invalid password or username,Please Change!", MsgBoxStyle.Information)
                    txtpassword.Clear()
                    txtusername.Clear()
                    trycount += 1
                    If trycount = 3 Then
                        MessageBox.Show("Wrong Password")
                        End
                    End If

                    Exit Sub
                Else

                    With frmMain
                        .lblfullname.Text = fullname
                        .lblposition.Text = position
                    End With
                    MsgBox("Successfully Logged In", MsgBoxStyle.Information)
                    frmMain.Show()
  End If

            Next
        End With

    End Sub

推荐答案


public class UserBusinessService
{
     //You may user dependency injection for writing decouple code.
     private IUserDataService _userDataService = new UserDataService();

     public bool IsUserExists(string userName, string password)
     {
         //if user exists data service return User object otherwise return null
         User user = _userDataService.GetUser(userName, password);
         return user != null;
     }
}



实现这种类型的代码后,您应该对代码进行单元测试,以便您可以理解或不了解任何问题。 br />


从WindowsForm / WPF UI类中,您应该调用它并正确验证用户




After implementing that type of code, you should unit test your code so that you can understand any problem there or not.

From your WindowsForm/WPF UI class you should call it and verify user properly

public class LoginForm:WindowsFormBase
{
   //you may use dependency injection, IOC container, ServiceFactory pattern
   private readonly IUserBusinessService _userService= new UserBusinessService();
   public void Login_Click(object sender, EventArgs e)
   {
        bool success = _userService.IsUserExists(txtUserName.Text, txtPassword.Text);
        if(!success)
        {
            MessageBox.Show("User name/password is incorrect. Please check");
        }
        else
        {
           //write your code after successfully authenticated user
        }
   }
}


这篇关于为什么每次我使用正确的用户名和密码登录时,我仍然会收到登录消息“无效的密码或用户名”)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 09:08