我試圖從 foreach 回圈回傳密碼以進行一些驗證,但我無法回傳密碼變數。我不斷收到錯誤。我在我的控制器中執行此操作。
我的代碼:
[HttpPost]
public IActionResult Login(userModel user)
{
ViewBag.Password = pwd.GetPassword(user);
string password = "";
foreach(var pwdR in ViewBag.Password.Rows)
{
password = pwdR[1];
}
return password; // Here I get this error: CS0029: Cannot implicitly convert type 'string' to 'Microsoft.AspNetCore.Mvc.IActionResult'
// VALIDATION CODE
....................
}
我究竟做錯了什么?
謝謝!
更新:
[HttpPost]
public IActionResult Login(userModel user)
{
ScryptEncoder enc = new ScryptEncoder();
UserModel pwd = new UserModel();
ViewBag.Password = pwd.GetPassword(user);
string password = "";
foreach(var pwdR in ViewBag.Password.Rows)
{
password = pwdR[1];
}
return password; // Here I get this error: CS0029: Cannot implicitly convert type 'string' to 'Microsoft.AspNetCore.Mvc.IActionResult'
// VALIDATION CODE
bool match = enc.Compare(user.pwd, password);
if (match)
{
ViewBag.Error = "You are now logged in.";
return View();
} else
{
ViewBag.Error = "Login failed.";
return View();
}
}
uj5u.com熱心網友回復:
加載所有用戶并找到您需要的用戶是一個很大的性能錯誤。
試試這個代碼
[HttpPost]
public IActionResult Login(UserModel user)
{
ScryptEncoder enc = new ScryptEncoder();
var userNamePassword= GetUserNamePassword (user) ;
if( userNamePassword==null)
ViewBag.Error = "Login failed. User is not found";
return View();
}
// VALIDATION CODE
bool match = enc.Compare(userNamePassword.Password, password);
if (match)
{
ViewBag.Error = "You are now logged in.";
return View();
} else
{
ViewBag.Error = "Login failed.";
return View();
}
}
將您的模型類更改為此
public class UserNamePasswordModel
{
public string Username { get; set; }
public string Password { get; set; }
}
并將此代碼放置在登錄操作附近的某個位置
private UserNamePasswordModel GetUserNamePassword (UserModel user)
{
UserNamePasswordModel userNamePassword= null;
var connectionString = "Server=localhost;Database=xxxx; uid = xxxx;Password=xxxx;";
using (var connection = new MySqlConnection(connectionString))
{
var command = new MySqlCommand("SELECT UserName, Password FROM User WHERE Username = @Username", connection);
command.Parameters.AddWithValue("@Username", user.Username);
connection.Open();
var reader = command.ExecuteReader();
if (reader.HasRows)
{
if reader.Read()
{
userNamePassword= new UserNamePasswordModel
{
Username= reader.GetString(0),
Password = reader.GetString(1)
};
}
}
reader.Close();
}
}
return userNamePassword;
}
uj5u.com熱心網友回復:
嘗試使用密碼回傳 Ok,例如: return Ok(password);
uj5u.com熱心網友回復:
您應該將驗證代碼包裝在另一個類中,例如UserService,然后將驗證方法呼叫到Login方法中,最后您可以根據驗證結果回傳狀態代碼,例如 Ok() 或 Unauthorized()
更新
在你的解釋之后,如果我沒有錯,你可以試試這個:
[HttpPost]
public IActionResult Login(userModel user)
{
ScryptEncoder enc = new ScryptEncoder();
UserModel pwd = new UserModel();
ViewBag.Password = pwd.GetPassword(user);
foreach(var pwdR in ViewBag.Password.Rows)
{
if (enc.Compare(user.pwd, pwdR[1])){
ViewBag.Error = "You are now logged in.";
return View();
}
}
ViewBag.Error = "Login failed.";
return View();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/353491.html
上一篇:UTF-8洗掉BOM
下一篇:PostgresRDS資料庫DB連接在星期六無限增加,導致SpringBootJavaAPI應用程式中的“JDBCConnectionException”
