首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >验证登录错误

验证登录错误
EN

Stack Overflow用户
提问于 2013-08-19 06:18:44
回答 2查看 438关注 0票数 0

我对登录页面使用了以下C#代码,但我得到了一个错误消息:

“请确保用户名和密码正确”

代码语言:javascript
复制
 protected void btnlogin_Click(object sender, EventArgs e)
    {
        int Results = 0;
        if (txtUsername.Text != string.Empty && txtPassword.Text != string.Empty)
        {
            Results = Validate_Logon(txtUsername.Text.Trim(), txtPassword.Text.Trim());
            if (Results == 1)
            {
                lblMessage.Text = "Login is Good, Send the User to another page or enable controls";
            }
            else
            {
                lblMessage.Text = "Invalid Login";
                lblMessage.ForeColor = System.Drawing.Color.Red;
                //Dont Give too much information this might tell a hacker what is wrong in the login    
            }
        }
        else
        {
            lblMessage.Text = "Please make sure that the username and the password is Correct";
        }
    }
    public int Validate_Logon(String Username, String Password)
    {
        SqlConnection con = new SqlConnection(@"***************");
        SqlCommand cmdselect = new SqlCommand();
        cmdselect.CommandType = CommandType.StoredProcedure;
        cmdselect.CommandText = "[dbo].[Log_Members]";
        cmdselect.Parameters.Add("@Username", SqlDbType.VarChar, 256).Value = Username;
        cmdselect.Parameters.Add("@UPassword", SqlDbType.VarChar, 55).Value = Password;
        cmdselect.Parameters.Add("@OutRes", SqlDbType.Int, 4);
        cmdselect.Parameters["@OutRes"].Direction = ParameterDirection.Output;
        cmdselect.Connection = con;
        int Results = 0;
        try
        {
            con.Open();
            cmdselect.ExecuteNonQuery();
            Results = (int)cmdselect.Parameters["@OutRes"].Value;
        }
        catch (SqlException ex)
        {
            lblMessage.Text = ex.Message;
        }
        finally
        {
            cmdselect.Dispose();
            if (con != null)
            {
                con.Close();
            }
        }
        return Results;
    }

请告诉我上面的代码出了什么问题

EN

回答 2

Stack Overflow用户

发布于 2013-08-19 06:38:32

它非常简单:

代码语言:javascript
复制
if (txtUsername.Text != string.Empty && txtPassword.Text != string.Empty)

这一行返回false,所以else执行,这是:

代码语言:javascript
复制
else
{
    lblMessage.Text = "Please make sure that the username and the password is Correct";
}

调试你的代码。

票数 0
EN

Stack Overflow用户

发布于 2013-08-19 07:51:35

那么,您为什么不检查一下您实际上在用户名和密码文本框中输入了文本

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18304206

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档