annotate Agendas/trunk/src/Agendas.Web/Models/AccountModels.cs @ 34:475be11edf56

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