Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @{
- ViewData["Title"] = "My apps login title";
- // Making this nullable makes it so I don't
- // have to pass a false boolean when I'm not postbacking
- bool? failedLogin = (bool?)ViewData["failedLogin"];
- }
- <h2>My Application's name goes here</h2>
- <form method="post">
- <div class="form-group">
- <div class="form-group">
- <label for="email">Username:</label>
- <input type="text" class="form-control" id="username" name="username">
- </div>
- <div class="form-group">
- <label for="pwd">Password:</label>
- <input type="password" class="form-control" id="password" name="password">
- </div>
- <div class="checkbox">
- <label><input type="checkbox"> Remember me</label>
- </div>
- @if (failedLogin == true)
- {
- <div class="text-danger form-group">
- Invalid Username and/or password
- </div>
- }
- <button type="submit" class="btn btn-default">Submit</button>
- </form>
- [HttpPost]
- [ValidateAntiForgeryToken]
- public IActionResult Index(string username, string password)
- {
- // I don't like seeing (using() {} everywhere in my code. I like to hide the ugliness)
- var matchedUser = ProgramWideFunctions.getUserByUserNameAndPassword(username, password);
- if (matchedUser != null)
- {
- HttpContext.Session.SetString("username", matchedUser.username);
- return RedirectToAction("ControlCentre");
- }
- else
- {
- ViewData["failedLogin"] = true;
- return View();
- }
- }
- public static KAPUsers getUserByUserName(string username)
- {
- using (var db = new AlertsDbContext())
- {
- return db.KAPUsers.Where(b => b.username == username).FirstOrDefault();
- }
- }
- public static KAPUsers getUserByUserNameAndPassword(string username, string password)
- {
- using (var db = new AlertsDbContext())
- {
- return db.KAPUsers.Where(b => b.username == username && b.password == password).FirstOrDefault();
- }
- }
Add Comment
Please, Sign In to add comment