【发布时间】:2021-12-21 10:12:24
【问题描述】:
我正在尝试从 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();
}
}
【问题讨论】:
-
因此您可以使用
ActionResult<string>。但无论如何,从安全角度来看,在客户端返回密码确实是个坏主意。更好地在服务器端运行验证。 -
如果您想了解更多关于 asp.net core 中的身份验证的信息,阅读 Microsoft 文档可能会很有用 :) docs.microsoft.com/en-us/aspnet/core/security/authentication/…
-
@mike 我已经阅读并尝试过使用 Identity,但我必须在这个项目中使用 MySql,这使事情变得很复杂。我花了几天时间尝试使用 Identity 进行注册和登录,但我遇到了很多复杂情况,所以我决定这样做。我已经有一个完成的数据库要连接,所以我不想创建新表。如果我可以使用 Sql 数据库,一切都会变得容易得多。
-
@AndrewSilver 我会试试的。谢谢。
-
你能解释一下这个代码字符串密码=“”有什么意义吗? foreach(ViewBag.Password.Rows 中的 var pwdR){ 密码 = pwdR[1]; } ?
标签: c# asp.net asp.net-core .net-core