并非所有代码路径都返回值

本文关键字:路径 返回值 代码 | 更新日期: 2023-09-27 18:32:01

我收到一个错误,并非所有代码路径都返回值?

    public string Authentication(string studentID, string password) // this line?
    {
        var result = students.FirstOrDefault(n => n.StudentID == studentID);
        //find the StudentID that matches the string studentID 
        if (result != null)
        //if result matches then do this
        {
            //---------------------------------------------------------------------------- 
            byte[] passwordHash = Hash(password, result.Salt);
            string HashedPassword = Convert.ToBase64String(passwordHash);
            //----------------------------------------------------------------------------
            // take the specific students salt and generate hash/salt for string password (same way student.Passowrd was created)
            if (HashedPassword == result.Password)
            //check if the HashedPassword (string password) matches the stored student.Password
            {
                return result.StudentID;
                // if it does return the Students ID                     
            } 
        }
        else
        //else return a message saying login failed 
        {
            return "Login Failed";
        }
    }

并非所有代码路径都返回值

如果结果不是空而是结果。密码 != 哈希密码 您不会返回任何内容。

您应该更改为以下内容:

...
if (HashedPassword == result.Password)
{
     return result.StudentID;
     // if it does return the Students ID                     
} 
return "Invalid Password";
...

问题是你的第一个 if 语句不能确保返回值,因为嵌套的 if 语句。假设您将结果设置为一个值(非空),并且您的哈希密码和提供的密码不匹配,如果您遵循该逻辑,您将无法命中 return 语句。

您应该在嵌套的 if 语句中添加一个 else 子句,如下所示:

if (HashedPassword == result.Password)
//check if the HashedPassword (string password) matches the stored student.Password
{
    return result.StudentID;
    // if it does return the Students ID                     
} 
else
{
    return "Login Failed";
}

或者更理想的是,删除您已有的 else 语句,以便函数以返回登录失败结束:

if (result != null)
{
   //....
}
return "Login Failed";

。使用第二种方法,您无需担心使用 ELSE,因为如果满足所有其他条件,嵌套的 return 语句无论如何都会结束函数。尝试将此最终返回视为任何身份验证步骤失败时的默认操作


关于代码的另一个注意事项是,以这种方式返回混合数据并不是理想的做法。 即结果可能是学生证,也可能是错误消息。请考虑创建一个具有多个属性的专用结果类,调用代码可以检查这些属性以查看逻辑验证的状态。像下面这样的类将是一个好的开始:

public class LoginResult
{
   //determines if the login was successful
   public bool Success {get;set;}
   //the ID of the student, perhaps an int datatype would be better?
   public string StudentID {get;set;}
   //the error message (provided the login failed)
   public string ErrorMessage {get;set;}
}

(说了这么多,你的调用代码似乎已经知道学生ID)

删除其他。只是做

if(result != null) {
    ...
}
return "Login Failed";

在以下情况下,您还应该返回一些内容:

if (HashedPassword != result.Password)

在内部放一个 else 如果

我对您的代码进行了一些更改。 试试吧。

public string Authentication(string studentID, string password) 
{
    var result = students.FirstOrDefault(n => n.StudentID == studentID);
    var yourVar;       
    if (result != null)       
    {
        byte[] passwordHash = Hash(password, result.Salt);
        string HashedPassword = Convert.ToBase64String(passwordHash);
        if (HashedPassword == result.Password)            
        {
            //return result.StudentID;
            yourVar = result.StudenID;
            // if it does return the Students ID                     
        } 
    }
    else
    //else return a message saying login failed 
    {
        yourVar = "Login Failed";
    }
    return yourVar;
}