ASP.NET MVC - 安全

为了学习 ASP.NET MVC,我们将构建一个 Internet 应用程序。

部分 8:添加安全。

MVC 应用程序安全性

Models 文件夹包含表示应用程序模型的类。

Visual Web Developer 自动创建 AccountModels.cs 文件,该文件包含用于应用程序认证的模型。

AccountModels 包含 LogOnModelChangePasswordModel 以及 RegisterModel

Change Password 模型

  1. public class ChangePasswordModel
  2. {
  3.  
  4. [Required]
  5. [DataType(DataType.Password)]
  6. [Display(Name = "Current password")]
  7. public string OldPassword { get; set; }
  8.  
  9. [Required]
  10. [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.",
  11. MinimumLength = 6)]
  12. [DataType(DataType.Password)]
  13. [Display(Name = "New password")]
  14. public string NewPassword { get; set; }
  15.  
  16. [DataType(DataType.Password)]
  17. [Display(Name = "Confirm new password")]
  18. [Compare("NewPassword", ErrorMessage = "The new password and confirmation password
  19. do not match.")]
  20. public string ConfirmPassword { get; set; }
  21.  
  22. }

Logon 模型

  1. public class LogOnModel
  2. {
  3.  
  4. [Required]
  5. [Display(Name = "User name")]
  6. public string UserName { get; set; }
  7.  
  8. [Required]
  9. [DataType(DataType.Password)]
  10. [Display(Name = "Password")]
  11. public string Password { get; set; }
  12.  
  13. [Display(Name = "Remember me?")]
  14. public bool RememberMe { get; set; }
  15.  
  16. }

Register 模型

  1. public class RegisterModel
  2. {
  3.  
  4. [Required]
  5. [Display(Name = "User name")]
  6. public string UserName { get; set; }
  7.  
  8. [Required]
  9. [DataType(DataType.EmailAddress)]
  10. [Display(Name = "Email address")]
  11. public string Email { get; set; }
  12.  
  13. [Required]
  14. [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.",
  15. MinimumLength = 6)]
  16. [DataType(DataType.Password)]
  17. [Display(Name = "Password")]
  18. public string Password { get; set; }
  19.  
  20. [DataType(DataType.Password)]
  21. [Display(Name = "Confirm password")]
  22. [Compare("Password", ErrorMessage = "The password and confirmation password
  23. do not match.")]
  24. public string ConfirmPassword { get; set; }
  25.  
  26. }