locked
How to go to loggedIn view if user logged In before RRS feed

  • Question

  • 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) 

    AccountController 

    [HttpPost]  
           [ValidateAntiForgeryToken]  
           public ActionResult Login(UserAccount user, string ReturnUrl = "")  
           {  
               using (ContainerClass db = new ContainerClass())  
               {  
                   var usr = db.userAccount.Where(u => u.UserName == user.UserName && u.Password == user.Password).FirstOrDefault();  
                   if (usr != null)  
                   {  
                       FormsAuthentication.SetAuthCookie(user.UserName, user.RememberMe);  
                       if (Url.IsLocalUrl(ReturnUrl))  
                       {  
                           return Redirect(ReturnUrl);  
                          
                       }  
      
      
                       else  
                       {  
                           Session["UserID"] = user.UserID.ToString();  
                           Session["UserName"] = user.UserName.ToString();  
                           return RedirectToAction("LoggedIn");  
                             
                       }  
                   }  
                   else  
                   {  
                       ModelState.AddModelError("", "Username or Password is wrong");  
                   }  
                     
               }  
               ModelState.Remove("Password");  
               return View();  
           }  
           public ActionResult LoggedIn()  
           {  
      
               if (Session["UserID"] != null)  
               {  
                   return View();  
               }  
               else  
               {  
                  return  RedirectToAction("login");  
               }  
      
           }  

    LoggedIn view 

    @{  
        ViewBag.Title = "LoggedIn";  
    }  
      
    @if(Session["UserID"]!=null)  
    {  
        <h4>Hellow @Session["UserName"].ToString()</h4>  
    }  

     This is my model UserAccount

    public class UserAccount  
        {  
            [Key]  
            public int UserID { get; set; }  
            [Required(ErrorMessage = "UserName is Required")]  
            public string UserName { get; set; }  
            [Required(ErrorMessage = "Email is Required")]  
            [Required(ErrorMessage = "Password is Required")]  
            [DataType(DataType.Password)]  
            public string Password{ get; set; }  
            public bool RememberMe { get; set; }  
      
        }  
    }  

    I work with mvc code first 

    Sunday, December 4, 2016 10:13 PM

All replies

  • Hi eng-ahmed-salah,

    Based on your description, it seems that you wan to save username and password in Internet Explorer, please refer to the following steps to achieve it.

        • In Internet Explorer, select the Tools button , and then select Internet options.
        • On the Content tab, under AutoComplete, select Settings.
        • Select the User names and passwords on forms check box, and then select OK.

    For more information, please refer to:

    https://support.microsoft.com/en-sg/help/17499/windows-internet-explorer-11-remember-passwords-fill-out-web-forms

    Best regards,

    Cole Wu


  • MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

Monday, December 5, 2016 2:08 AM