i want to create login page for admin and user in MVC5.
if registration time select user then after login automatically goto user panel.
and if admin then automatically go to admin panel in MVC
here is my code for admin.
- public ActionResult Login()
- {
- return View();
- }
- [HttpPost]
- public ActionResult Login(UserMaster_Model user)
- {
- if (ModelState.IsValid)
- {
- var details = (from userlist in _userMaster.GetAll()
- where userlist.USMType == "Admin" && userlist.USMEmail == user.USMEmail && userlist.USMPassword == user.USMPassword
- select new
- {
- userlist.USMUserName,
- userlist.USMId,
- userlist.USMEmail
- }).ToList();
- if (details.FirstOrDefault() != null)
- {
- Session["USMUserName"] = details.FirstOrDefault().USMUserName;
- Session["USMId"] = details.FirstOrDefault().USMId;
- Session["USMEmail"] = details.FirstOrDefault().USMEmail;
- return RedirectToAction("Dashboard");
- }
- else
- {
- ViewBag.ErrorMessage = "Invalid User Name OR Password";
- }
- }
- else
- {
- ViewBag.ErrorMessage = "Enter Username & Password";
- }
- return View(user);
- }
- public ActionResult Login()
- {
- return View();
- }
- [HttpPost]
- public ActionResult Login(UserMaster_Model user)
- {
- if (ModelState.IsValid)
- {
- var details = (from userlist in _userMaster.GetAll()
- where userlist.USMEmail == user.USMEmail && userlist.USMPassword == user.USMPassword
- select new
- {
- userlist.USMId,
- userlist.USMEmail,
- userlist.USMUserName
- }).FirstOrDefault();
- if (details != null)
- {
- Session["USMUserName"] = details.USMUserName;
- Session["USMId"] = details.USMId;
- Session["USMEmail"] = details.USMEmail;
- return RedirectToAction("Index", "CompanyMaster");
- }
- else
- {
- ViewBag.ErrorMessage = "Invalid User Name OR Password";
- }
- }
- else
- {
- ViewBag.ErrorMessage = "Enter Username AND Password";
- }
- return View(user);
- }
both code are in different controller, i want to both code are use in one action one controller How is possible ?
give me answer for only MVC.
thanks in advance


Ketan MokariyaRank #1,793 · 832 reputationPosted Apr 12, 2018, 9:06 PM EDT
When registering user, Store user with its type Admin or User, So when any user login you need to check user its type in db .As you are doing for admin. Better first validate user and then check for type as given below.
Rajesh GamiRank #30 · 41.5K reputationPosted Apr 12, 2018, 9:29 PM EDT