[英]Authenticating a login - C# MVC
Heyo,我是C#MVC的新手,并且在验证登录名时遇到问题。 希望您能提供帮助。
使用调试模式,看来我能够访问数据库并收集正确的信息来登录,但是一旦我被重定向,
@if (this.Context.User.Identity.IsAuthenticated)
当我希望它显示“帐户|”时,将被跳过并转到显示登录代码块 登出。 我猜我正在使用错误的代码来执行此操作。
这是我的控制器代码。
[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>
}
}
我仍在学习基础知识,因此请使其尽可能简单! 我不太担心密码盐和安全性,我只想现在就可以登录。 一旦我变得更有经验,我将使其更加安全。 谢谢!
据我所知看起来不错,但是您在webconfig中有哪些设置? 我在system.web中看起来像这样:
<authentication mode="Forms">
<forms loginUrl="~/Account/LogOn" timeout="2880" cookieless="UseCookies" />
</authentication>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.