Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.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
C# 比较登录凭据_C#_Asp.net_Forms - Fatal编程技术网

C# 比较登录凭据

C# 比较登录凭据,c#,asp.net,forms,C#,Asp.net,Forms,你好,我正在处理一个登录表单。我可以在我的网站上注册一个帐户,但当我输入用户名和密码时,它不允许我登录。我有一些代码,假设比较两者,但出于某种原因,只是不起作用。这是我的密码。出于某种原因,它甚至不会在代码底部执行while循环。任何帮助都将不胜感激 private bool CompareStrings(string string1, string string2) { return String.Compare(string1, string2, true, System.Globa

你好,我正在处理一个登录表单。我可以在我的网站上注册一个帐户,但当我输入用户名和密码时,它不允许我登录。我有一些代码,假设比较两者,但出于某种原因,只是不起作用。这是我的密码。出于某种原因,它甚至不会在代码底部执行while循环。任何帮助都将不胜感激

private bool CompareStrings(string string1, string string2)
{
    return String.Compare(string1, string2, true, System.Globalization.CultureInfo.InvariantCulture) == 0 ? true : false;
}

public void LogInAccount(string UserName, string UserPassword, Label InvalidLogIn)
{
    connection.ConnectionString = @"Connection String";
    connection.Open();

    string compare = "select ISNULL(UserName, '') As UserName, ISNULL(UserPassword, '') As UserPassword from SignUp where UserName= @UserName";

    SqlCommand CompareUser = new SqlCommand(compare, connection);
    //SqlCommand Command2 = new SqlCommand("select * from SignUp where FirstName= @FirstName", connection);

    //Command2.Parameters.AddWithValue("@FirsName", FirstName.Text);

    CompareUser.Parameters.AddWithValue("@UserPassword", UserPassword);
    CompareUser.Parameters.AddWithValue("@UserName", UserName);

    SqlDataReader dr = CompareUser.ExecuteReader();

    //string User = UserName;
    //string UserPassword = Password;

    //HtmlAnchor LogIn = (HtmlAnchor)Master.FindControl("LogIn");
    //HtmlAnchor SignUp = (HtmlAnchor)Master.FindControl("SignUp");

    while (dr.Read())
    {
        if (this.CompareStrings(dr["UserName"].ToString(), UserName) &&
             this.CompareStrings(dr["UserPassword"].ToString(), UserPassword))
        {
            InvalidLogIn.Visible = false;
            FormsAuthentication.RedirectFromLoginPage(UserName, true);
        }
        else
        {
            InvalidLogIn.Visible = true;
        }
    }

    connection.Close();
} 

我想说的是:你真的不应该像这样比较用户名/密码

登录身份验证背后的基本思想是:

  • 创建密码时,哈希值存储在 桌子
  • 当用户尝试登录时,他们会提交密码
  • 然后对尝试的密码进行哈希运算
  • 将哈希(尝试)与数据库中的条目进行比较
  • 。。。如果我读对了您的代码,那么密码似乎是以纯文本形式存储在SQL中的?您的查询将检索该值,然后在c#中对其进行比较


    不要误会我的意思-如果这只是一个只有你使用的个人网站,而密码表中只有你的条目?那就没问题了。但是,如果你让其他人在你的网站上输入用户名/密码,你真的需要保护他们的数据。

    嘿,非常感谢你的回复。我真的试过了,但不幸的是没有成功。奇怪的是,有些东西我现在还没有理解,sql代码是有效的(不需要@-这改变了事情的全部含义。有趣的是,为什么它不允许用户登录。当我调试while循环时,它甚至没有被执行。你认为它可能与代码之外的东西有关吗?非常感谢Hanks的帮助,我真的解决了问题你好,谢谢你的输入,是的,这是pLAN将是一个更大的站点,而不仅仅是一个个人站点。我正在研究如何在sql数据库表中散列密码。也许你可以帮助我。在我的标记中,我有textmode=password,因此当用户输入密码时它是隐藏的,但不确定如何在表中获得散列格式。然后,我想要散列的fo要比较的rmat和用户名,以便用户可以登录该站点。感谢您的帮助。一些谷歌搜索会有所帮助,但以下是一些基本信息。创建密码时,生成一个名为Salt的随机字符串。将Salt添加到密码中,然后将其输入到安全哈希算法(如SHA256)。因此,如果用户的密码为“12345”,然后生成“8D3kc93”作为salt,将123458D3kc93输入哈希算法-然后将该输出存储在数据库的一列中,salt存储在另一列中。稍后,当用户想要登录时,您将获得两个SQL列。然后将salt添加到他们的猜测中,然后对其进行哈希-并检查是否匹配。