[HttpPost]
public ActionResult Login(string username, string pass)
{
if (ModelState.IsValid)
{
var newUser = _userRepository.GetUserByNameAndPassword(username, pass);
if (newUser != null)
{
var json = JsonConvert.SerializeObject(newUser);
var userCookie = new HttpCookie("user", json);
userCookie.Expires.AddDays(365);
HttpContext.Response.Cookies.Add(userCookie);
return RedirectToActionPermanent("Index");
}
}
return View("UserLog");
}
LogOut Action
|
public ActionResult UserOut()
{
if (Request.Cookies["user"] != null)
{
var user = new HttpCookie("user")
{
Expires = DateTime.Now.AddDays(-1),
Value = null
};
Response.SetCookie(cookie);
}
return RedirectToActionPermanent("UserLog");
}
@using EShop.Core
@using Newtonsoft.Json
@{
var userInCookie = Request.Cookies["user"];
}
...
@if (userInCookie != null && userInCookie.Value)
{
<li><a href="#">Salam</a></li>
<li><a href="@Url.Action("UserOut", "Home")">Cıxış</a></li>
}
else
{
<li><a href="@Url.Action("UserLog", "Home")">Giriş</a></li>
}
Please put a comment if you have any doubts. Play around with this code and ask any question regarding the issues you faced. See you in the next tutorial.
No comments:
Post a Comment