2012-04-24 120 views
0

我得到一個錯誤,並非所有的代碼路徑都返回一個值?並非所有的代碼路徑都返回一個值

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"; 
     } 
    } 
+4

由於您將魔法字符串'Login Failed'與學生ID混合在一起,因此您的設計非常難看。你可以使用'null'作爲失敗的返回值,或者將返回類型改爲更復雜的東西,或許是某種區別聯合。 – CodesInChaos 2012-04-24 09:42:04

+2

你已經得到了你的問題的答案。不過,我建議你稍微重構這一點。不要將'Login Failed'作爲一個字符串返回,而應該返回'null',或者返回一個複雜的類型,表示登錄是否成功,以及學生的ID(或'null')。 – 2012-04-24 09:43:08

+0

可能的重複[爲什麼我得到這個錯誤:不是所有的代碼路徑都返回一個值?](http://stackoverflow.com/questions/929608/why-am-i-getting-this-error-not-all-代碼路徑返回值) – 2012-05-01 18:36:19

回答

6

如果結果不爲空,但result.Password!= HashedPassword你沒有返回任何東西。

你應該更改爲類似:

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

的問題是,你的第一個if語句不保證值的迴歸,由於嵌套的if語句。假設你將結果設置爲一個值(非空),並且你的散列密碼和提供的密碼不匹配,如果你遵循該邏輯,你將無法擊中返回語句。

您應該else子句添加到您的嵌套的if語句,像這樣:

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語句將終止該函數。試想想的最後回報如有的認證步驟失敗


另外要注意使你的代碼的默認操作是,它是不理想的做法以這樣的方式被返回數據的混合。即結果可能是學生ID,或者可能是錯誤消息。考慮創建一個具有多個屬性的專用結果類,調用代碼可以檢查該屬性以查看邏輯驗證的狀態。一類像下面將是一個良好的開端:

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;} 
} 

(話說,雖然,你的調用代碼似乎已經意識到了studentID反正所有)

1

取出東西。只要做到

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

,你也應該在的情況下,返回的東西:

if (HashedPassword != result.Password) 

放在一個else在內,如果

-2

我已經在你的代碼的一些變化。嘗試一下。

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; 
} 
+0

這不會起作用:1)編譯器無法推斷'var yourVar'的類型,因此無法編譯。2)在目前沒有返回路徑的情況下,用戶存在但錯誤的密碼的情況下,你還沒有初始化'yourVar' – Rup 2012-04-24 10:07:09

相關問題