ahmed salah

ahmed salah

  • NA
  • 530
  • 141.8k

How to go to loggedIn view if user logged In before

Dec 4 2016 12:53 PM
I need user logged in directly when open my application if he logged in before .
meaning if i write user name sa and password 123 then open web application it will go to loggedIn view if username and password is correct
 when i open my web application again go to  loggedIn view directly without write user name and password(save correct usename and password correct for first time and after this no need to write user name and password)
I working with mvc .
 
AccountController 
  1. [HttpPost]  
  2.        [ValidateAntiForgeryToken]  
  3.        public ActionResult Login(UserAccount user, string ReturnUrl = "")  
  4.        {  
  5.            using (ContainerClass db = new ContainerClass())  
  6.            {  
  7.                var usr = db.userAccount.Where(u => u.UserName == user.UserName && u.Password == user.Password).FirstOrDefault();  
  8.                if (usr != null)  
  9.                {  
  10.                    FormsAuthentication.SetAuthCookie(user.UserName, user.RememberMe);  
  11.                    if (Url.IsLocalUrl(ReturnUrl))  
  12.                    {  
  13.                        //return Redirect(ReturnUrl);  
  14.                       
  15.                    }  
  16.   
  17.   
  18.                    else  
  19.                    {  
  20.                        Session["UserID"] = user.UserID.ToString();  
  21.                        Session["UserName"] = user.UserName.ToString();  
  22.                        return RedirectToAction("LoggedIn");  
  23.                          
  24.                    }  
  25.                }  
  26.                else  
  27.                {  
  28.                    ModelState.AddModelError("""Username or Password is wrong");  
  29.                }  
  30.                  
  31.            }  
  32.            ModelState.Remove("Password");  
  33.            return View();  
  34.        }  
  35.        public ActionResult LoggedIn()  
  36.        {  
  37.   
  38.            if (Session["UserID"] != null)  
  39.            {  
  40.                return View();  
  41.            }  
  42.            else  
  43.            {  
  44.               return  RedirectToAction("login");  
  45.            }  
  46.   
  47.        }  
 LoggedIn view 
  1. @{  
  2.     ViewBag.Title = "LoggedIn";  
  3. }  
  4.   
  5. @if(Session["UserID"]!=null)  
  6. {  
  7.     <h4>Hellow @Session["UserName"].ToString()</h4>  
  8. }  
 This is my model UserAccount
  1.  public class UserAccount  
  2.     {  
  3.         [Key]  
  4.         public int UserID { getset; }  
  5.         [Required(ErrorMessage = "UserName is Required")]  
  6.         public string UserName { getset; }  
  7.         [Required(ErrorMessage = "Email is Required")]  
  8.         [Required(ErrorMessage = "Password is Required")]  
  9.         [DataType(DataType.Password)]  
  10.         public string Password{ getset; }  
  11.         public bool RememberMe { getset; }  
  12.   
  13.     }  
  14. }  
 
I work with mvc code first 

Answers (2)