public static User checkToken() { HttpCookie c*k = HttpContext.Current.Request.Cookies["tk"]; if (c*k == null) { return(null); } if (HttpContext.Current.Session["user"] != null) { return((User)HttpContext.Current.Session["user"]); } oylmzEntities db = new oylmzEntities(); Token token = db.Tokens.Where(t => t.tokentext == c*k.Value && t.enddate > DateTime.Now).FirstOrDefault(); if (token == null) { HttpCookie ck = new HttpCookie("tk"); ck.Expires = DateTime.Now.AddDays(-1); HttpContext.Current.Response.Cookies.Add(ck); HttpContext.Current.Session["user"] = token.User; return(null); } return(token.User); }
public ActionResult LogOut() { HttpCookie c*k = HttpContext.Request.Cookies["tk"]; oylmzEntities datas = new oylmzEntities(); Token token = datas.Tokens.Where(t => t.tokentext == c*k.Value && t.enddate > DateTime.Now).FirstOrDefault(); token.enddate = DateTime.Now.AddHours(-2); datas.SaveChanges(); return(RedirectToAction("Index", "Home")); }
public ActionResult Login(admin.Models.LoginViewModel userdata) { if (!ModelState.IsValid) { return(View()); } oylmzEntities datas = new oylmzEntities(); User loginUser = datas.Users.Where(t => t.userName == userdata.userName && t.password == userdata.password).FirstOrDefault(); if (loginUser == null) { ViewBag.hata = "Kullanıcı adı veya şifre hatalı"; return(View()); } Token token = new Token(); do { token.enddate = DateTime.Now.AddHours(2); token.tokentext = RandomSfr.Generate(10); } while (datas.Tokens.Count(t => t.tokentext == token.tokentext) > 0); token.userId = loginUser.Id; datas.Tokens.Add(token); datas.SaveChanges(); HttpCookie c*k = new HttpCookie("tk"); c*k.Value = token.tokentext; c*k.Expires = DateTime.Now.AddHours(2); Response.Cookies.Add(c*k); return(RedirectToAction("Index", "Home")); }