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