PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/AnotherStartup/App_Code/Account/Login.cs

#
C# | 88 lines | 65 code | 11 blank | 12 comment | 9 complexity | 7221006f50f046a2ba1bd9636aa9a830 MD5 | raw file
  1. using System;
  2. using System.Web.WebPages;
  3. using WebMatrix.WebData;
  4. /// <summary>
  5. /// Summary description for Login
  6. /// </summary>
  7. ///
  8. namespace Account
  9. {
  10. public static class Login
  11. {
  12. public class Model
  13. {
  14. public string Username { get; set; }
  15. public string Password { get; set; }
  16. public bool RememberMe { get; set; }
  17. }
  18. public class Errors
  19. {
  20. public ErrorMessage Username { get; set; }
  21. public ErrorMessage Password { get; set; }
  22. public Errors()
  23. {
  24. Username = ErrorMessage.Nil;
  25. Password = ErrorMessage.Nil;
  26. }
  27. }
  28. public static void Init(WebPageBase web)
  29. {
  30. // Initialize model properties
  31. Model model = web.Page.Model = new Model();
  32. // Initialize validation properties
  33. Errors errors = web.Page.Errors = new Errors();
  34. // If this is a POST request, validate and process data
  35. if (web.IsPost)
  36. {
  37. model.Username = web.Request.Form["username"];
  38. model.Password = web.Request.Form["password"];
  39. model.RememberMe = web.Request.Form["rememberMe"].AsBool();
  40. // Validate the user's username
  41. if (model.Username.IsEmpty())
  42. {
  43. errors.Username = web.Error("You must specify a username.");
  44. }
  45. // Validate the user's password
  46. if (model.Password.IsEmpty())
  47. {
  48. errors.Password = web.Error("You must specify a password.");
  49. }
  50. // Confirm there are no validation errors
  51. if (web.Page.IsValid)
  52. {
  53. if (WebSecurity.UserExists(model.Username) && WebSecurity.GetPasswordFailuresSinceLastSuccess(model.Username) > 4 && WebSecurity.GetLastPasswordFailureDate(model.Username).AddSeconds(60) > DateTime.UtcNow)
  54. {
  55. web.Response.Redirect("~/account/AccountLockedOut");
  56. return;
  57. }
  58. // Attempt to login to the Security object using provided creds
  59. if (WebSecurity.Login(model.Username, model.Password, model.RememberMe))
  60. {
  61. var returnUrl = web.Request.QueryString["ReturnUrl"];
  62. if (returnUrl.IsEmpty())
  63. {
  64. web.Response.Redirect("~/");
  65. }
  66. else
  67. {
  68. web.Context.RedirectLocal(returnUrl);
  69. }
  70. }
  71. // If we arrived here, the login failed; convey that to the user
  72. web.Page.IsValid = false;
  73. }
  74. }
  75. }
  76. }
  77. }