annotate Agendas/trunk/src/Agendas.Web/Models/AccountModels.cs @ 10:c62b77fc33f4

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