ahmed salah

ahmed salah

  • 1.6k
  • 530
  • 123k

How to check user status still login by using User.Identity.

Dec 9 2016 5:49 PM

In code bellow i make login for save username and password in cookies client if it is correct username and password

Actually what i need

I need not using cookies to save username and password In place of cookies i need to using User.Identity.IsAuthenticated in place of cookies

How to do that ?

code below working without any problem

I working in visual studio 2015 with asp.net mvc 4

what i try

for login view get I can restore username and password in login if username and password logged before(no problem in that)

  1. public ActionResult Login()  
  2.         {  
  3.             UserAccount model = new UserAccount();  
  4.             if (Request.Cookies["Login"] != null)  
  5.             {  
  6.                  model.UserName = Request.Cookies["Login"].Values["UserName"];  
  7.                  model.Password = Request.Cookies["Login"].Values["Password"];  
  8.             }  
  9.             return View(model);  

From my code below i can login if username and password is correct also i can save username and password in cookies(no problem in that) .

  1. public ActionResult Login(UserAccount user)  
  2.         {  
  3.             using (ContainerClass db = new ContainerClass())  
  4.             {  
  5.                 var usr = db.userAccount.Where(u => u.UserName == user.UserName && u.Password == user.Password).FirstOrDefault();  
  6.   
  7.                 if (usr != null)  
  8.                 {  
  9.                     FormsAuthentication.SetAuthCookie(user.UserName, user.RememberMe);  
  10.                     Session["UserID"] = user.UserID.ToString();  
  11.                     Session["UserName"] = user.UserName.ToString();  
  12.   
  13.                     if (user.RememberMe)  
  14.                     {  
  15.                         HttpCookie cookie = new HttpCookie("Login");  
  16.                         cookie.Values.Add("UserID", user.UserID.ToString());  
  17.                         cookie.Values.Add("UserName", user.UserName);  
  18.                         cookie.Values.Add("Password", user.Password);  
  19.                         cookie.Expires = DateTime.Now.AddDays(15);  
  20.                         Response.Cookies.Add(cookie);  
  21.   
  22.                     }  
  23.   
  24.                       return RedirectToAction("LoggedIn""Account");  
  25.   
  26.                 }  
  27.                 else  
  28.                 {  
  29.                     ModelState.AddModelError("""Username or Password is wrong");  
  30.                 }  
  31.   
  32.             }  
  33.   
  34.             return View(user);  
  35.         }  


Answers (2)