Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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# Linq查询返回错误。存在显式转换_C#_Linq_Wcf - Fatal编程技术网

C# Linq查询返回错误。存在显式转换

C# Linq查询返回错误。存在显式转换,c#,linq,wcf,C#,Linq,Wcf,我正在处理linq查询。我有一个名为tblUsers的表,它的get列名为username、password、reattamp和isLocked。通过linq查询,我检查用户名和密码,如果用户名帐户被锁定,我想返回false,否则如果用户帐户被解锁,用户名和密码正确,我想返回true。。当我完成查询时,出现以下错误 Severity Code Description Project File Line Suppression State Error CS0266 C

我正在处理linq查询。我有一个名为tblUsers的表,它的get列名为username、password、reattamp和isLocked。通过linq查询,我检查用户名和密码,如果用户名帐户被锁定,我想返回false,否则如果用户帐户被解锁,用户名和密码正确,我想返回true。。当我完成查询时,出现以下错误

Severity    Code    Description Project File    Line    Suppression State
Error   CS0266  Cannot implicitly convert type 'int?' to 'int'. An explicit conversion exists (are you missing a cast?) HalifaxWCFProject   
这是我在ADO.NET中的代码和正在运行的代码

 public bool AuthenticateUser(UserLogin userLogin)
        {
            string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
            using (SqlConnection con = new SqlConnection(CS))
            {
                var result = false;
                SqlCommand cmd = new SqlCommand("spAuthenticateUser", con);
                cmd.CommandType = CommandType.StoredProcedure;
                string encryptedpassword = FormsAuthentication.HashPasswordForStoringInConfigFile(userLogin.Password, "SHA1");
                SqlParameter paramUsername = new SqlParameter("@UserName", userLogin.Username);
                SqlParameter paramPassword = new SqlParameter("@Password", encryptedpassword);
                cmd.Parameters.Add(paramUsername);
                cmd.Parameters.Add(paramPassword);
                con.Open();
                SqlDataReader rdr = cmd.ExecuteReader();
                if (rdr.HasRows)
                {

                    while (rdr.Read())
                    {


                        int RetryAttempts = Convert.ToInt32(rdr["RetryAttempts"]);
                        if (Convert.ToBoolean(rdr["AccountLocked"]))
                        {
                            result = false;
                        }
                        else if (RetryAttempts == 1)
                        {
                            result = false;
                        }
                        else if (RetryAttempts > 1)
                        {
                            int AttemptsLeft = (4 - RetryAttempts);
                            result = true;

                        }

                        else if (Convert.ToBoolean(rdr["Authenticated"]))
                        {
                            result = true;
                        }
                    }


                }
                return result;
            }
        }
这是我的代码linq

 public bool AuthenticateUser1(UserLogin userLogin)
        {
            using (HalifaxDatabaseEntities db = new HalifaxDatabaseEntities())
            {

                var exceeded = false;
                var totalRetries = -1;
                var attamp = from X in db.tblUsers
                             where X.Username == userLogin.Username
                             select X;

                if (attamp.Any())
                {
                    if (attamp.FirstOrDefault().RetryAttempts.HasValue)
                    {
                        totalRetries = attamp.FirstOrDefault().RetryAttempts;//Error on this line 
                        exceeded = totalRetries > 4;
                    }
                }
                return exceeded;

            }
        }

您不能将
int?
类型的值存储到
int
类型的变量中,因为根本无法将null表示为整数,因此会出现错误;要修复编译器错误,可以执行以下操作:

totalRetries = attamp.FirstOrDefault().RetryAttempts.Value;
或:


您不能将
int?
类型的值存储到
int
类型的变量中,因为根本无法将null表示为整数,因此会出现错误;要修复编译器错误,可以执行以下操作:

totalRetries = attamp.FirstOrDefault().RetryAttempts.Value;
或:


根据错误消息,看起来
RetryAttempts
是可为空的int。您的代码正在尝试将其设置为int变量

您可以将变量更改为可为null的int

int? totalRetries=null;
或者修复代码以包含空检查,如果它不为空,则读取
属性

if (attamp.FirstOrDefault().RetryAttempts.HasValue)
{
    totalRetries = attamp.FirstOrDefault().RetryAttempts.Value;
}
使用C#6空条件运算符可以进一步简化代码。实际上,您不需要使用
totalRetries
变量,因为它是一个临时变量,用于导出布尔变量
的值,这就是您的方法返回的值

bool exceeded=false;

var attamp = db.tblUsers.FirstOrDefault(x=>x.UserName == userLogin.Username);

if (attamp?.RetryAttempts != null)
{
    exceeded = attamp.RetryAttempts.Value> 4;
}

根据错误消息,看起来
RetryAttempts
是可为空的int。您的代码正在尝试将其设置为int变量

您可以将变量更改为可为null的int

int? totalRetries=null;
或者修复代码以包含空检查,如果它不为空,则读取
属性

if (attamp.FirstOrDefault().RetryAttempts.HasValue)
{
    totalRetries = attamp.FirstOrDefault().RetryAttempts.Value;
}
使用C#6空条件运算符可以进一步简化代码。实际上,您不需要使用
totalRetries
变量,因为它是一个临时变量,用于导出布尔变量
的值,这就是您的方法返回的值

bool exceeded=false;

var attamp = db.tblUsers.FirstOrDefault(x=>x.UserName == userLogin.Username);

if (attamp?.RetryAttempts != null)
{
    exceeded = attamp.RetryAttempts.Value> 4;
}

RetryAttempts
似乎是可为空的int(
int?
)。你试过这个吗

if (attamp.FirstOrDefault().RetryAttempts.HasValue)
{
    totalRetries = attamp.FirstOrDefault().RetryAttempts.Value; 
    exceeded = totalRetries > 4;
}

您可以在此处阅读有关可空类型的更多信息:

RetryAttempts
似乎是可空int(
int?
)。你试过这个吗

if (attamp.FirstOrDefault().RetryAttempts.HasValue)
{
    totalRetries = attamp.FirstOrDefault().RetryAttempts.Value; 
    exceeded = totalRetries > 4;
}


您可以在此处阅读有关可空类型的更多信息:

我没有得到预期的结果?您是否收到其他错误?你得到了什么结果?你期望得到什么?我期望如果用户输入错误的用户名和密码4次,那么用户帐户将被锁定,访问将被重新写入,否则如果用户在4次内输入正确的用户名和密码,那么重新附加计数将为空并重定向到另一个页面我看不到RetryAttempts所在的代码增加,或设置AccountLocked。这段代码中可能有问题吗?您是否有一个数据库行的重试次数为5次或以上?在您的原始代码中,我看到
intattemptsleft=(4-RetryAttempts)
但是
AttemptsLeft
没有在任何地方使用。对于上面的ado.net,正确的linq将询问什么?我没有得到预期的结果?您是否收到其他错误?你得到了什么结果?你期望得到什么?我期望如果用户输入错误的用户名和密码4次,那么用户帐户将被锁定,访问将被重新写入,否则如果用户在4次内输入正确的用户名和密码,那么重新附加计数将为空并重定向到另一个页面我看不到RetryAttempts所在的代码增加,或设置AccountLocked。这段代码中可能有问题吗?您是否有一个数据库行的重试次数为5次或以上?在您的原始代码中,我看到
intattemptsleft=(4-RetryAttempts)
但是
AttemptsLeft
没有在任何地方使用。对于上面的ado.net,正确的linq查询是什么?非常感谢,但是我没有得到预期的结果。您的意思是,您没有得到预期的结果?你的问题是关于编译错误的,答案中的代码应该可以解决这个问题。请看我回答的第二部分。实际上,您甚至不需要该变量。请检查ADO.NET代码和我正在尝试转换为linq的代码,但它没有给出正确的结果。如果RetryAttest列对该用户具有不可为null的值,并且该值大于4,则我发布的代码将返回true,否则将返回false。您可能还想包括密码检查。它不包括重新连接到sql数据库的次数。非常感谢您,但我没有得到预期的结果。您的意思是,您没有得到预期的结果?你的问题是关于编译错误的,答案中的代码应该可以解决这个问题。请看我回答的第二部分。实际上,您甚至不需要该变量。请检查ADO.NET代码和我正在尝试转换为linq的代码,但它没有给出正确的结果。如果RetryAttest列对该用户具有不可为null的值,并且该值大于4,则我发布的代码将返回true,否则将返回false。您可能还想包括密码检查。它不计算重新附加到sql数据库的次数。当保存密码验证器时,仅使用哈希函数(如SHA-1)是不够的,仅添加一个salt对提高安全性几乎没有作用。相反,使用随机盐在HMAC上迭代大约100ms,并使用散列保存盐。最好使用函数,如
PBKDF2
Rfc2898DeriveBytes
password\u hash
Bcrypt
passlib.hash
或类似函数。关键是让攻击者花费大量时间通过暴力手段查找密码。在保存密码验证器时,仅使用散列函数(如SHA-1)是不够的,仅添加一个salt并不能解决问题