From my knowledge it looks right, but what settings do you have in your webconfig? Mine looks like this in system.web:
<authentication mode="Forms">
<forms loginUrl="~/Account/LogOn" timeout="2880" cookieless="UseCookies" />
</authentication>
Question
Heyo, I'm new to C# MVC and I'm having a problem Authenticating a login. Hopefully you can help.
Using debug mode, it seems that I am able to access the database and collect the correct information to log in, however once I am redirected, the
@if (this.Context.User.Identity.IsAuthenticated)
is skipped and goes to the show login code block when I want it to display Account | logout. I'm guessing I'm using the wrong code to do this.
Here is my Controller code.
[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();
}
The Partial View Code
@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>
}
}
I'm still learning the basics so please keep it as simple as possible! I'm not too worried about password salts and security I just want to be able to login for now. Once I'm more experienced I will make it more secure. Thanks!
Solution
From my knowledge it looks right, but what settings do you have in your webconfig? Mine looks like this in system.web:
<authentication mode="Forms">
<forms loginUrl="~/Account/LogOn" timeout="2880" cookieless="UseCookies" />
</authentication>