[5756] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.ComponentModel;
|
---|
| 4 | using System.ComponentModel.DataAnnotations;
|
---|
| 5 | using System.Globalization;
|
---|
| 6 | using System.Linq;
|
---|
| 7 | using System.Web;
|
---|
| 8 | using System.Web.Mvc;
|
---|
| 9 | using System.Web.Security;
|
---|
| 10 |
|
---|
| 11 | namespace HLWebServiceTestPlugin.Models {
|
---|
| 12 |
|
---|
| 13 | #region Models
|
---|
| 14 | [PropertiesMustMatch("NewPassword", "ConfirmPassword", ErrorMessage = "The new password and confirmation password do not match.")]
|
---|
| 15 | public class ChangePasswordModel {
|
---|
| 16 | [Required]
|
---|
| 17 | [DataType(DataType.Password)]
|
---|
| 18 | [DisplayName("Current password")]
|
---|
| 19 | public string OldPassword { get; set; }
|
---|
| 20 |
|
---|
| 21 | [Required]
|
---|
| 22 | [ValidatePasswordLength]
|
---|
| 23 | [DataType(DataType.Password)]
|
---|
| 24 | [DisplayName("New password")]
|
---|
| 25 | public string NewPassword { get; set; }
|
---|
| 26 |
|
---|
| 27 | [Required]
|
---|
| 28 | [DataType(DataType.Password)]
|
---|
| 29 | [DisplayName("Confirm new password")]
|
---|
| 30 | public string ConfirmPassword { get; set; }
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | public class LogOnModel {
|
---|
| 34 | [Required]
|
---|
| 35 | [DisplayName("User name")]
|
---|
| 36 | public string UserName { get; set; }
|
---|
| 37 |
|
---|
| 38 | [Required]
|
---|
| 39 | [DataType(DataType.Password)]
|
---|
| 40 | [DisplayName("Password")]
|
---|
| 41 | public string Password { get; set; }
|
---|
| 42 |
|
---|
| 43 | [DisplayName("Remember me?")]
|
---|
| 44 | public bool RememberMe { get; set; }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | [PropertiesMustMatch("Password", "ConfirmPassword", ErrorMessage = "The password and confirmation password do not match.")]
|
---|
| 48 | public class RegisterModel {
|
---|
| 49 | [Required]
|
---|
| 50 | [DisplayName("User name")]
|
---|
| 51 | public string UserName { get; set; }
|
---|
| 52 |
|
---|
| 53 | [Required]
|
---|
| 54 | [DataType(DataType.EmailAddress)]
|
---|
| 55 | [DisplayName("Email address")]
|
---|
| 56 | public string Email { get; set; }
|
---|
| 57 |
|
---|
| 58 | [Required]
|
---|
| 59 | [ValidatePasswordLength]
|
---|
| 60 | [DataType(DataType.Password)]
|
---|
| 61 | [DisplayName("Password")]
|
---|
| 62 | public string Password { get; set; }
|
---|
| 63 |
|
---|
| 64 | [Required]
|
---|
| 65 | [DataType(DataType.Password)]
|
---|
| 66 | [DisplayName("Confirm password")]
|
---|
| 67 | public string ConfirmPassword { get; set; }
|
---|
| 68 | }
|
---|
| 69 | #endregion
|
---|
| 70 |
|
---|
| 71 | #region Services
|
---|
| 72 | // The FormsAuthentication type is sealed and contains static members, so it is difficult to
|
---|
| 73 | // unit test code that calls its members. The interface and helper class below demonstrate
|
---|
| 74 | // how to create an abstract wrapper around such a type in order to make the AccountController
|
---|
| 75 | // code unit testable.
|
---|
| 76 |
|
---|
| 77 | public interface IMembershipService {
|
---|
| 78 | int MinPasswordLength { get; }
|
---|
| 79 |
|
---|
| 80 | bool ValidateUser(string userName, string password);
|
---|
| 81 | MembershipCreateStatus CreateUser(string userName, string password, string email);
|
---|
| 82 | bool ChangePassword(string userName, string oldPassword, string newPassword);
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | public class AccountMembershipService : IMembershipService {
|
---|
| 86 | private readonly MembershipProvider _provider;
|
---|
| 87 |
|
---|
| 88 | public AccountMembershipService()
|
---|
| 89 | : this(null) {
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | public AccountMembershipService(MembershipProvider provider) {
|
---|
| 93 | _provider = provider ?? Membership.Provider;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | public int MinPasswordLength {
|
---|
| 97 | get {
|
---|
| 98 | return _provider.MinRequiredPasswordLength;
|
---|
| 99 | }
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | public bool ValidateUser(string userName, string password) {
|
---|
| 103 | if (String.IsNullOrEmpty(userName)) throw new ArgumentException("Value cannot be null or empty.", "userName");
|
---|
| 104 | if (String.IsNullOrEmpty(password)) throw new ArgumentException("Value cannot be null or empty.", "password");
|
---|
| 105 |
|
---|
| 106 | return _provider.ValidateUser(userName, password);
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | public MembershipCreateStatus CreateUser(string userName, string password, string email) {
|
---|
| 110 | if (String.IsNullOrEmpty(userName)) throw new ArgumentException("Value cannot be null or empty.", "userName");
|
---|
| 111 | if (String.IsNullOrEmpty(password)) throw new ArgumentException("Value cannot be null or empty.", "password");
|
---|
| 112 | if (String.IsNullOrEmpty(email)) throw new ArgumentException("Value cannot be null or empty.", "email");
|
---|
| 113 |
|
---|
| 114 | MembershipCreateStatus status;
|
---|
| 115 | _provider.CreateUser(userName, password, email, null, null, true, null, out status);
|
---|
| 116 | return status;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | public bool ChangePassword(string userName, string oldPassword, string newPassword) {
|
---|
| 120 | if (String.IsNullOrEmpty(userName)) throw new ArgumentException("Value cannot be null or empty.", "userName");
|
---|
| 121 | if (String.IsNullOrEmpty(oldPassword)) throw new ArgumentException("Value cannot be null or empty.", "oldPassword");
|
---|
| 122 | if (String.IsNullOrEmpty(newPassword)) throw new ArgumentException("Value cannot be null or empty.", "newPassword");
|
---|
| 123 |
|
---|
| 124 | // The underlying ChangePassword() will throw an exception rather
|
---|
| 125 | // than return false in certain failure scenarios.
|
---|
| 126 | try {
|
---|
| 127 | MembershipUser currentUser = _provider.GetUser(userName, true /* userIsOnline */);
|
---|
| 128 | return currentUser.ChangePassword(oldPassword, newPassword);
|
---|
| 129 | }
|
---|
| 130 | catch (ArgumentException) {
|
---|
| 131 | return false;
|
---|
| 132 | }
|
---|
| 133 | catch (MembershipPasswordException) {
|
---|
| 134 | return false;
|
---|
| 135 | }
|
---|
| 136 | }
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | public interface IFormsAuthenticationService {
|
---|
| 140 | void SignIn(string userName, bool createPersistentCookie);
|
---|
| 141 | void SignOut();
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | public class FormsAuthenticationService : IFormsAuthenticationService {
|
---|
| 145 | public void SignIn(string userName, bool createPersistentCookie) {
|
---|
| 146 | if (String.IsNullOrEmpty(userName)) throw new ArgumentException("Value cannot be null or empty.", "userName");
|
---|
| 147 |
|
---|
| 148 | FormsAuthentication.SetAuthCookie(userName, createPersistentCookie);
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | public void SignOut() {
|
---|
| 152 | FormsAuthentication.SignOut();
|
---|
| 153 | }
|
---|
| 154 | }
|
---|
| 155 | #endregion
|
---|
| 156 |
|
---|
| 157 | #region Validation
|
---|
| 158 | public static class AccountValidation {
|
---|
| 159 | public static string ErrorCodeToString(MembershipCreateStatus createStatus) {
|
---|
| 160 | // See http://go.microsoft.com/fwlink/?LinkID=177550 for
|
---|
| 161 | // a full list of status codes.
|
---|
| 162 | switch (createStatus) {
|
---|
| 163 | case MembershipCreateStatus.DuplicateUserName:
|
---|
| 164 | return "Username already exists. Please enter a different user name.";
|
---|
| 165 |
|
---|
| 166 | case MembershipCreateStatus.DuplicateEmail:
|
---|
| 167 | return "A username for that e-mail address already exists. Please enter a different e-mail address.";
|
---|
| 168 |
|
---|
| 169 | case MembershipCreateStatus.InvalidPassword:
|
---|
| 170 | return "The password provided is invalid. Please enter a valid password value.";
|
---|
| 171 |
|
---|
| 172 | case MembershipCreateStatus.InvalidEmail:
|
---|
| 173 | return "The e-mail address provided is invalid. Please check the value and try again.";
|
---|
| 174 |
|
---|
| 175 | case MembershipCreateStatus.InvalidAnswer:
|
---|
| 176 | return "The password retrieval answer provided is invalid. Please check the value and try again.";
|
---|
| 177 |
|
---|
| 178 | case MembershipCreateStatus.InvalidQuestion:
|
---|
| 179 | return "The password retrieval question provided is invalid. Please check the value and try again.";
|
---|
| 180 |
|
---|
| 181 | case MembershipCreateStatus.InvalidUserName:
|
---|
| 182 | return "The user name provided is invalid. Please check the value and try again.";
|
---|
| 183 |
|
---|
| 184 | case MembershipCreateStatus.ProviderError:
|
---|
| 185 | return "The authentication provider returned an error. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
|
---|
| 186 |
|
---|
| 187 | case MembershipCreateStatus.UserRejected:
|
---|
| 188 | return "The user creation request has been canceled. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
|
---|
| 189 |
|
---|
| 190 | default:
|
---|
| 191 | return "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
|
---|
| 192 | }
|
---|
| 193 | }
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
|
---|
| 197 | public sealed class PropertiesMustMatchAttribute : ValidationAttribute {
|
---|
| 198 | private const string _defaultErrorMessage = "'{0}' and '{1}' do not match.";
|
---|
| 199 | private readonly object _typeId = new object();
|
---|
| 200 |
|
---|
| 201 | public PropertiesMustMatchAttribute(string originalProperty, string confirmProperty)
|
---|
| 202 | : base(_defaultErrorMessage) {
|
---|
| 203 | OriginalProperty = originalProperty;
|
---|
| 204 | ConfirmProperty = confirmProperty;
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | public string ConfirmProperty { get; private set; }
|
---|
| 208 | public string OriginalProperty { get; private set; }
|
---|
| 209 |
|
---|
| 210 | public override object TypeId {
|
---|
| 211 | get {
|
---|
| 212 | return _typeId;
|
---|
| 213 | }
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 | public override string FormatErrorMessage(string name) {
|
---|
| 217 | return String.Format(CultureInfo.CurrentUICulture, ErrorMessageString,
|
---|
| 218 | OriginalProperty, ConfirmProperty);
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | public override bool IsValid(object value) {
|
---|
| 222 | PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value);
|
---|
| 223 | object originalValue = properties.Find(OriginalProperty, true /* ignoreCase */).GetValue(value);
|
---|
| 224 | object confirmValue = properties.Find(ConfirmProperty, true /* ignoreCase */).GetValue(value);
|
---|
| 225 | return Object.Equals(originalValue, confirmValue);
|
---|
| 226 | }
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
|
---|
| 230 | public sealed class ValidatePasswordLengthAttribute : ValidationAttribute {
|
---|
| 231 | private const string _defaultErrorMessage = "'{0}' must be at least {1} characters long.";
|
---|
| 232 | private readonly int _minCharacters = Membership.Provider.MinRequiredPasswordLength;
|
---|
| 233 |
|
---|
| 234 | public ValidatePasswordLengthAttribute()
|
---|
| 235 | : base(_defaultErrorMessage) {
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 | public override string FormatErrorMessage(string name) {
|
---|
| 239 | return String.Format(CultureInfo.CurrentUICulture, ErrorMessageString,
|
---|
| 240 | name, _minCharacters);
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | public override bool IsValid(object value) {
|
---|
| 244 | string valueAsString = value as string;
|
---|
| 245 | return (valueAsString != null && valueAsString.Length >= _minCharacters);
|
---|
| 246 | }
|
---|
| 247 | }
|
---|
| 248 | #endregion
|
---|
| 249 |
|
---|
| 250 | }
|
---|