为什么每次我使用正确的用户名和密码登录时,我仍然会收到登录消息“无效的密码或用户名”)? [英] Why is that everytime I login with my correct username and password, I still receive the login message "Invalid password or username")?

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

问题描述

下面是代码:



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

推荐答案

嗯,这真的取决于你,而不是我们。

那里有两个原因:

1) txtpassword.Text 不是山姆e as pass

2) txtusername.Text 与<$ c不同$ c>用户名



由于我们不知道这些值中的任何值,您需要先从调试器。

在上面代码的第一行放一个断点并运行你的程序。

当你尝试登录时,断点应该触发,你的程序将停止。

在VS中,将鼠标悬停在Text属性中的值上,并将传递用户名变量并查看它们包含的值。

如果它们看起来不对,那么回头看看你的代码并尝试找出它们出错的地方。

如果它们看起来正确,请单步调试器并查看代码是否与您同意。如果是,请运行它并查看是否由于某种原因再次执行相同的代码。如果没有,那么再看一遍,而不是更接近...
Well, that's really up to you, not us.
There are two reasons why:
1) txtpassword.Text is not the same as pass
2) txtusername.Text is not the same as username

Since we have no idea what is in any of those values, you need to start by looking at them in the debugger.
Put a breakpoint on the first line of the above code and run your program.
When you try to log in, the breakpoint should trigger and you program will stop.
In VS, hover your mouse over the values in the Text properties, and the pass and username variables and see what values they do contain.
If they look wrong, then look back in your code and try to work out where they went wrong.
If they look right, single step the debugger and see if the code agrees with you. If it does, run it on and see if the same code executes again for some reason. If it doesn't, then look again, rather more closely...


从你的代码示例中我不明白问题出在哪里。但我写了一些如何编码验证用户的方法。如果您这样做并进行单元测试,那么您很容易理解问题所在。



步骤1>您应该将IsUserExists方法写入您的业务层,如下所示



Actaully from your code sample i do not understand where the problem is. But i write some way how to code for authenticating user. If you do so and unit test then easily you understand where the problem is.

step 1> You should write a IsUserExists methold to your business layer as follows

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
        }       
   }
}


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

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