在mvc需要帮助
本文关键字:帮助 mvc | 更新日期: 2023-09-27 17:51:20
我试图使,如果用户登录持有密码"tja"在数据库发送他到一个页面,否则另一个,但不能让它工作。
有谁能帮我输入正确的代码吗?
//This is the code I have now.
public string mypassword = "tja";
// GET: SmsBuys
public ActionResult Index()
{
var getuser = User.Identity.GetUserId();
var getpassword = db.SmsBuys.Where(x => x.code == mypassword);
if (getpassword == getuser) //This is wrong I know
{
return RedirectToAction("Index", "Buys");
}
else
{
return View(db.SmsBuys.ToList().Where(x => x.UserId == User.Identity.GetUserId()));
}
}
处理密码可访问性
当前用户的密码实际上只有在他们将其发布到服务器时才能访问。
因此,您可能需要在实际的POST事件中处理它,在该事件中,您的用户将其适当的登录凭据提交给服务器,以便在应用程序中进行身份验证。
让我们假设你有一个非常基本的表单,看起来像这样的用户登录:
<form action='@Url.Action("SignIn","Account")' method='post'>
<input name='username' placeholder='Username' />
<input name='password' type='password' placeholder='Password' />
<input type='submit' value='Log In' />
</form>
发布后,这将在您的帐户控制器中查找一个SignIn操作,看起来像下面这样,这就是您将处理此逻辑的地方:
[HttpPost]
public ActionResult SignIn(string username, string password)
{
// Compare your password
if(password == yourSpecialPassword)
{
return RedirectToAction("Index", "Buys");
}
// If you are still around, authenticate the user here
}
用户的密码将在password
变量中传递,然后您可以使用该变量与数据库值进行比较,以确定要采取的最佳操作过程。
从数据库访问
此外,如果您正在从数据库中读取密码以进行检查,您可能希望使用FirstOrDefault()
方法提取单个密码,而不是Where()
方法,后者将返回密码的集合:
var getpassword = db.SmsBuys.FirstOrDefault(x => x.code == mypassword);
// If a password was found, check against the posted value
if(password == getpassword)
{
return RedirectToAction("Index", "Buys");
}