简体   繁体   English

我的登录始终成功,无论用户名或密码正确/错误

[英]my login is always successful, whether it is correct/wrong username or password

The login form is using the patient table to login. 登录表单正在使用患者表进行登录。 Whether I type in correct or wrong username/password it always redirect me to the home page which is index.aspx which means sucessful. 无论我输入正确或错误的用户名/密码,它总是将我重定向到主页index.aspx,这意味着成功。 For my below code, I select patientID too because I would like to store the patientID in a session. 在下面的代码中,我也选择了PatientID,因为我想将PatientID存储在会话中。

an integer in c# is never null. C#中的整数永远不会为null。 it can be 0 though. 它可以是0。

int id = Convert.ToInt32(cmd.ExecuteScalar());

id will be 0 in case of no record found so this condition 如果找不到记录,则id将为0,因此这种情况

if (id != null)

will always be true.check if id is not 0 like this 将始终为true。检查id是否为0像这样

if (id <= 0)

Problem : you need to understand that Valu types will never hold null . 问题:您需要了解Valu类型永远不会为null so checking id against null will always retur true hence you are able to login into system in all conditions. 因此,将id与null进行比较将始终为true因此您可以在所有情况下登录系统。

Solution : you should have a mechanism where you can identify the actual value of the ExecuteScalar() . 解决方案:您应该具有一种机制,可以识别ExecuteScalar()的实际值。 it will return null if incase there are no records.but it wont be converted to int as null so you should first check for null against ExecuteScalar() retur value and then if it is not null convert that into integer otherwise assign zero. 如果没有记录,它将返回null 。但是不会将其转换为int为null,因此您应该首先针对ExecuteScalar() retur值检查null,然后如果不为null,则将其转换为整数,否则分配零。

Try This: 尝试这个:

        int id =(cmd.ExecuteScalar()!=null)?Convert.ToInt32(cmd.ExecuteScalar()):0;
        if (id > 0)
        {
            Session.Add("ID", id);
            Session.Add("pUsername", txtUserName.Text);
            Session.Add("pPassword", txtPassword.Text);
            FormsAuthentication.SetAuthCookie(txtUserName.Text, true);
            Response.Redirect("index.aspx");
        }

EDIT: Your system allows Empty UserName and Empty Password as Valid credentials as you you have Id 5 with empty UserName and Password in above table. 编辑:您的系统允许空用户名和空密码作为有效凭据,因为您在上表中具有5的UserNamePassword为空。 so add UserName and Password for userId 5 so that it wont allow empty username and password. 因此,请为userId 5添加UserNamePassword ,以免UserNamePassword为空。

Disclaimer: This won't fix your issue, but it's too long to leave as comment and important enough to what you're doing I felt justified providing it as an answer. 免责声明:这无法解决您的问题,但是留下太多评论作为对您正在做的事情来说足够重要,我觉得有理由提供答案。


The code below is really, really bad: 下面的代码确实非常糟糕:

cmd.CommandText = "SELECT patientID, pUsername, pPassword FROM patient WHERE pUsername='" + user + "' AND pPassword='" + pwd + "'  ";

You MUST do it this way instead: 您必须以这种方式执行此操作:

cmd.CommandText = "SELECT patientID, pUsername, pPassword FROM patient WHERE pUsername= @user and pPassword= @password;"
cmd.Parameters.Add("@user", SqlDbType.NVarChar, 16).Value = user;
cmd.Parameters.Add("@password", SqlDbType.NVarChar, 200).Value = pwd;

You must use Parameters anywhere you substitute user data into a query. 必须在将用户数据替换为查询的任何位置使用“参数”。 No Exceptions . 没有例外 Anything else WILL result in your app being hacked... probably sooner rather than later. 任何其他情况都会导致您的应用程序被黑客入侵……可能迟早会发生。

Additionally, it looks like you're storing passwords in plain text. 此外,您似乎以纯文本形式存储密码。 That's nearly as bad as not using parameters. 这几乎和不使用参数一样糟糕。 The correct way to store passwords is to prepend a per-user salt to the password and then store a cryptographicly secure hash (bcrypt or scrypt) of the result. 正确的密码存储方法是在密码前添加每个用户的密码,然后存储结果的加密安全哈希(bcrypt或scrypt)。 When authenticating a user, you perform the same steps on the attempted password and compare the hash values, not the password directly. 对用户进行身份验证时,您对尝试的密码执行相同的步骤,并比较哈希值,而不是直接比较密码。

Really, you should not be writing authentication code at all. 确实,您根本不应该编写身份验证代码。 Authentication is one of those things where it's easy to build something that seems to work... passes all your tests... but in the end is fundamentally flawed in a subtle way that results in you finding out a year later that you were pwned six months ago. 身份验证是其中的一件事情,它很容易构建看起来可行的东西……通过了所有测试……但是最终从根本上来说,它存在一种微妙的缺陷,导致您在一年后发现自己被伪造了。六个月前。 The best course is to always lean as much as possible on the authentication features provided by your platform of choice. 最好的方法是始终尽可能选择您所选择的平台提供的身份验证功能。 In the case of ASP.Net, that means learning and using the Membership provider features. 对于ASP.Net,这意味着学习和使用成员资格提供程序功能。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试创建一个登录页面,如果用户名正确,它还会显示“只有密码错误” - Trying to create a Login page where it also shows that "Only password is wrong" if username is correct 用户名和密码总是错误的,即使它们在数据库中为真 - username and password always wrong, even if they are true in the database 为什么我的登录表单允许用户使用正确的用户名登录,但密码不正确? - Why my login form allowing users to login with correct USERNAME, but incorrect PASSWORD? 强制 .NET 3.1 中的寡妇身份验证始终提示输入用户名和密码以登录 - Force widows authentication in .NET 3.1 to always prompt for username and password to login 输入正确的用户名和密码后,ASP登录无法正常工作 - ASP login doesn't work when correct username and password are entered mvc3登录表单错误的用户名和密码消息 - mvc3 login form wrong username and password message FtpWebRequest 在使用正确密码成功连接后忽略错误密码 - FtpWebRequest ignores wrong password after previous successful connection using correct password 用户名和密码C#错误 - Wrong username and password C# PrincipalSearcher错误的用户名或密码错误 - PrincipalSearcher Wrong Username Or Password error 由于用户不存在/密码/用户名正确而导致尝试登录网站失败时如何返回消息? - How to return a message when a try to login to the website failed, because the user does not exist/password/username arent correct?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM