Guest User

Untitled

a guest
Oct 22nd, 2017
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. @{
  2. ViewData["Title"] = "My apps login title";
  3. // Making this nullable makes it so I don't
  4. // have to pass a false boolean when I'm not postbacking
  5. bool? failedLogin = (bool?)ViewData["failedLogin"];
  6. }
  7. <h2>My Application's name goes here</h2>
  8. <form method="post">
  9. <div class="form-group">
  10. <div class="form-group">
  11. <label for="email">Username:</label>
  12. <input type="text" class="form-control" id="username" name="username">
  13. </div>
  14. <div class="form-group">
  15. <label for="pwd">Password:</label>
  16. <input type="password" class="form-control" id="password" name="password">
  17. </div>
  18. <div class="checkbox">
  19. <label><input type="checkbox"> Remember me</label>
  20. </div>
  21. @if (failedLogin == true)
  22. {
  23. <div class="text-danger form-group">
  24. Invalid Username and/or password
  25. </div>
  26. }
  27. <button type="submit" class="btn btn-default">Submit</button>
  28. </form>
  29.  
  30. [HttpPost]
  31. [ValidateAntiForgeryToken]
  32. public IActionResult Index(string username, string password)
  33. {
  34. // I don't like seeing (using() {} everywhere in my code. I like to hide the ugliness)
  35. var matchedUser = ProgramWideFunctions.getUserByUserNameAndPassword(username, password);
  36. if (matchedUser != null)
  37. {
  38. HttpContext.Session.SetString("username", matchedUser.username);
  39. return RedirectToAction("ControlCentre");
  40. }
  41. else
  42. {
  43. ViewData["failedLogin"] = true;
  44. return View();
  45. }
  46. }
  47.  
  48. public static KAPUsers getUserByUserName(string username)
  49. {
  50. using (var db = new AlertsDbContext())
  51. {
  52. return db.KAPUsers.Where(b => b.username == username).FirstOrDefault();
  53. }
  54.  
  55. }
  56.  
  57. public static KAPUsers getUserByUserNameAndPassword(string username, string password)
  58. {
  59. using (var db = new AlertsDbContext())
  60. {
  61. return db.KAPUsers.Where(b => b.username == username && b.password == password).FirstOrDefault();
  62. }
  63.  
  64. }
Add Comment
Please, Sign In to add comment