-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserLoginController.cs
58 lines (55 loc) · 1.92 KB
/
UserLoginController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using HomeManagementApplicationMain.Models;
namespace HomeManagementApplicationMain.Controllers
{
[Route("HomeManagementApplicationMain/[controller]")]
public class UserLoginController : Controller
{
// GET: Login
private HMDBContext context;
public UserLoginController(HMDBContext dbContext)
{
context = dbContext;
}
public ActionResult Login()
{
return View();
}
[HttpPost]
public ActionResult Login(UserLogin loginDetails)
{
if (ModelState.IsValid)
{
if (loginDetails != null && !string.IsNullOrEmpty(loginDetails.UserName) &&
!string.IsNullOrEmpty(loginDetails.Password))
{
var userNameInDB = context.UserLogin.Where(x => x.UserName.Equals(loginDetails.UserName)).FirstOrDefault();
if (userNameInDB != null)
{
if (userNameInDB.Password.Equals(loginDetails.Password))
{
RedirectToAction("Main");
}
else
{
ModelState.AddModelError("password", "Incorrect password");
}
}
else
{
ModelState.AddModelError("username", "Incorrect username");
}
}
else
{
ModelState.AddModelError("errorMessage", "Please enter username and password to login");
}
}
return View(loginDetails);
}
}
}