Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.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#_Sql Server - Fatal编程技术网

C# 我的登录功能不工作

C# 我的登录功能不工作,c#,sql-server,C#,Sql Server,我正在开发一个C#应用程序,它需要一个登录表单,用户和密码保存在SQL Server数据库中 我使用我的CodePass功能对密码进行编码,之前我在我的数据库中添加了一个用户及其编码密码(用户、密码和登录角色保存在数据库中) 现在当我像这样调用我的doLogin函数 doLogin("Arashdn","123"); 它返回0(错误的用户或密码) 调试应用程序后,我发现散列(一个保持从数据库读取加密密码的变量)保存着123个未加密密码 有什么问题吗 这是我的密码: public class D

我正在开发一个C#应用程序,它需要一个登录表单,用户和密码保存在SQL Server数据库中

我使用我的
CodePass
功能对密码进行编码,之前我在我的数据库中添加了一个用户及其编码密码(用户、密码和登录角色保存在数据库中)

现在当我像这样调用我的doLogin函数

doLogin("Arashdn","123");
它返回0(错误的用户或密码) 调试应用程序后,我发现
散列
(一个保持从数据库读取加密密码的变量)保存着123个未加密密码

有什么问题吗

这是我的密码:

public class DB
{
    public static string constr = "Server=localhost;Database=University;
    Integrated Security=true;MultipleActiveResultSets=True;";
    public static string userTable = "Users", userPassword = "Passwd", 
    userName = "UserID", loginRole = "Role";
}

public class login
{
    public int doLogin(string user, string pass)
    {
        string role="0";
        SqlConnection conn = new SqlConnection(DB.constr);
        try
        {
            conn.Open();
            SqlCommand my_cm = conn.CreateCommand();
            SqlDataReader dbread1;
            my_cm.CommandText = "Select " + DB.userPassword + " from " + 
                DB.userTable + " WHERE " + DB.userName + "=" + user;
            dbread1 = my_cm.ExecuteReader();
            string hash="";

            while (dbread1.Read())
            {
               hash = dbread1[0].ToString();
            }

            if (CodePass(user, pass) == hash)
            {
                SqlCommand my_cm2 = conn.CreateCommand();
                SqlDataReader dbread2;
                my_cm2.CommandText = "Select " + DB.loginRole + " from " + 
                DB.userTable + " WHERE " + DB.userName + "=" + user;
                dbread2 = my_cm2.ExecuteReader();

                while (dbread2.Read())
                {
                   role = dbread2[0].ToString(); 
                }
            } 
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            if (conn.State == System.Data.ConnectionState.Open)
                    conn.Close();
        }

        return int.Parse(role);
    }

    public string CodePass(string user, string pass)
    {
        System.Security.Cryptography.SHA1CryptoServiceProvider sha = 
              new SHA1CryptoServiceProvider();
        return System.Text.Encoding.ASCII.GetString(
          sha.ComputeHash(System.Text.Encoding.ASCII.GetBytes(user + pass)));
    }
}
感谢

基于:

调试应用程序后,我发现hash(一个从数据库中保留红色加密密码的变量)包含123个未加密密码。

我建议您查看您的数据库,并根据以下情况将PW字段中的PW从
123
更改为
#:

调试应用程序后,我发现hash(一个从数据库中保留红色加密密码的变量)包含123个未加密密码。

我建议您查看数据库,并将PW字段中的PW从
123
更改为
。我发现了问题
我的计算机上有SQLServerExpress和DeveloperEdition,SQLServerDeveloper中有另一个数据库,其中passwd是123,我的主数据库是SQLServerExpress

愚蠢的我

我发现了问题所在 我的计算机上有SQLServerExpress和DeveloperEdition,SQLServerDeveloper中有另一个数据库,其中passwd是123,我的主数据库是SQLServerExpress


愚蠢的我

很可能数据库中至少有两条记录与用户名匹配,一条密码为“123”

从SqlManager中查看以下结果,请记住替换用户名:

select userID,passwd from Users where userId= '<put the username here>';

很可能数据库中至少有2条记录与用户名匹配,其中一条密码为“123”

从SqlManager中查看以下结果,请记住替换用户名:

select userID,passwd from Users where userId= '<put the username here>';

如果数据库的密码是123,而不是加密的,则结果是正常的。@kostasch。我已经在DB-->#中保存了加密密码,所以它不是
密码传递(“Arashdn”,“123”)==“123”
?抱歉误解。你说密码传递(字符串用户,字符串传递)不等于数据库中的密码?密码传递返回加密密码,密码加密保存在数据库中,但哈希保留未加密密码。如果数据库有密码123,而不是加密密码,则结果正常。@kostasch。我已经在DB-->#中保存了加密密码,所以它不是
密码传递(“Arashdn”,“123”)==“123”
?抱歉误解。所以您说CodePass(string user,string pass)不等于数据库中的密码?CodePass返回加密的密码,密码加密保存在DB中,但哈希保留未加密的密码