0
嗨,我是C#MVC的新手,我遇到问题身份验证登录。希望你能帮忙。身份验证登录 - C#MVC
使用调试模式,似乎我能够访问数据库,并收集了正确的信息登录,但是一旦我重定向,该
@if (this.Context.User.Identity.IsAuthenticated)
被跳过,进入到显示登入码当我想让它显示Account |时阻止登出。 我猜我正在使用错误的代码来做到这一点。
这是我的控制器代码。
[HttpPost]
public ActionResult Login([Bind(Include="Email, Password")] User user)
{
if (ModelState.IsValid)
{
//Get info from database
var result = from u in db.Users
where (u.Email == user.Email && u.Password == user.Password)
select u;
if (result.Count() == 1)
{
FormsAuthentication.SetAuthCookie(user.Email, false);
return RedirectToAction("Index", "Pictures");
}
else
{
ModelState.AddModelError("", "Invalid Username and Password combination");
}
}
return View();
}
局部视图代码
@model Project1.Models.Users
<div id="login">
@if (this.Context.User.Identity.IsAuthenticated)
//***This bit is not validating***
{
@:My Account | Logout
}
else
{
using (Html.BeginForm("Login", "Users"))
{
<span>
@Html.LabelFor(u => u.Email)
@Html.TextBoxFor(u => u.Email)
</span>
<span>
@Html.LabelFor(u => u.Password)
@Html.PasswordFor(u => u.Password)
</span>
<span class="login">
@Html.ActionLink("Register", "Register", "Users")
<input class="login" type="submit" name="login" value="Log In" />
</span>
}
}
我还在学习的基础知识,所以请保持尽可能的简单!我并不担心密码盐和安全性,我只想现在就可以登录。一旦我更有经验,我会让它更安全。 谢谢!
你绝对现货上!非常感谢=) – Ultigma