Merhaba arkadaşlar,
Asp.Net Mvc bir proje geliştiroyurum. Beni hatırla kısmında ufak bir sıkıntı yaşıyorum.
AccountController Login
public ActionResult Login(LoginModel data, string returnUrl)
{
if (!ModelState.IsValid)
return View(data);
var user = _userService.Login(data.Email, FormsAuthentication.HashPasswordForStoringInConfigFile(data.Password, "md5"));
if (user != null)
{
Session["UserID"] = user.UserID;
Session["UserEmail"] = user.Email;
if(data.IsRemember)
{
HttpCookie onlineCookie = new HttpCookie("OnlineUser");
onlineCookie["UserID"] = user.UserID.ToString();
onlineCookie["UserEmail"] = user.Email;
onlineCookie.Expires = DateTime.Now.AddYears(1);
Response.Cookies.Add(onlineCookie);
}
if (!string.IsNullOrEmpty(returnUrl))
return Redirect(returnUrl);
return RedirectToAction("Index", "Home");
}
ModelState.AddModelError("", "Kullanıcı adı ve şifre yanlış.");
return View(data);
}
Burda cookie ekliyorum.
Global.asax.cs
protected void Session_Start()
{
try
{
var httpCookie = HttpContext.Current.Request.Cookies["OnlineUser"];
if (httpCookie != null && (HttpContext.Current.Session["UserID"] == null || HttpContext.Current.Session["UserEmail"] == null))
{
HttpContext.Current.Session.Add("UserID", httpCookie.Values["UserID"]);
HttpContext.Current.Session.Add("UserEmail", httpCookie.Values["UserEmail"]);
}
else
{
Response.Redirect("/");
}
}
catch
{
Response.Redirect("/");
}
}
burda ise kontrol ettiyorum. Fakat buda sayfa ilk açılışında oldukça yavaşlamaya sebeb oluyor.
Ve birde eğer kullanıcı beni hatırla demez ise session süresi doluktan sonra sayfa hataya düşüyor.Bu cookie ve session kontrolü başka türlü nasıl yapabilirim.