簡體   English   中英

從LINQ SQLite查詢C#返回實際的字符串值

[英]Return actual string value from LINQ SQLite query C#

我目前正在Xamarin.ios上使用C#使用SQLite創建登錄系統。 我的問題是,將散列密碼發送到數據庫可以正常工作。 在登錄方面,我需要取回實際的哈希值,以便可以運行一種方法來對照用戶輸入的值檢查返回的值。

我遇到的問題是查詢實際上並沒有返回所有值,而且我實際上不理解從查詢中得到的結果。 我運行FirstOrDefault以確保僅在用戶迷戀電子郵件后才能得到電子郵件和密碼。 但是,由於我沒有返回字符串值,因此我不確定如何將其放入檢查哈希值的方法中。

希望這是有道理的。 這是運行查詢的代碼:

public void UserLogin()
    {
        string dbPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "PmApp.db3");
        var connection = new SQLiteConnection(dbPath);


        string loginEmail = loginEmailInput.Text;
        string loginPass = loginPasswordInput.Text;

        var emailExists = (from s in connection.Table<SQLiteTables.LoginInfo>()
                           where s.Email.Equals(loginEmail)
                           where s.Password.Equals(loginPass)
                           select s).FirstOrDefault();


        Console.WriteLine(emailExists); //This is just to see what value gets returned
    }

這是返回的值:

2018-05-16 15:57:50.401 SQLite[15735:8043670] SQLite.SQLiteTables+LoginInfo

希望有人可以提供幫助。 傑米

如果只需要返回一列,請嘗試以下操作:

var emailExists = (from s in connection.Table<SQLiteTables.LoginInfo>()
                       where s.Email.Equals(loginEmail)
                       where s.Password.Equals(loginPass)
                       select s.Email).FirstOrDefault();
var loginInfo = (from s in connection.Table<SQLiteTables.LoginInfo>()
                       where s.Email.Equals(loginEmail)
                       select s).FirstOrDefault();

if (loginInfo != null) {
  var dbPass = loginInfo.Password;
  if (hash(dbPass) == hash(loginPass) {
    // hashed passwords match
  }
}

你也可以先做哈希

var hashedPass = hash(loginPass);
var loginInfo = (from s in connection.Table<SQLiteTables.LoginInfo>()
                       where s.Email.Equals(loginEmail)
                       where s.Password.Equals(hashedPass)
                       select s).FirstOrDefault();

if (loginInfo != null) {
  // hashed passwords match
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM