2 // System.Web.UI.WebControls.ChangePassword.cs
5 // Igor Zelmanovich (igorz@mainsoft.com)
6 // Vladimir Krasnov (vladimirk@mainsoft.com)
8 // (C) 2006 Mainsoft, Inc (http://www.mainsoft.com)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Web.Security;
34 using System.Collections.Generic;
35 using System.Collections.Specialized;
37 using System.ComponentModel;
38 using System.Collections;
39 using System.Net.Mail;
41 namespace System.Web.UI.WebControls
43 public class ChangePassword : CompositeControl, INamingContainer
45 public static readonly string CancelButtonCommandName = "Cancel";
46 public static readonly string ChangePasswordButtonCommandName = "ChangePassword";
47 public static readonly string ContinueButtonCommandName = "Continue";
49 Style _cancelButtonStyle = null;
50 Style _changePasswordButtonStyle = null;
51 Style _continueButtonStyle = null;
52 TableItemStyle _failureTextStyle = null;
53 TableItemStyle _hyperLinkStyle = null;
54 TableItemStyle _instructionTextStyle = null;
55 TableItemStyle _labelStyle = null;
56 TableItemStyle _passwordHintStyle = null;
57 TableItemStyle _successTextStyle = null;
58 Style _textBoxStyle = null;
59 TableItemStyle _titleTextStyle = null;
60 Style _validatorTextStyle = null;
62 MailDefinition _mailDefinition = null;
63 MembershipProvider _provider = null;
65 ITemplate _changePasswordTemplate = null;
66 ITemplate _successTemplate = null;
68 Control _changePasswordTemplateContainer = null;
69 Control _successTemplateContainer = null;
71 string _username = null;
72 string _currentPassword = null;
73 string _newPassword = null;
74 string _newPasswordConfirm = null;
76 bool _showContinue = false;
78 #region Public Properties
81 public virtual int BorderPadding
83 get { return ViewState.GetInt ("BorderPadding", 1); }
87 throw new ArgumentOutOfRangeException ();
88 ViewState ["BorderPadding"] = value;
93 public virtual string CancelButtonImageUrl
95 get { return ViewState.GetString ("CancelButtonImageUrl", String.Empty); }
96 set { ViewState ["CancelButtonImageUrl"] = value; }
99 [PersistenceMode (PersistenceMode.InnerProperty)]
100 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
101 [NotifyParentProperty (true)]
102 public Style CancelButtonStyle
106 if (_cancelButtonStyle == null) {
107 _cancelButtonStyle = new Style ();
108 if (IsTrackingViewState)
109 _cancelButtonStyle.TrackViewState ();
111 return _cancelButtonStyle;
116 public virtual string CancelButtonText
118 get { return ViewState.GetString ("CancelButtonText", "Cancel"); }
119 set { ViewState ["CancelButtonText"] = value; }
122 public virtual ButtonType CancelButtonType
124 get { return ViewState ["CancelButtonType"] == null ? ButtonType.Button : (ButtonType) ViewState ["CancelButtonType"]; }
125 set { ViewState ["CancelButtonType"] = value; }
129 public virtual string CancelDestinationPageUrl
131 get { return ViewState.GetString ("CancelDestinationPageUrl", String.Empty); }
132 set { ViewState ["CancelDestinationPageUrl"] = value; }
135 public virtual string ChangePasswordButtonImageUrl
137 get { return ViewState.GetString ("ChangePasswordButtonImageUrl", String.Empty); }
138 set { ViewState ["ChangePasswordButtonImageUrl"] = value; }
142 [PersistenceMode (PersistenceMode.InnerProperty)]
143 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
144 [NotifyParentProperty (true)]
145 public Style ChangePasswordButtonStyle
149 if (_changePasswordButtonStyle == null) {
150 _changePasswordButtonStyle = new Style ();
151 if (IsTrackingViewState)
152 _changePasswordButtonStyle.TrackViewState ();
154 return _changePasswordButtonStyle;
159 public virtual string ChangePasswordButtonText
161 get { return ViewState.GetString ("ChangePasswordButtonText", "Change Password"); }
162 set { ViewState ["ChangePasswordButtonText"] = value; }
165 public virtual ButtonType ChangePasswordButtonType
167 get { return ViewState ["ChangePasswordButtonType"] == null ? ButtonType.Button : (ButtonType) ViewState ["ChangePasswordButtonType"]; }
168 set { ViewState ["ChangePasswordButtonType"] = value; }
172 public virtual string ChangePasswordFailureText
174 get { return ViewState.GetString ("ChangePasswordFailureText", "Your attempt to change passwords was unsuccessful. Please try again."); }
175 set { ViewState ["ChangePasswordFailureText"] = value; }
179 [PersistenceMode (PersistenceMode.InnerProperty)]
180 [TemplateContainer (typeof (ChangePassword))]
181 public virtual ITemplate ChangePasswordTemplate
183 get { return _changePasswordTemplate; }
184 set { _changePasswordTemplate = value; }
188 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
189 public Control ChangePasswordTemplateContainer
193 if (_changePasswordTemplateContainer == null)
194 _changePasswordTemplateContainer = new ChangePasswordContainer (this);
195 return _changePasswordTemplateContainer;
200 public virtual string ChangePasswordTitleText
202 get { return ViewState.GetString ("ChangePasswordTitleText", "Change Your Password"); }
203 set { ViewState ["ChangePasswordTitleText"] = value; }
206 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
210 public virtual string ConfirmNewPassword
212 get { return _newPasswordConfirm != null ? _newPasswordConfirm : ""; }
216 public virtual string ConfirmNewPasswordLabelText
218 get { return ViewState.GetString ("ConfirmNewPasswordLabelText", "Confirm New Password:"); }
219 set { ViewState ["ConfirmNewPasswordLabelText"] = value; }
223 public virtual string ConfirmPasswordCompareErrorMessage
225 get { return ViewState.GetString ("ConfirmPasswordCompareErrorMessage", "The Confirm New Password must match the New Password entry."); }
226 set { ViewState ["ConfirmPasswordCompareErrorMessage"] = value; }
230 public virtual string ConfirmPasswordRequiredErrorMessage
232 get { return ViewState.GetString ("ConfirmPasswordRequiredErrorMessage", String.Empty); }
233 set { ViewState ["ConfirmPasswordRequiredErrorMessage"] = value; }
236 public virtual string ContinueButtonImageUrl
238 get { return ViewState.GetString ("ContinueButtonImageUrl", String.Empty); }
239 set { ViewState ["ContinueButtonImageUrl"] = value; }
242 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
243 [NotifyParentProperty (true)]
244 [PersistenceMode (PersistenceMode.InnerProperty)]
245 public Style ContinueButtonStyle
249 if (_continueButtonStyle == null) {
250 _continueButtonStyle = new Style ();
251 if (IsTrackingViewState)
252 _continueButtonStyle.TrackViewState ();
254 return _continueButtonStyle;
259 public virtual string ContinueButtonText
261 get { return ViewState.GetString ("ContinueButtonText", "Continue"); }
262 set { ViewState ["ContinueButtonText"] = value; }
265 public virtual ButtonType ContinueButtonType
267 get { return ViewState ["ContinueButtonType"] == null ? ButtonType.Button : (ButtonType) ViewState ["ContinueButtonType"]; }
268 set { ViewState ["ContinueButtonType"] = value; }
272 public virtual string ContinueDestinationPageUrl
274 get { return ViewState.GetString ("ContinueDestinationPageUrl", String.Empty); }
275 set { ViewState ["ContinueDestinationPageUrl"] = value; }
278 public virtual string CreateUserIconUrl
280 get { return ViewState.GetString ("CreateUserIconUrl", String.Empty); }
281 set { ViewState ["CreateUserIconUrl"] = value; }
285 public virtual string CreateUserText
287 get { return ViewState.GetString ("CreateUserText", String.Empty); }
288 set { ViewState ["CreateUserText"] = value; }
291 public virtual string CreateUserUrl
293 get { return ViewState.GetString ("CreateUserUrl", String.Empty); }
294 set { ViewState ["CreateUserUrl"] = value; }
297 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
300 public virtual string CurrentPassword
302 get { return _currentPassword != null ? _currentPassword : ""; }
305 [DefaultValue (false)]
306 public virtual bool DisplayUserName
308 get { return ViewState.GetBool ("DisplayUserName", false); }
309 set { ViewState ["DisplayUserName"] = value; }
312 public virtual string EditProfileIconUrl
314 get { return ViewState.GetString ("EditProfileIconUrl", String.Empty); }
315 set { ViewState ["EditProfileIconUrl"] = value; }
319 public virtual string EditProfileText
321 get { return ViewState.GetString ("EditProfileText", String.Empty); }
322 set { ViewState ["EditProfileText"] = value; }
325 public virtual string EditProfileUrl
327 get { return ViewState.GetString ("EditProfileUrl", String.Empty); }
328 set { ViewState ["EditProfileUrl"] = value; }
331 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
332 [NotifyParentProperty (true)]
333 [PersistenceMode (PersistenceMode.InnerProperty)]
334 public TableItemStyle FailureTextStyle
338 if (_failureTextStyle == null) {
339 _failureTextStyle = new TableItemStyle ();
340 if (IsTrackingViewState)
341 _failureTextStyle.TrackViewState ();
343 return _failureTextStyle;
347 public virtual string HelpPageIconUrl
349 get { return ViewState.GetString ("HelpPageIconUrl", String.Empty); }
350 set { ViewState ["HelpPageIconUrl"] = value; }
354 public virtual string HelpPageText
356 get { return ViewState.GetString ("HelpPageText", String.Empty); }
357 set { ViewState ["HelpPageText"] = value; }
360 public virtual string HelpPageUrl
362 get { return ViewState.GetString ("HelpPageUrl", String.Empty); }
363 set { ViewState ["HelpPageUrl"] = value; }
366 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
367 [NotifyParentProperty (true)]
368 [PersistenceMode (PersistenceMode.InnerProperty)]
369 public TableItemStyle HyperLinkStyle
373 if (_hyperLinkStyle == null) {
374 _hyperLinkStyle = new TableItemStyle ();
375 if (IsTrackingViewState)
376 _hyperLinkStyle.TrackViewState ();
378 return _hyperLinkStyle;
383 public virtual string InstructionText
385 get { return ViewState.GetString ("InstructionText", String.Empty); }
386 set { ViewState ["InstructionText"] = value; }
389 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
390 [NotifyParentProperty (true)]
391 [PersistenceMode (PersistenceMode.InnerProperty)]
392 public TableItemStyle InstructionTextStyle
396 if (_instructionTextStyle == null) {
397 _instructionTextStyle = new TableItemStyle ();
398 if (IsTrackingViewState)
399 _instructionTextStyle.TrackViewState ();
401 return _instructionTextStyle;
405 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
406 [NotifyParentProperty (true)]
407 [PersistenceMode (PersistenceMode.InnerProperty)]
408 public TableItemStyle LabelStyle
412 if (_labelStyle == null) {
413 _labelStyle = new TableItemStyle ();
414 if (IsTrackingViewState)
415 _labelStyle.TrackViewState ();
422 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
423 [NotifyParentProperty (true)]
424 [PersistenceMode (PersistenceMode.InnerProperty)]
425 public MailDefinition MailDefinition
429 if (_mailDefinition == null) {
430 _mailDefinition = new MailDefinition ();
431 if (IsTrackingViewState)
432 ((IStateManager) _mailDefinition).TrackViewState ();
434 return _mailDefinition;
440 public virtual string MembershipProvider
444 object o = ViewState ["MembershipProvider"];
445 return (o == null) ? "" : (string) o;
450 ViewState.Remove ("MembershipProvider");
452 ViewState ["MembershipProvider"] = value;
458 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
460 public virtual string NewPassword
462 get { return _newPassword != null ? _newPassword : ""; }
466 public virtual string NewPasswordLabelText
468 get { return ViewState.GetString ("NewPasswordLabelText", "New Password:"); }
469 set { ViewState ["NewPasswordLabelText"] = value; }
472 public virtual string NewPasswordRegularExpression
474 get { return ViewState.GetString ("NewPasswordRegularExpression", String.Empty); }
475 set { ViewState ["NewPasswordRegularExpression"] = value; }
478 public virtual string NewPasswordRegularExpressionErrorMessage
480 get { return ViewState.GetString ("NewPasswordRegularExpressionErrorMessage", String.Empty); }
481 set { ViewState ["NewPasswordRegularExpressionErrorMessage"] = value; }
485 public virtual string NewPasswordRequiredErrorMessage
487 get { return ViewState.GetString ("NewPasswordRequiredErrorMessage", "New Password is required."); }
488 set { ViewState ["NewPasswordRequiredErrorMessage"] = value; }
491 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
492 [NotifyParentProperty (true)]
493 [PersistenceMode (PersistenceMode.InnerProperty)]
494 public TableItemStyle PasswordHintStyle
498 if (_passwordHintStyle == null) {
499 _passwordHintStyle = new TableItemStyle ();
500 if (IsTrackingViewState)
501 _passwordHintStyle.TrackViewState ();
503 return _passwordHintStyle;
508 public virtual string PasswordHintText
510 get { return ViewState.GetString ("PasswordHintText", String.Empty); }
511 set { ViewState ["PasswordHintText"] = value; }
515 public virtual string PasswordLabelText
517 get { return ViewState.GetString ("PasswordLabelText", "Password:"); }
518 set { ViewState ["PasswordLabelText"] = value; }
521 public virtual string PasswordRecoveryIconUrl
523 get { return ViewState.GetString ("PasswordRecoveryIconUrl", String.Empty); }
524 set { ViewState ["PasswordRecoveryIconUrl"] = value; }
528 public virtual string PasswordRecoveryText
530 get { return ViewState.GetString ("PasswordRecoveryText", String.Empty); }
531 set { ViewState ["PasswordRecoveryText"] = value; }
534 public virtual string PasswordRecoveryUrl
536 get { return ViewState.GetString ("PasswordRecoveryUrl", String.Empty); }
537 set { ViewState ["PasswordRecoveryUrl"] = value; }
541 public virtual string PasswordRequiredErrorMessage
543 get { return ViewState.GetString ("PasswordRequiredErrorMessage", String.Empty); }
544 set { ViewState ["PasswordRequiredErrorMessage"] = value; }
548 public virtual string SuccessPageUrl
550 get { return ViewState.GetString ("SuccessPageUrl", String.Empty); }
551 set { ViewState ["SuccessPageUrl"] = value; }
554 [PersistenceMode (PersistenceMode.InnerProperty)]
555 [TemplateContainer (typeof (ChangePassword))]
557 public virtual ITemplate SuccessTemplate
559 get { return _successTemplate; }
560 set { _successTemplate = value; }
564 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
565 public Control SuccessTemplateContainer
569 if (_successTemplateContainer == null)
570 _successTemplateContainer = new SuccessContainer (this);
571 return _successTemplateContainer;
577 public virtual string SuccessText
579 get { return ViewState.GetString ("SuccessText", "Your password has been changed!"); }
580 set { ViewState ["SuccessText"] = value; }
583 [NotifyParentProperty (true)]
584 [PersistenceMode (PersistenceMode.InnerProperty)]
585 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
586 public TableItemStyle SuccessTextStyle
590 if (_successTextStyle == null) {
591 _successTextStyle = new TableItemStyle ();
592 if (IsTrackingViewState)
593 _successTextStyle.TrackViewState ();
595 return _successTextStyle;
600 public virtual string SuccessTitleText
602 get { return ViewState.GetString ("SuccessTitleText", "Change Password Complete"); }
603 set { ViewState ["SuccessTitleText"] = value; }
606 [NotifyParentProperty (true)]
607 [PersistenceMode (PersistenceMode.InnerProperty)]
608 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
609 public Style TextBoxStyle
613 if (_textBoxStyle == null) {
614 _textBoxStyle = new Style ();
615 if (IsTrackingViewState)
616 _textBoxStyle.TrackViewState ();
618 return _textBoxStyle;
622 [NotifyParentProperty (true)]
623 [PersistenceMode (PersistenceMode.InnerProperty)]
624 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
625 public TableItemStyle TitleTextStyle
629 if (_titleTextStyle == null) {
630 _titleTextStyle = new TableItemStyle ();
631 if (IsTrackingViewState)
632 _titleTextStyle.TrackViewState ();
634 return _titleTextStyle;
639 public virtual string UserName
643 if (_username == null && HttpContext.Current.Request.IsAuthenticated)
644 _username = HttpContext.Current.User.Identity.Name;
646 return _username != null ? _username : "";
648 set { _username = value; }
652 public virtual string UserNameLabelText
654 get { return ViewState.GetString ("UserNameLabelText", "User Name:"); }
655 set { ViewState ["UserNameLabelText"] = value; }
659 public virtual string UserNameRequiredErrorMessage
661 get { return ViewState.GetString ("UserNameRequiredErrorMessage", "User Name is required."); }
662 set { ViewState ["UserNameRequiredErrorMessage"] = value; }
665 [NotifyParentProperty (true)]
666 [PersistenceMode (PersistenceMode.InnerProperty)]
667 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
668 public Style ValidatorTextStyle
672 if (_validatorTextStyle == null) {
673 _validatorTextStyle = new Style ();
674 if (IsTrackingViewState)
675 _validatorTextStyle.TrackViewState ();
677 return _validatorTextStyle;
683 #region Protected Methods
685 protected internal override void CreateChildControls ()
689 ITemplate cpTemplate = ChangePasswordTemplate;
690 if (cpTemplate == null)
691 cpTemplate = new ChangePasswordDeafultTemplate (this);
692 ((ChangePasswordContainer) ChangePasswordTemplateContainer).InstantiateTemplate (cpTemplate);
694 ITemplate sTemplate = SuccessTemplate;
695 if (sTemplate == null)
696 sTemplate = new SuccessDefaultTemplate (this);
697 ((SuccessContainer) SuccessTemplateContainer).InstantiateTemplate (sTemplate);
699 Controls.AddAt (0, ChangePasswordTemplateContainer);
700 Controls.AddAt (1, SuccessTemplateContainer);
702 IEditableTextControl editable;
704 ChangePasswordContainer container = (ChangePasswordContainer) ChangePasswordTemplateContainer;
705 if (DisplayUserName) {
706 editable = container.UserNameTextBox;
707 if (editable != null)
708 editable.TextChanged += new EventHandler (UserName_TextChanged);
711 editable = container.CurrentPasswordTextBox;
712 if (editable != null)
713 editable.TextChanged += new EventHandler (CurrentPassword_TextChanged);
715 editable = container.NewPasswordTextBox;
716 if (editable != null)
717 editable.TextChanged += new EventHandler (NewPassword_TextChanged);
719 editable = container.ConfirmNewPasswordTextBox;
720 if (editable != null)
721 editable.TextChanged += new EventHandler (NewPasswordConfirm_TextChanged);
724 protected internal override void Render (HtmlTextWriter writer)
726 for (int i = 0; i < Controls.Count; i++)
727 if (Controls [i].Visible)
728 Controls [i].Render (writer);
733 #region Private Methods
735 [MonoTODO ("Not implemented")]
736 protected override void SetDesignModeState (IDictionary data)
738 throw new NotImplementedException ();
741 private void InitMemberShipProvider ()
743 string mp = MembershipProvider;
744 _provider = (mp.Length == 0) ? Membership.Provider : Membership.Providers [mp];
745 if (_provider == null)
746 throw new HttpException (Locale.GetText ("No provider named '{0}' could be found.", mp));
749 private void ProcessChangePasswordEvent (CommandEventArgs args)
754 LoginCancelEventArgs loginCancelEventArgs = new LoginCancelEventArgs ();
755 OnChangingPassword (loginCancelEventArgs);
756 if (loginCancelEventArgs.Cancel)
761 res = MembershipProviderInternal.ChangePassword (UserName, CurrentPassword, NewPassword);
\r
767 OnChangedPassword (args);
768 _showContinue = true;
770 if (_mailDefinition != null)
771 SendMail (UserName, NewPassword);
774 OnChangePasswordError (EventArgs.Empty);
775 string lastError = string.Format (
776 "Password incorrect or New Password invalid. New Password length minimum: {0}. Non-alphanumeric characters required: {1}.",
777 MembershipProviderInternal.MinRequiredPasswordLength,
778 MembershipProviderInternal.MinRequiredNonAlphanumericCharacters);
780 ChangePasswordContainer container = (ChangePasswordContainer) ChangePasswordTemplateContainer;
781 container.FailureTextLiteral.Text = lastError;
782 _showContinue = false;
788 private void ProcessCancelEvent (CommandEventArgs args)
790 OnCancelButtonClick (args);
792 if (ContinueDestinationPageUrl.Length > 0)
793 Context.Response.Redirect (ContinueDestinationPageUrl);
798 private void ProcessContinueEvent (CommandEventArgs args)
800 OnContinueButtonClick (args);
802 if (ContinueDestinationPageUrl.Length > 0)
803 Context.Response.Redirect (ContinueDestinationPageUrl);
808 private void SendMail (string username, string password)
810 MembershipUser user = MembershipProviderInternal.GetUser (UserName, false);
814 ListDictionary dictionary = new ListDictionary ();
815 dictionary.Add ("<%USERNAME%>", username);
816 dictionary.Add ("<%PASSWORD%>", password);
818 MailMessage message = MailDefinition.CreateMailMessage (user.Email, dictionary, this);
820 MailMessageEventArgs args = new MailMessageEventArgs (message);
821 OnSendingMail (args);
823 SmtpClient smtpClient = new SmtpClient ();
825 smtpClient.Send (message);
827 catch (Exception e) {
828 SendMailErrorEventArgs mailArgs = new SendMailErrorEventArgs (e);
829 OnSendMailError (mailArgs);
830 if (!mailArgs.Handled)
839 protected override HtmlTextWriterTag TagKey
841 get { return HtmlTextWriterTag.Table; }
844 internal virtual MembershipProvider MembershipProviderInternal
848 if (_provider == null)
849 InitMemberShipProvider ();
857 #region View and Control State
859 protected internal override void LoadControlState (object savedState)
861 if (savedState == null) return;
862 object [] state = (object []) savedState;
863 base.LoadControlState (state [0]);
865 _showContinue = (bool) state [1];
866 _username = (string) state [2];
869 protected internal override object SaveControlState ()
871 object state = base.SaveControlState ();
872 return new object [] { state, _showContinue, _username };
875 protected override void LoadViewState (object savedState)
878 if (savedState == null)
881 object [] states = (object []) savedState;
882 base.LoadViewState (states [0]);
884 if (states [1] != null)
885 CancelButtonStyle.LoadViewState (states [1]);
886 if (states [2] != null)
887 ChangePasswordButtonStyle.LoadViewState (states [2]);
888 if (states [3] != null)
889 ContinueButtonStyle.LoadViewState (states [3]);
891 if (states [4] != null)
892 FailureTextStyle.LoadViewState (states [4]);
893 if (states [5] != null)
894 HyperLinkStyle.LoadViewState (states [5]);
895 if (states [6] != null)
896 InstructionTextStyle.LoadViewState (states [6]);
898 if (states [7] != null)
899 LabelStyle.LoadViewState (states [7]);
900 if (states [8] != null)
901 PasswordHintStyle.LoadViewState (states [8]);
902 if (states [9] != null)
903 SuccessTextStyle.LoadViewState (states [9]);
905 if (states [10] != null)
906 TextBoxStyle.LoadViewState (states [10]);
907 if (states [11] != null)
908 TitleTextStyle.LoadViewState (states [11]);
909 if (states [12] != null)
910 ValidatorTextStyle.LoadViewState (states [12]);
912 if (states [13] != null)
913 ((IStateManager) MailDefinition).LoadViewState (states [13]);
916 protected override object SaveViewState ()
918 object [] states = new object [14];
919 states [0] = base.SaveViewState ();
921 if (_cancelButtonStyle != null)
922 states [1] = _cancelButtonStyle.SaveViewState ();
923 if (_changePasswordButtonStyle != null)
924 states [2] = _changePasswordButtonStyle.SaveViewState ();
925 if (_continueButtonStyle != null)
926 states [3] = _continueButtonStyle.SaveViewState ();
928 if (_failureTextStyle != null)
929 states [4] = _failureTextStyle.SaveViewState ();
930 if (_hyperLinkStyle != null)
931 states [5] = _hyperLinkStyle.SaveViewState ();
932 if (_instructionTextStyle != null)
933 states [6] = _instructionTextStyle.SaveViewState ();
935 if (_labelStyle != null)
936 states [7] = _labelStyle.SaveViewState ();
937 if (_passwordHintStyle != null)
938 states [8] = _passwordHintStyle.SaveViewState ();
939 if (_successTextStyle != null)
940 states [9] = _successTextStyle.SaveViewState ();
942 if (_textBoxStyle != null)
943 states [10] = _textBoxStyle.SaveViewState ();
944 if (_titleTextStyle != null)
945 states [11] = _titleTextStyle.SaveViewState ();
946 if (_validatorTextStyle != null)
947 states [12] = _validatorTextStyle.SaveViewState ();
949 if (_mailDefinition != null)
950 states [13] = ((IStateManager) _mailDefinition).SaveViewState ();
952 for (int i = 0; i < states.Length; i++) {
953 if (states [i] != null)
959 protected override void TrackViewState ()
962 base.TrackViewState ();
964 if (_cancelButtonStyle != null)
965 _cancelButtonStyle.TrackViewState ();
966 if (_changePasswordButtonStyle != null)
967 _changePasswordButtonStyle.TrackViewState ();
968 if (_continueButtonStyle != null)
969 _continueButtonStyle.TrackViewState ();
971 if (_failureTextStyle != null)
972 _failureTextStyle.TrackViewState ();
973 if (_hyperLinkStyle != null)
974 _hyperLinkStyle.TrackViewState ();
975 if (_instructionTextStyle != null)
976 _instructionTextStyle.TrackViewState ();
978 if (_labelStyle != null)
979 _labelStyle.TrackViewState ();
980 if (_passwordHintStyle != null)
981 _passwordHintStyle.TrackViewState ();
982 if (_successTextStyle != null)
983 _successTextStyle.TrackViewState ();
985 if (_textBoxStyle != null)
986 _textBoxStyle.TrackViewState ();
987 if (_titleTextStyle != null)
988 _titleTextStyle.TrackViewState ();
989 if (_validatorTextStyle != null)
990 _validatorTextStyle.TrackViewState ();
992 if (_mailDefinition != null)
993 ((IStateManager) _mailDefinition).TrackViewState ();
998 #region Public Events
1000 public event EventHandler CancelButtonClick;
1001 public event EventHandler ChangedPassword;
1002 public event EventHandler ChangePasswordError;
1003 public event LoginCancelEventHandler ChangingPassword;
1004 public event EventHandler ContinueButtonClick;
1005 public event MailMessageEventHandler SendingMail;
1006 public event SendMailErrorEventHandler SendMailError;
1010 #region Event Handlers
1012 protected override bool OnBubbleEvent (object source, EventArgs e)
1014 CommandEventArgs args = e as CommandEventArgs;
1016 if (args.CommandName == ChangePasswordButtonCommandName) {
1017 ProcessChangePasswordEvent (args);
1020 if (args.CommandName == CancelButtonCommandName) {
1021 ProcessCancelEvent (args);
1024 if (args.CommandName == ContinueButtonCommandName) {
1025 ProcessContinueEvent (args);
1029 return base.OnBubbleEvent (source, e);
1032 protected virtual void OnCancelButtonClick (EventArgs e)
1034 if (CancelButtonClick != null)
1035 CancelButtonClick (this, e);
1038 protected virtual void OnChangedPassword (EventArgs e)
1040 if (ChangedPassword != null)
1041 ChangedPassword (this, e);
1044 protected virtual void OnChangePasswordError (EventArgs e)
1046 if (ChangePasswordError != null)
1047 ChangePasswordError (this, e);
1050 protected virtual void OnChangingPassword (LoginCancelEventArgs e)
1052 if (ChangingPassword != null)
1053 ChangingPassword (this, e);
1056 protected virtual void OnContinueButtonClick (EventArgs e)
1058 if (ContinueButtonClick != null)
1059 ContinueButtonClick (this, e);
1062 protected internal override void OnInit (EventArgs e)
1064 Page.RegisterRequiresControlState (this);
1068 protected internal override void OnPreRender (EventArgs e)
1070 ChangePasswordTemplateContainer.Visible = !_showContinue;
1071 SuccessTemplateContainer.Visible = _showContinue;
1072 base.OnPreRender (e);
1075 protected virtual void OnSendingMail (MailMessageEventArgs e)
1077 if (SendingMail != null)
1078 SendingMail (this, e);
1081 protected virtual void OnSendMailError (SendMailErrorEventArgs e)
1083 if (SendMailError != null)
1084 SendMailError (this, e);
1087 private void UserName_TextChanged (object sender, EventArgs e)
1089 UserName = ((ITextControl) sender).Text;
1092 private void CurrentPassword_TextChanged (object sender, EventArgs e)
1094 _currentPassword = ((ITextControl) sender).Text;
1097 private void NewPassword_TextChanged (object sender, EventArgs e)
1099 _newPassword = ((ITextControl) sender).Text;
1102 private void NewPasswordConfirm_TextChanged (object sender, EventArgs e)
1104 _newPasswordConfirm = ((ITextControl) sender).Text;
1109 class BaseChangePasswordContainer : Table, INamingContainer
1111 protected readonly ChangePassword _owner = null;
1112 TableCell _containerCell = null;
1114 public BaseChangePasswordContainer (ChangePassword owner)
1117 SetBindingContainer (false);
1121 public void InstantiateTemplate (ITemplate template)
1123 template.InstantiateIn (_containerCell);
1126 private void InitTable ()
1128 Attributes.Add ("ID", _owner.ID);
1131 CellPadding = _owner.BorderPadding;
1133 _containerCell = new TableCell ();
1135 TableRow row = new TableRow ();
1136 row.Cells.Add (_containerCell);
1140 protected internal override void OnPreRender (EventArgs e)
1142 ApplyStyle (_owner.ControlStyle);
1143 base.OnPreRender (e);
1147 sealed class ChangePasswordContainer : BaseChangePasswordContainer
1149 public ChangePasswordContainer (ChangePassword owner) : base (owner)
1151 ID = "ChangePasswordContainerID";
1154 // Requried controls
1155 public IEditableTextControl UserNameTextBox
1159 Control c = FindControl ("UserName");
1161 throw new HttpException ("ChangePasswordTemplate does not contain an IEditableTextControl with ID UserName for the username, this is required if DisplayUserName=true.");
1162 return c as IEditableTextControl;
1165 public IEditableTextControl CurrentPasswordTextBox
1169 Control c = FindControl ("CurrentPassword");
1171 throw new HttpException ("ChangePasswordTemplate does not contain an IEditableTextControl with ID CurrentPassword for the current password.");
1172 return c as IEditableTextControl;
1175 public IEditableTextControl NewPasswordTextBox
1179 Control c = FindControl ("NewPassword");
1181 throw new HttpException ("ChangePasswordTemplate does not contain an IEditableTextControl with ID NewPassword for the new password.");
1182 return c as IEditableTextControl;
1186 // Optional controls
1187 public IEditableTextControl ConfirmNewPasswordTextBox
1189 get { return FindControl ("ConfirmNewPassword") as IEditableTextControl; }
1191 public Control CancelButton
1193 get { return FindControl ("Cancel"); }
1195 public Control ChangePasswordButton
1197 get { return FindControl ("ChangePassword"); }
1199 public ITextControl FailureTextLiteral
1201 get { return FindControl ("FailureText") as ITextControl; }
1205 sealed class ChangePasswordDeafultTemplate : ITemplate
1207 readonly ChangePassword _owner = null;
1209 internal ChangePasswordDeafultTemplate (ChangePassword cPassword)
1214 TableRow CreateRow (Control c0, Control c1, Control c2, Style s0, Style s1)
1216 TableRow row = new TableRow ();
1217 TableCell cell0 = new TableCell ();
1218 TableCell cell1 = new TableCell ();
1220 cell0.Controls.Add (c0);
1221 row.Controls.Add (cell0);
1223 if ((c1 != null) && (c2 != null)) {
1224 cell1.Controls.Add (c1);
1225 cell1.Controls.Add (c2);
1226 cell0.HorizontalAlign = HorizontalAlign.Right;
1229 cell0.ApplyStyle (s0);
1231 cell1.ApplyStyle (s1);
1233 row.Controls.Add (cell1);
1236 cell0.ColumnSpan = 2;
1237 cell0.HorizontalAlign = HorizontalAlign.Center;
1239 cell0.ApplyStyle (s0);
1244 bool AddLink (string pageUrl, string linkText, string linkIcon, WebControl container)
1247 if (linkIcon.Length > 0) {
1248 Image img = new Image ();
1249 img.ImageUrl = linkIcon;
1250 container.Controls.Add (img);
1253 if (linkText.Length > 0) {
1254 HyperLink link = new HyperLink ();
1255 link.NavigateUrl = pageUrl;
1256 link.Text = linkText;
1257 link.ControlStyle.CopyTextStylesFrom (container.ControlStyle);
1258 container.Controls.Add (link);
1264 public void InstantiateIn (Control container)
1266 Table table = new Table ();
1267 table.CellPadding = 0;
1270 table.Controls.Add (
1271 CreateRow (new LiteralControl (_owner.ChangePasswordTitleText),
1272 null, null, _owner.TitleTextStyle, null));
1275 if (_owner.InstructionText.Length > 0) {
1276 table.Controls.Add (
1277 CreateRow (new LiteralControl (_owner.InstructionText),
1278 null, null, _owner.InstructionTextStyle, null));
1282 if (_owner.DisplayUserName) {
1283 TextBox UserName = new TextBox ();
1284 UserName.ID = "UserName";
1285 UserName.Text = _owner.UserName;
1286 UserName.ApplyStyle (_owner.TextBoxStyle);
1288 Label UserNameLabel = new Label ();
1289 UserNameLabel.ID = "UserNameLabel";
1290 UserNameLabel.AssociatedControlID = "UserName";
1291 UserNameLabel.Text = _owner.UserNameLabelText;
1293 RequiredFieldValidator UserNameRequired = new RequiredFieldValidator ();
1294 UserNameRequired.ID = "UserNameRequired";
1295 UserNameRequired.ControlToValidate = "UserName";
1296 UserNameRequired.ErrorMessage = _owner.UserNameRequiredErrorMessage;
1297 UserNameRequired.ToolTip = _owner.UserNameRequiredErrorMessage;
1298 UserNameRequired.Text = "*";
1299 UserNameRequired.ValidationGroup = _owner.ID;
1300 UserNameRequired.ApplyStyle (_owner.ValidatorTextStyle);
1302 table.Controls.Add (CreateRow (UserNameLabel, UserName, UserNameRequired, _owner.LabelStyle, null));
1306 TextBox CurrentPassword = new TextBox ();
1307 CurrentPassword.ID = "CurrentPassword";
1308 CurrentPassword.TextMode = TextBoxMode.Password;
1309 CurrentPassword.ApplyStyle (_owner.TextBoxStyle);
1311 Label CurrentPasswordLabel = new Label ();
1312 CurrentPasswordLabel.ID = "CurrentPasswordLabel";
1313 CurrentPasswordLabel.AssociatedControlID = "CurrentPasswordLabel";
1314 CurrentPasswordLabel.Text = _owner.PasswordLabelText;
1316 RequiredFieldValidator CurrentPasswordRequired = new RequiredFieldValidator ();
1317 CurrentPasswordRequired.ID = "CurrentPasswordRequired";
1318 CurrentPasswordRequired.ControlToValidate = "CurrentPassword";
1319 CurrentPasswordRequired.ErrorMessage = _owner.PasswordRequiredErrorMessage;
1320 CurrentPasswordRequired.ToolTip = _owner.PasswordRequiredErrorMessage;
1321 CurrentPasswordRequired.Text = "*";
1322 CurrentPasswordRequired.ValidationGroup = _owner.ID;
1323 CurrentPasswordRequired.ApplyStyle (_owner.ValidatorTextStyle);
1325 table.Controls.Add (CreateRow (CurrentPasswordLabel, CurrentPassword, CurrentPasswordRequired, _owner.LabelStyle, null));
1328 TextBox NewPassword = new TextBox ();
1329 NewPassword.ID = "NewPassword";
1330 NewPassword.TextMode = TextBoxMode.Password;
1331 NewPassword.ApplyStyle (_owner.TextBoxStyle);
1333 Label NewPasswordLabel = new Label ();
1334 NewPasswordLabel.ID = "NewPasswordLabel";
1335 NewPasswordLabel.AssociatedControlID = "NewPassword";
1336 NewPasswordLabel.Text = _owner.NewPasswordLabelText;
1338 RequiredFieldValidator NewPasswordRequired = new RequiredFieldValidator ();
1339 NewPasswordRequired.ID = "NewPasswordRequired";
1340 NewPasswordRequired.ControlToValidate = "NewPassword";
1341 NewPasswordRequired.ErrorMessage = _owner.PasswordRequiredErrorMessage;
1342 NewPasswordRequired.ToolTip = _owner.PasswordRequiredErrorMessage;
1343 NewPasswordRequired.Text = "*";
1344 NewPasswordRequired.ValidationGroup = _owner.ID;
1345 NewPasswordRequired.ApplyStyle (_owner.ValidatorTextStyle);
1347 table.Controls.Add (CreateRow (NewPasswordLabel, NewPassword, NewPasswordRequired, _owner.LabelStyle, null));
1350 if (_owner.PasswordHintText.Length > 0) {
1351 table.Controls.Add (
1352 CreateRow (new LiteralControl (""),
1353 new LiteralControl (_owner.PasswordHintText),
1354 new LiteralControl (""),
1355 null, _owner.PasswordHintStyle));
1359 TextBox ConfirmNewPassword = new TextBox ();
1360 ConfirmNewPassword.ID = "ConfirmNewPassword";
1361 ConfirmNewPassword.TextMode = TextBoxMode.Password;
1362 ConfirmNewPassword.ApplyStyle (_owner.TextBoxStyle);
1364 Label ConfirmNewPasswordLabel = new Label ();
1365 ConfirmNewPasswordLabel.ID = "ConfirmNewPasswordLabel";
1366 ConfirmNewPasswordLabel.AssociatedControlID = "ConfirmNewPasswordLabel";
1367 ConfirmNewPasswordLabel.Text = _owner.ConfirmNewPasswordLabelText;
1369 RequiredFieldValidator ConfirmNewPasswordRequired = new RequiredFieldValidator ();
1370 ConfirmNewPasswordRequired.ID = "ConfirmNewPasswordRequired";
1371 ConfirmNewPasswordRequired.ControlToValidate = "ConfirmNewPassword";
1372 ConfirmNewPasswordRequired.ErrorMessage = _owner.PasswordRequiredErrorMessage;
1373 ConfirmNewPasswordRequired.ToolTip = _owner.PasswordRequiredErrorMessage;
1374 ConfirmNewPasswordRequired.Text = "*";
1375 ConfirmNewPasswordRequired.ValidationGroup = _owner.ID;
1376 ConfirmNewPasswordRequired.ApplyStyle (_owner.ValidatorTextStyle);
1378 table.Controls.Add (CreateRow (ConfirmNewPasswordLabel, ConfirmNewPassword, ConfirmNewPasswordRequired, _owner.LabelStyle, null));
1381 CompareValidator NewPasswordCompare = new CompareValidator ();
1382 NewPasswordCompare.ID = "NewPasswordCompare";
1383 NewPasswordCompare.ControlToCompare = "NewPassword";
1384 NewPasswordCompare.ControlToValidate = "ConfirmNewPassword";
1385 NewPasswordCompare.Display = ValidatorDisplay.Dynamic;
1386 NewPasswordCompare.ErrorMessage = _owner.ConfirmPasswordCompareErrorMessage;
1387 NewPasswordCompare.ValidationGroup = _owner.ID;
1389 table.Controls.Add (CreateRow (NewPasswordCompare, null, null, null, null));
1392 Literal FailureTextLiteral = new Literal ();
1393 FailureTextLiteral.ID = "FailureText";
1394 FailureTextLiteral.EnableViewState = false;
1396 if (_owner.FailureTextStyle.ForeColor.IsEmpty)
1397 _owner.FailureTextStyle.ForeColor = System.Drawing.Color.Red;
1399 table.Controls.Add (CreateRow (FailureTextLiteral, null, null, _owner.FailureTextStyle, null));
1402 WebControl ChangePasswordButton = null;
1403 switch (_owner.ChangePasswordButtonType) {
1404 case ButtonType.Button:
1405 ChangePasswordButton = new Button ();
1407 case ButtonType.Image:
1408 ChangePasswordButton = new ImageButton ();
1410 case ButtonType.Link:
1411 ChangePasswordButton = new LinkButton ();
1415 ChangePasswordButton.ID = "ChangePasswordPushButton";
1416 ChangePasswordButton.ApplyStyle (_owner.ChangePasswordButtonStyle);
1417 ((IButtonControl) ChangePasswordButton).CommandName = ChangePassword.ChangePasswordButtonCommandName;
1418 ((IButtonControl) ChangePasswordButton).Text = _owner.ChangePasswordButtonText;
1419 ((IButtonControl) ChangePasswordButton).ValidationGroup = _owner.ID;
1421 WebControl CancelButton = null;
1422 switch (_owner.CancelButtonType) {
1423 case ButtonType.Button:
1424 CancelButton = new Button ();
1426 case ButtonType.Image:
1427 CancelButton = new ImageButton ();
1429 case ButtonType.Link:
1430 CancelButton = new LinkButton ();
1434 CancelButton.ID = "CancelPushButton";
1435 CancelButton.ApplyStyle (_owner.CancelButtonStyle);
1436 ((IButtonControl) CancelButton).CommandName = ChangePassword.CancelButtonCommandName;
1437 ((IButtonControl) CancelButton).Text = _owner.CancelButtonText;
1438 ((IButtonControl) CancelButton).CausesValidation = false;
1440 table.Controls.Add (CreateRow (ChangePasswordButton, CancelButton, new LiteralControl (""), null, null));
1443 TableRow linksRow = new TableRow ();
1444 TableCell linksCell = new TableCell ();
1445 linksCell.ColumnSpan = 2;
1446 linksCell.ControlStyle.CopyFrom (_owner.HyperLinkStyle);
1448 linksRow.Cells.Add (linksCell);
1450 if (AddLink (_owner.HelpPageUrl, _owner.HelpPageText, _owner.HelpPageIconUrl, linksCell))
1451 linksCell.Controls.Add (new LiteralControl ("<br/>"));
1453 if (AddLink (_owner.CreateUserUrl, _owner.CreateUserText, _owner.CreateUserIconUrl, linksCell))
1454 linksCell.Controls.Add (new LiteralControl ("<br/>"));
1456 if (AddLink (_owner.PasswordRecoveryUrl, _owner.PasswordRecoveryText, _owner.PasswordRecoveryIconUrl, linksCell))
1457 linksCell.Controls.Add (new LiteralControl ("<br/>"));
1459 AddLink (_owner.EditProfileUrl, _owner.EditProfileText, _owner.EditProfileIconUrl, linksCell);
1461 table.Controls.Add (linksRow);
1463 container.Controls.Add (table);
1467 sealed class SuccessDefaultTemplate : ITemplate
1469 readonly ChangePassword _cPassword = null;
1471 internal SuccessDefaultTemplate (ChangePassword cPassword)
1473 _cPassword = cPassword;
1476 TableRow CreateRow (Control c0, Style s0, HorizontalAlign align)
1478 TableRow row = new TableRow ();
1479 TableCell cell0 = new TableCell ();
1481 cell0.Controls.Add (c0);
1482 cell0.HorizontalAlign = align;
1484 cell0.ApplyStyle (s0);
1486 row.Controls.Add (cell0);
1490 public void InstantiateIn (Control container)
1492 Table table = new Table ();
1493 table.ControlStyle.Width = Unit.Percentage (100);
1494 table.ControlStyle.Height = Unit.Percentage (100);
1497 table.Controls.Add (
1498 CreateRow (new LiteralControl (_cPassword.SuccessTitleText), _cPassword.TitleTextStyle, HorizontalAlign.Center));
1501 table.Controls.Add (
1502 CreateRow (new LiteralControl (_cPassword.SuccessText), _cPassword.SuccessTextStyle, HorizontalAlign.Center));
1505 WebControl ContinueButton = null;
1506 switch (_cPassword.ChangePasswordButtonType) {
1507 case ButtonType.Button:
1508 ContinueButton = new Button ();
1510 case ButtonType.Image:
1511 ContinueButton = new ImageButton ();
1513 case ButtonType.Link:
1514 ContinueButton = new LinkButton ();
1518 ContinueButton.ID = "ContinueButton";
1519 ContinueButton.ApplyStyle (_cPassword.ContinueButtonStyle);
1520 ((IButtonControl) ContinueButton).CommandName = ChangePassword.ContinueButtonCommandName;
1521 ((IButtonControl) ContinueButton).Text = _cPassword.ContinueButtonText;
1522 ((IButtonControl) ContinueButton).CausesValidation = false;
1524 table.Controls.Add (
1525 CreateRow (ContinueButton, null, HorizontalAlign.Right));
1527 container.Controls.Add (table);
1531 sealed class SuccessContainer : BaseChangePasswordContainer
1533 public SuccessContainer (ChangePassword owner) : base (owner)
1535 ID = "SuccessContainerID";
1537 public Control ChangePasswordButton
1539 get { return FindControl ("Continue"); }