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 static readonly object cancelButtonClickEvent = new object ();
46 static readonly object changedPasswordEvent = new object ();
47 static readonly object changePasswordErrorEvent = new object ();
48 static readonly object changingPasswordEvent = new object ();
49 static readonly object continueButtonClickEvent = new object ();
50 static readonly object sendingMailEvent = new object ();
51 static readonly object sendMailErrorEvent = new object ();
53 public static readonly string CancelButtonCommandName = "Cancel";
54 public static readonly string ChangePasswordButtonCommandName = "ChangePassword";
55 public static readonly string ContinueButtonCommandName = "Continue";
57 Style _cancelButtonStyle = null;
58 Style _changePasswordButtonStyle = null;
59 Style _continueButtonStyle = null;
60 TableItemStyle _failureTextStyle = null;
61 TableItemStyle _hyperLinkStyle = null;
62 TableItemStyle _instructionTextStyle = null;
63 TableItemStyle _labelStyle = null;
64 TableItemStyle _passwordHintStyle = null;
65 TableItemStyle _successTextStyle = null;
66 Style _textBoxStyle = null;
67 TableItemStyle _titleTextStyle = null;
68 Style _validatorTextStyle = null;
70 MailDefinition _mailDefinition = null;
71 MembershipProvider _provider = null;
73 ITemplate _changePasswordTemplate = null;
74 ITemplate _successTemplate = null;
76 Control _changePasswordTemplateContainer = null;
77 Control _successTemplateContainer = null;
79 string _username = null;
80 string _currentPassword = null;
81 string _newPassword = null;
82 string _newPasswordConfirm = null;
84 bool _showContinue = false;
86 EventHandlerList events = new EventHandlerList ();
89 public event EventHandler CancelButtonClick {
90 add { events.AddHandler (cancelButtonClickEvent, value); }
91 remove { events.RemoveHandler (cancelButtonClickEvent, value); }
94 public event EventHandler ChangedPassword {
95 add { events.AddHandler (changedPasswordEvent, value); }
96 remove { events.RemoveHandler (changedPasswordEvent, value); }
99 public event EventHandler ChangePasswordError {
100 add { events.AddHandler (changePasswordErrorEvent, value); }
101 remove { events.RemoveHandler (changePasswordErrorEvent, value); }
104 public event LoginCancelEventHandler ChangingPassword {
105 add { events.AddHandler (changingPasswordEvent, value); }
106 remove { events.RemoveHandler (changingPasswordEvent, value); }
109 public event EventHandler ContinueButtonClick {
110 add { events.AddHandler (continueButtonClickEvent, value); }
111 remove { events.RemoveHandler (continueButtonClickEvent, value); }
114 public event MailMessageEventHandler SendingMail {
115 add { events.AddHandler (sendingMailEvent, value); }
116 remove { events.RemoveHandler (sendingMailEvent, value); }
119 public event SendMailErrorEventHandler SendMailError {
120 add { events.AddHandler (sendMailErrorEvent, value); }
121 remove { events.RemoveHandler (sendMailErrorEvent, value); }
125 #region Public Properties
128 public virtual int BorderPadding
130 get { return ViewState.GetInt ("BorderPadding", 1); }
134 throw new ArgumentOutOfRangeException ();
135 ViewState ["BorderPadding"] = value;
140 public virtual string CancelButtonImageUrl
142 get { return ViewState.GetString ("CancelButtonImageUrl", String.Empty); }
143 set { ViewState ["CancelButtonImageUrl"] = value; }
146 [PersistenceMode (PersistenceMode.InnerProperty)]
147 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
148 [NotifyParentProperty (true)]
149 public Style CancelButtonStyle
153 if (_cancelButtonStyle == null) {
154 _cancelButtonStyle = new Style ();
155 if (IsTrackingViewState)
156 _cancelButtonStyle.TrackViewState ();
158 return _cancelButtonStyle;
163 public virtual string CancelButtonText
165 get { return ViewState.GetString ("CancelButtonText", "Cancel"); }
166 set { ViewState ["CancelButtonText"] = value; }
169 public virtual ButtonType CancelButtonType
171 get { return ViewState ["CancelButtonType"] == null ? ButtonType.Button : (ButtonType) ViewState ["CancelButtonType"]; }
172 set { ViewState ["CancelButtonType"] = value; }
176 public virtual string CancelDestinationPageUrl
178 get { return ViewState.GetString ("CancelDestinationPageUrl", String.Empty); }
179 set { ViewState ["CancelDestinationPageUrl"] = value; }
182 public virtual string ChangePasswordButtonImageUrl
184 get { return ViewState.GetString ("ChangePasswordButtonImageUrl", String.Empty); }
185 set { ViewState ["ChangePasswordButtonImageUrl"] = value; }
189 [PersistenceMode (PersistenceMode.InnerProperty)]
190 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
191 [NotifyParentProperty (true)]
192 public Style ChangePasswordButtonStyle
196 if (_changePasswordButtonStyle == null) {
197 _changePasswordButtonStyle = new Style ();
198 if (IsTrackingViewState)
199 _changePasswordButtonStyle.TrackViewState ();
201 return _changePasswordButtonStyle;
206 public virtual string ChangePasswordButtonText
208 get { return ViewState.GetString ("ChangePasswordButtonText", "Change Password"); }
209 set { ViewState ["ChangePasswordButtonText"] = value; }
212 public virtual ButtonType ChangePasswordButtonType
214 get { return ViewState ["ChangePasswordButtonType"] == null ? ButtonType.Button : (ButtonType) ViewState ["ChangePasswordButtonType"]; }
215 set { ViewState ["ChangePasswordButtonType"] = value; }
219 public virtual string ChangePasswordFailureText
221 get { return ViewState.GetString ("ChangePasswordFailureText", "Your attempt to change passwords was unsuccessful. Please try again."); }
222 set { ViewState ["ChangePasswordFailureText"] = value; }
226 [PersistenceMode (PersistenceMode.InnerProperty)]
227 [TemplateContainer (typeof (ChangePassword))]
228 public virtual ITemplate ChangePasswordTemplate
230 get { return _changePasswordTemplate; }
231 set { _changePasswordTemplate = value; }
235 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
236 public Control ChangePasswordTemplateContainer
240 if (_changePasswordTemplateContainer == null)
241 _changePasswordTemplateContainer = new ChangePasswordContainer (this);
242 return _changePasswordTemplateContainer;
247 public virtual string ChangePasswordTitleText
249 get { return ViewState.GetString ("ChangePasswordTitleText", "Change Your Password"); }
250 set { ViewState ["ChangePasswordTitleText"] = value; }
253 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
257 public virtual string ConfirmNewPassword
259 get { return _newPasswordConfirm != null ? _newPasswordConfirm : ""; }
263 public virtual string ConfirmNewPasswordLabelText
265 get { return ViewState.GetString ("ConfirmNewPasswordLabelText", "Confirm New Password:"); }
266 set { ViewState ["ConfirmNewPasswordLabelText"] = value; }
270 public virtual string ConfirmPasswordCompareErrorMessage
272 get { return ViewState.GetString ("ConfirmPasswordCompareErrorMessage", "The Confirm New Password must match the New Password entry."); }
273 set { ViewState ["ConfirmPasswordCompareErrorMessage"] = value; }
277 public virtual string ConfirmPasswordRequiredErrorMessage
279 get { return ViewState.GetString ("ConfirmPasswordRequiredErrorMessage", String.Empty); }
280 set { ViewState ["ConfirmPasswordRequiredErrorMessage"] = value; }
283 public virtual string ContinueButtonImageUrl
285 get { return ViewState.GetString ("ContinueButtonImageUrl", String.Empty); }
286 set { ViewState ["ContinueButtonImageUrl"] = value; }
289 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
290 [NotifyParentProperty (true)]
291 [PersistenceMode (PersistenceMode.InnerProperty)]
292 public Style ContinueButtonStyle
296 if (_continueButtonStyle == null) {
297 _continueButtonStyle = new Style ();
298 if (IsTrackingViewState)
299 _continueButtonStyle.TrackViewState ();
301 return _continueButtonStyle;
306 public virtual string ContinueButtonText
308 get { return ViewState.GetString ("ContinueButtonText", "Continue"); }
309 set { ViewState ["ContinueButtonText"] = value; }
312 public virtual ButtonType ContinueButtonType
314 get { return ViewState ["ContinueButtonType"] == null ? ButtonType.Button : (ButtonType) ViewState ["ContinueButtonType"]; }
315 set { ViewState ["ContinueButtonType"] = value; }
319 public virtual string ContinueDestinationPageUrl
321 get { return ViewState.GetString ("ContinueDestinationPageUrl", String.Empty); }
322 set { ViewState ["ContinueDestinationPageUrl"] = value; }
325 public virtual string CreateUserIconUrl
327 get { return ViewState.GetString ("CreateUserIconUrl", String.Empty); }
328 set { ViewState ["CreateUserIconUrl"] = value; }
332 public virtual string CreateUserText
334 get { return ViewState.GetString ("CreateUserText", String.Empty); }
335 set { ViewState ["CreateUserText"] = value; }
338 public virtual string CreateUserUrl
340 get { return ViewState.GetString ("CreateUserUrl", String.Empty); }
341 set { ViewState ["CreateUserUrl"] = value; }
344 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
347 public virtual string CurrentPassword
349 get { return _currentPassword != null ? _currentPassword : ""; }
352 [DefaultValue (false)]
353 public virtual bool DisplayUserName
355 get { return ViewState.GetBool ("DisplayUserName", false); }
356 set { ViewState ["DisplayUserName"] = value; }
359 public virtual string EditProfileIconUrl
361 get { return ViewState.GetString ("EditProfileIconUrl", String.Empty); }
362 set { ViewState ["EditProfileIconUrl"] = value; }
366 public virtual string EditProfileText
368 get { return ViewState.GetString ("EditProfileText", String.Empty); }
369 set { ViewState ["EditProfileText"] = value; }
372 public virtual string EditProfileUrl
374 get { return ViewState.GetString ("EditProfileUrl", String.Empty); }
375 set { ViewState ["EditProfileUrl"] = value; }
378 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
379 [NotifyParentProperty (true)]
380 [PersistenceMode (PersistenceMode.InnerProperty)]
381 public TableItemStyle FailureTextStyle
385 if (_failureTextStyle == null) {
386 _failureTextStyle = new TableItemStyle ();
387 if (IsTrackingViewState)
388 _failureTextStyle.TrackViewState ();
390 return _failureTextStyle;
394 public virtual string HelpPageIconUrl
396 get { return ViewState.GetString ("HelpPageIconUrl", String.Empty); }
397 set { ViewState ["HelpPageIconUrl"] = value; }
401 public virtual string HelpPageText
403 get { return ViewState.GetString ("HelpPageText", String.Empty); }
404 set { ViewState ["HelpPageText"] = value; }
407 public virtual string HelpPageUrl
409 get { return ViewState.GetString ("HelpPageUrl", String.Empty); }
410 set { ViewState ["HelpPageUrl"] = value; }
413 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
414 [NotifyParentProperty (true)]
415 [PersistenceMode (PersistenceMode.InnerProperty)]
416 public TableItemStyle HyperLinkStyle
420 if (_hyperLinkStyle == null) {
421 _hyperLinkStyle = new TableItemStyle ();
422 if (IsTrackingViewState)
423 _hyperLinkStyle.TrackViewState ();
425 return _hyperLinkStyle;
430 public virtual string InstructionText
432 get { return ViewState.GetString ("InstructionText", String.Empty); }
433 set { ViewState ["InstructionText"] = value; }
436 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
437 [NotifyParentProperty (true)]
438 [PersistenceMode (PersistenceMode.InnerProperty)]
439 public TableItemStyle InstructionTextStyle
443 if (_instructionTextStyle == null) {
444 _instructionTextStyle = new TableItemStyle ();
445 if (IsTrackingViewState)
446 _instructionTextStyle.TrackViewState ();
448 return _instructionTextStyle;
452 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
453 [NotifyParentProperty (true)]
454 [PersistenceMode (PersistenceMode.InnerProperty)]
455 public TableItemStyle LabelStyle
459 if (_labelStyle == null) {
460 _labelStyle = new TableItemStyle ();
461 if (IsTrackingViewState)
462 _labelStyle.TrackViewState ();
469 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
470 [NotifyParentProperty (true)]
471 [PersistenceMode (PersistenceMode.InnerProperty)]
472 public MailDefinition MailDefinition
476 if (_mailDefinition == null) {
477 _mailDefinition = new MailDefinition ();
478 if (IsTrackingViewState)
479 ((IStateManager) _mailDefinition).TrackViewState ();
481 return _mailDefinition;
487 public virtual string MembershipProvider
491 object o = ViewState ["MembershipProvider"];
492 return (o == null) ? "" : (string) o;
497 ViewState.Remove ("MembershipProvider");
499 ViewState ["MembershipProvider"] = value;
505 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
507 public virtual string NewPassword
509 get { return _newPassword != null ? _newPassword : ""; }
513 public virtual string NewPasswordLabelText
515 get { return ViewState.GetString ("NewPasswordLabelText", "New Password:"); }
516 set { ViewState ["NewPasswordLabelText"] = value; }
519 public virtual string NewPasswordRegularExpression
521 get { return ViewState.GetString ("NewPasswordRegularExpression", String.Empty); }
522 set { ViewState ["NewPasswordRegularExpression"] = value; }
525 public virtual string NewPasswordRegularExpressionErrorMessage
527 get { return ViewState.GetString ("NewPasswordRegularExpressionErrorMessage", String.Empty); }
528 set { ViewState ["NewPasswordRegularExpressionErrorMessage"] = value; }
532 public virtual string NewPasswordRequiredErrorMessage
534 get { return ViewState.GetString ("NewPasswordRequiredErrorMessage", "New Password is required."); }
535 set { ViewState ["NewPasswordRequiredErrorMessage"] = value; }
538 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
539 [NotifyParentProperty (true)]
540 [PersistenceMode (PersistenceMode.InnerProperty)]
541 public TableItemStyle PasswordHintStyle
545 if (_passwordHintStyle == null) {
546 _passwordHintStyle = new TableItemStyle ();
547 if (IsTrackingViewState)
548 _passwordHintStyle.TrackViewState ();
550 return _passwordHintStyle;
555 public virtual string PasswordHintText
557 get { return ViewState.GetString ("PasswordHintText", String.Empty); }
558 set { ViewState ["PasswordHintText"] = value; }
562 public virtual string PasswordLabelText
564 get { return ViewState.GetString ("PasswordLabelText", "Password:"); }
565 set { ViewState ["PasswordLabelText"] = value; }
568 public virtual string PasswordRecoveryIconUrl
570 get { return ViewState.GetString ("PasswordRecoveryIconUrl", String.Empty); }
571 set { ViewState ["PasswordRecoveryIconUrl"] = value; }
575 public virtual string PasswordRecoveryText
577 get { return ViewState.GetString ("PasswordRecoveryText", String.Empty); }
578 set { ViewState ["PasswordRecoveryText"] = value; }
581 public virtual string PasswordRecoveryUrl
583 get { return ViewState.GetString ("PasswordRecoveryUrl", String.Empty); }
584 set { ViewState ["PasswordRecoveryUrl"] = value; }
588 public virtual string PasswordRequiredErrorMessage
590 get { return ViewState.GetString ("PasswordRequiredErrorMessage", String.Empty); }
591 set { ViewState ["PasswordRequiredErrorMessage"] = value; }
595 public virtual string SuccessPageUrl
597 get { return ViewState.GetString ("SuccessPageUrl", String.Empty); }
598 set { ViewState ["SuccessPageUrl"] = value; }
601 [PersistenceMode (PersistenceMode.InnerProperty)]
602 [TemplateContainer (typeof (ChangePassword))]
604 public virtual ITemplate SuccessTemplate
606 get { return _successTemplate; }
607 set { _successTemplate = value; }
611 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
612 public Control SuccessTemplateContainer
616 if (_successTemplateContainer == null)
617 _successTemplateContainer = new SuccessContainer (this);
618 return _successTemplateContainer;
624 public virtual string SuccessText
626 get { return ViewState.GetString ("SuccessText", "Your password has been changed!"); }
627 set { ViewState ["SuccessText"] = value; }
630 [NotifyParentProperty (true)]
631 [PersistenceMode (PersistenceMode.InnerProperty)]
632 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
633 public TableItemStyle SuccessTextStyle
637 if (_successTextStyle == null) {
638 _successTextStyle = new TableItemStyle ();
639 if (IsTrackingViewState)
640 _successTextStyle.TrackViewState ();
642 return _successTextStyle;
647 public virtual string SuccessTitleText
649 get { return ViewState.GetString ("SuccessTitleText", "Change Password Complete"); }
650 set { ViewState ["SuccessTitleText"] = value; }
653 [NotifyParentProperty (true)]
654 [PersistenceMode (PersistenceMode.InnerProperty)]
655 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
656 public Style TextBoxStyle
660 if (_textBoxStyle == null) {
661 _textBoxStyle = new Style ();
662 if (IsTrackingViewState)
663 _textBoxStyle.TrackViewState ();
665 return _textBoxStyle;
669 [NotifyParentProperty (true)]
670 [PersistenceMode (PersistenceMode.InnerProperty)]
671 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
672 public TableItemStyle TitleTextStyle
676 if (_titleTextStyle == null) {
677 _titleTextStyle = new TableItemStyle ();
678 if (IsTrackingViewState)
679 _titleTextStyle.TrackViewState ();
681 return _titleTextStyle;
686 public virtual string UserName
690 if (_username == null && HttpContext.Current.Request.IsAuthenticated)
691 _username = HttpContext.Current.User.Identity.Name;
693 return _username != null ? _username : "";
695 set { _username = value; }
699 public virtual string UserNameLabelText
701 get { return ViewState.GetString ("UserNameLabelText", "User Name:"); }
702 set { ViewState ["UserNameLabelText"] = value; }
706 public virtual string UserNameRequiredErrorMessage
708 get { return ViewState.GetString ("UserNameRequiredErrorMessage", "User Name is required."); }
709 set { ViewState ["UserNameRequiredErrorMessage"] = value; }
712 [NotifyParentProperty (true)]
713 [PersistenceMode (PersistenceMode.InnerProperty)]
714 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
715 public Style ValidatorTextStyle
719 if (_validatorTextStyle == null) {
720 _validatorTextStyle = new Style ();
721 if (IsTrackingViewState)
722 _validatorTextStyle.TrackViewState ();
724 return _validatorTextStyle;
730 #region Protected Methods
732 protected internal override void CreateChildControls ()
736 ITemplate cpTemplate = ChangePasswordTemplate;
737 if (cpTemplate == null)
738 cpTemplate = new ChangePasswordDeafultTemplate (this);
739 ((ChangePasswordContainer) ChangePasswordTemplateContainer).InstantiateTemplate (cpTemplate);
741 ITemplate sTemplate = SuccessTemplate;
742 if (sTemplate == null)
743 sTemplate = new SuccessDefaultTemplate (this);
744 ((SuccessContainer) SuccessTemplateContainer).InstantiateTemplate (sTemplate);
746 Controls.AddAt (0, ChangePasswordTemplateContainer);
747 Controls.AddAt (1, SuccessTemplateContainer);
749 IEditableTextControl editable;
751 ChangePasswordContainer container = (ChangePasswordContainer) ChangePasswordTemplateContainer;
752 if (DisplayUserName) {
753 editable = container.UserNameTextBox;
754 if (editable != null)
755 editable.TextChanged += new EventHandler (UserName_TextChanged);
758 editable = container.CurrentPasswordTextBox;
759 if (editable != null)
760 editable.TextChanged += new EventHandler (CurrentPassword_TextChanged);
762 editable = container.NewPasswordTextBox;
763 if (editable != null)
764 editable.TextChanged += new EventHandler (NewPassword_TextChanged);
766 editable = container.ConfirmNewPasswordTextBox;
767 if (editable != null)
768 editable.TextChanged += new EventHandler (NewPasswordConfirm_TextChanged);
771 protected internal override void Render (HtmlTextWriter writer)
773 for (int i = 0; i < Controls.Count; i++)
774 if (Controls [i].Visible)
775 Controls [i].Render (writer);
780 #region Private Methods
782 [MonoTODO ("Not implemented")]
783 protected override void SetDesignModeState (IDictionary data)
785 throw new NotImplementedException ();
788 void InitMemberShipProvider ()
790 string mp = MembershipProvider;
791 _provider = (mp.Length == 0) ? Membership.Provider : Membership.Providers [mp];
792 if (_provider == null)
793 throw new HttpException (Locale.GetText ("No provider named '{0}' could be found.", mp));
796 void ProcessChangePasswordEvent (CommandEventArgs args)
801 LoginCancelEventArgs loginCancelEventArgs = new LoginCancelEventArgs ();
802 OnChangingPassword (loginCancelEventArgs);
803 if (loginCancelEventArgs.Cancel)
808 res = MembershipProviderInternal.ChangePassword (UserName, CurrentPassword, NewPassword);
814 OnChangedPassword (args);
815 _showContinue = true;
817 if (_mailDefinition != null)
818 SendMail (UserName, NewPassword);
821 OnChangePasswordError (EventArgs.Empty);
822 string lastError = string.Format (
823 "Password incorrect or New Password invalid. New Password length minimum: {0}. Non-alphanumeric characters required: {1}.",
824 MembershipProviderInternal.MinRequiredPasswordLength,
825 MembershipProviderInternal.MinRequiredNonAlphanumericCharacters);
827 ChangePasswordContainer container = (ChangePasswordContainer) ChangePasswordTemplateContainer;
828 container.FailureTextLiteral.Text = lastError;
829 _showContinue = false;
835 void ProcessCancelEvent (CommandEventArgs args)
837 OnCancelButtonClick (args);
839 if (ContinueDestinationPageUrl.Length > 0)
840 Context.Response.Redirect (ContinueDestinationPageUrl);
845 void ProcessContinueEvent (CommandEventArgs args)
847 OnContinueButtonClick (args);
849 if (ContinueDestinationPageUrl.Length > 0)
850 Context.Response.Redirect (ContinueDestinationPageUrl);
855 void SendMail (string username, string password)
857 MembershipUser user = MembershipProviderInternal.GetUser (UserName, false);
861 ListDictionary dictionary = new ListDictionary ();
862 dictionary.Add ("<%USERNAME%>", username);
863 dictionary.Add ("<%PASSWORD%>", password);
865 MailMessage message = MailDefinition.CreateMailMessage (user.Email, dictionary, this);
867 MailMessageEventArgs args = new MailMessageEventArgs (message);
868 OnSendingMail (args);
870 SmtpClient smtpClient = new SmtpClient ();
872 smtpClient.Send (message);
874 catch (Exception e) {
875 SendMailErrorEventArgs mailArgs = new SendMailErrorEventArgs (e);
876 OnSendMailError (mailArgs);
877 if (!mailArgs.Handled)
886 protected override HtmlTextWriterTag TagKey
888 get { return HtmlTextWriterTag.Table; }
891 internal virtual MembershipProvider MembershipProviderInternal
895 if (_provider == null)
896 InitMemberShipProvider ();
904 #region View and Control State
906 protected internal override void LoadControlState (object savedState)
908 if (savedState == null) return;
909 object [] state = (object []) savedState;
910 base.LoadControlState (state [0]);
912 _showContinue = (bool) state [1];
913 _username = (string) state [2];
916 protected internal override object SaveControlState ()
918 object state = base.SaveControlState ();
919 return new object [] { state, _showContinue, _username };
922 protected override void LoadViewState (object savedState)
925 if (savedState == null)
928 object [] states = (object []) savedState;
929 base.LoadViewState (states [0]);
931 if (states [1] != null)
932 CancelButtonStyle.LoadViewState (states [1]);
933 if (states [2] != null)
934 ChangePasswordButtonStyle.LoadViewState (states [2]);
935 if (states [3] != null)
936 ContinueButtonStyle.LoadViewState (states [3]);
938 if (states [4] != null)
939 FailureTextStyle.LoadViewState (states [4]);
940 if (states [5] != null)
941 HyperLinkStyle.LoadViewState (states [5]);
942 if (states [6] != null)
943 InstructionTextStyle.LoadViewState (states [6]);
945 if (states [7] != null)
946 LabelStyle.LoadViewState (states [7]);
947 if (states [8] != null)
948 PasswordHintStyle.LoadViewState (states [8]);
949 if (states [9] != null)
950 SuccessTextStyle.LoadViewState (states [9]);
952 if (states [10] != null)
953 TextBoxStyle.LoadViewState (states [10]);
954 if (states [11] != null)
955 TitleTextStyle.LoadViewState (states [11]);
956 if (states [12] != null)
957 ValidatorTextStyle.LoadViewState (states [12]);
959 if (states [13] != null)
960 ((IStateManager) MailDefinition).LoadViewState (states [13]);
963 protected override object SaveViewState ()
965 object [] states = new object [14];
966 states [0] = base.SaveViewState ();
968 if (_cancelButtonStyle != null)
969 states [1] = _cancelButtonStyle.SaveViewState ();
970 if (_changePasswordButtonStyle != null)
971 states [2] = _changePasswordButtonStyle.SaveViewState ();
972 if (_continueButtonStyle != null)
973 states [3] = _continueButtonStyle.SaveViewState ();
975 if (_failureTextStyle != null)
976 states [4] = _failureTextStyle.SaveViewState ();
977 if (_hyperLinkStyle != null)
978 states [5] = _hyperLinkStyle.SaveViewState ();
979 if (_instructionTextStyle != null)
980 states [6] = _instructionTextStyle.SaveViewState ();
982 if (_labelStyle != null)
983 states [7] = _labelStyle.SaveViewState ();
984 if (_passwordHintStyle != null)
985 states [8] = _passwordHintStyle.SaveViewState ();
986 if (_successTextStyle != null)
987 states [9] = _successTextStyle.SaveViewState ();
989 if (_textBoxStyle != null)
990 states [10] = _textBoxStyle.SaveViewState ();
991 if (_titleTextStyle != null)
992 states [11] = _titleTextStyle.SaveViewState ();
993 if (_validatorTextStyle != null)
994 states [12] = _validatorTextStyle.SaveViewState ();
996 if (_mailDefinition != null)
997 states [13] = ((IStateManager) _mailDefinition).SaveViewState ();
999 for (int i = 0; i < states.Length; i++) {
1000 if (states [i] != null)
1006 protected override void TrackViewState ()
1009 base.TrackViewState ();
1011 if (_cancelButtonStyle != null)
1012 _cancelButtonStyle.TrackViewState ();
1013 if (_changePasswordButtonStyle != null)
1014 _changePasswordButtonStyle.TrackViewState ();
1015 if (_continueButtonStyle != null)
1016 _continueButtonStyle.TrackViewState ();
1018 if (_failureTextStyle != null)
1019 _failureTextStyle.TrackViewState ();
1020 if (_hyperLinkStyle != null)
1021 _hyperLinkStyle.TrackViewState ();
1022 if (_instructionTextStyle != null)
1023 _instructionTextStyle.TrackViewState ();
1025 if (_labelStyle != null)
1026 _labelStyle.TrackViewState ();
1027 if (_passwordHintStyle != null)
1028 _passwordHintStyle.TrackViewState ();
1029 if (_successTextStyle != null)
1030 _successTextStyle.TrackViewState ();
1032 if (_textBoxStyle != null)
1033 _textBoxStyle.TrackViewState ();
1034 if (_titleTextStyle != null)
1035 _titleTextStyle.TrackViewState ();
1036 if (_validatorTextStyle != null)
1037 _validatorTextStyle.TrackViewState ();
1039 if (_mailDefinition != null)
1040 ((IStateManager) _mailDefinition).TrackViewState ();
1045 #region Event Handlers
1047 protected override bool OnBubbleEvent (object source, EventArgs e)
1049 CommandEventArgs args = e as CommandEventArgs;
1051 if (args.CommandName == ChangePasswordButtonCommandName) {
1052 ProcessChangePasswordEvent (args);
1055 if (args.CommandName == CancelButtonCommandName) {
1056 ProcessCancelEvent (args);
1059 if (args.CommandName == ContinueButtonCommandName) {
1060 ProcessContinueEvent (args);
1064 return base.OnBubbleEvent (source, e);
1067 protected virtual void OnCancelButtonClick (EventArgs e)
1069 EventHandler eh = events [cancelButtonClickEvent] as EventHandler;
1074 protected virtual void OnChangedPassword (EventArgs e)
1076 EventHandler eh = events [changedPasswordEvent] as EventHandler;
1081 protected virtual void OnChangePasswordError (EventArgs e)
1083 EventHandler eh = events [changePasswordErrorEvent] as EventHandler;
1088 protected virtual void OnChangingPassword (LoginCancelEventArgs e)
1090 LoginCancelEventHandler eh = events [changingPasswordEvent] as LoginCancelEventHandler;
1095 protected virtual void OnContinueButtonClick (EventArgs e)
1097 EventHandler eh = events [continueButtonClickEvent] as EventHandler;
1102 protected internal override void OnInit (EventArgs e)
1104 Page.RegisterRequiresControlState (this);
1108 protected internal override void OnPreRender (EventArgs e)
1110 ChangePasswordTemplateContainer.Visible = !_showContinue;
1111 SuccessTemplateContainer.Visible = _showContinue;
1112 base.OnPreRender (e);
1115 protected virtual void OnSendingMail (MailMessageEventArgs e)
1117 MailMessageEventHandler eh = events [sendingMailEvent] as MailMessageEventHandler;
1122 protected virtual void OnSendMailError (SendMailErrorEventArgs e)
1124 SendMailErrorEventHandler eh = events [sendMailErrorEvent] as SendMailErrorEventHandler;
1129 void UserName_TextChanged (object sender, EventArgs e)
1131 UserName = ((ITextControl) sender).Text;
1134 void CurrentPassword_TextChanged (object sender, EventArgs e)
1136 _currentPassword = ((ITextControl) sender).Text;
1139 void NewPassword_TextChanged (object sender, EventArgs e)
1141 _newPassword = ((ITextControl) sender).Text;
1144 void NewPasswordConfirm_TextChanged (object sender, EventArgs e)
1146 _newPasswordConfirm = ((ITextControl) sender).Text;
1151 class BaseChangePasswordContainer : Table, INamingContainer, INonBindingContainer
1153 protected readonly ChangePassword _owner = null;
1154 TableCell _containerCell = null;
1156 public BaseChangePasswordContainer (ChangePassword owner)
1162 public void InstantiateTemplate (ITemplate template)
1164 template.InstantiateIn (_containerCell);
1169 Attributes.Add ("ID", _owner.ID);
1172 CellPadding = _owner.BorderPadding;
1174 _containerCell = new TableCell ();
1176 TableRow row = new TableRow ();
1177 row.Cells.Add (_containerCell);
1181 protected internal override void OnPreRender (EventArgs e)
1183 ApplyStyle (_owner.ControlStyle);
1184 base.OnPreRender (e);
1188 sealed class ChangePasswordContainer : BaseChangePasswordContainer
1190 public ChangePasswordContainer (ChangePassword owner) : base (owner)
1192 ID = "ChangePasswordContainerID";
1195 // Requried controls
1196 public IEditableTextControl UserNameTextBox
1200 Control c = FindControl ("UserName");
1202 throw new HttpException ("ChangePasswordTemplate does not contain an IEditableTextControl with ID UserName for the username, this is required if DisplayUserName=true.");
1203 return c as IEditableTextControl;
1206 public IEditableTextControl CurrentPasswordTextBox
1210 Control c = FindControl ("CurrentPassword");
1212 throw new HttpException ("ChangePasswordTemplate does not contain an IEditableTextControl with ID CurrentPassword for the current password.");
1213 return c as IEditableTextControl;
1216 public IEditableTextControl NewPasswordTextBox
1220 Control c = FindControl ("NewPassword");
1222 throw new HttpException ("ChangePasswordTemplate does not contain an IEditableTextControl with ID NewPassword for the new password.");
1223 return c as IEditableTextControl;
1227 // Optional controls
1228 public IEditableTextControl ConfirmNewPasswordTextBox
1230 get { return FindControl ("ConfirmNewPassword") as IEditableTextControl; }
1232 public Control CancelButton
1234 get { return FindControl ("Cancel"); }
1236 public Control ChangePasswordButton
1238 get { return FindControl ("ChangePassword"); }
1240 public ITextControl FailureTextLiteral
1242 get { return FindControl ("FailureText") as ITextControl; }
1246 sealed class ChangePasswordDeafultTemplate : ITemplate
1248 readonly ChangePassword _owner = null;
1250 internal ChangePasswordDeafultTemplate (ChangePassword cPassword)
1255 TableRow CreateRow (Control c0, Control c1, Control c2, Style s0, Style s1)
1257 TableRow row = new TableRow ();
1258 TableCell cell0 = new TableCell ();
1259 TableCell cell1 = new TableCell ();
1261 cell0.Controls.Add (c0);
1262 row.Controls.Add (cell0);
1264 if ((c1 != null) && (c2 != null)) {
1265 cell1.Controls.Add (c1);
1266 cell1.Controls.Add (c2);
1267 cell0.HorizontalAlign = HorizontalAlign.Right;
1270 cell0.ApplyStyle (s0);
1272 cell1.ApplyStyle (s1);
1274 row.Controls.Add (cell1);
1277 cell0.ColumnSpan = 2;
1278 cell0.HorizontalAlign = HorizontalAlign.Center;
1280 cell0.ApplyStyle (s0);
1285 bool AddLink (string pageUrl, string linkText, string linkIcon, WebControl container)
1288 if (linkIcon.Length > 0) {
1289 Image img = new Image ();
1290 img.ImageUrl = linkIcon;
1291 container.Controls.Add (img);
1294 if (linkText.Length > 0) {
1295 HyperLink link = new HyperLink ();
1296 link.NavigateUrl = pageUrl;
1297 link.Text = linkText;
1298 link.ControlStyle.CopyTextStylesFrom (container.ControlStyle);
1299 container.Controls.Add (link);
1305 public void InstantiateIn (Control container)
1307 Table table = new Table ();
1308 table.CellPadding = 0;
1311 table.Controls.Add (
1312 CreateRow (new LiteralControl (_owner.ChangePasswordTitleText),
1313 null, null, _owner.TitleTextStyle, null));
1316 if (_owner.InstructionText.Length > 0) {
1317 table.Controls.Add (
1318 CreateRow (new LiteralControl (_owner.InstructionText),
1319 null, null, _owner.InstructionTextStyle, null));
1323 if (_owner.DisplayUserName) {
1324 TextBox UserName = new TextBox ();
1325 UserName.ID = "UserName";
1326 UserName.Text = _owner.UserName;
1327 UserName.ApplyStyle (_owner.TextBoxStyle);
1329 Label UserNameLabel = new Label ();
1330 UserNameLabel.ID = "UserNameLabel";
1331 UserNameLabel.AssociatedControlID = "UserName";
1332 UserNameLabel.Text = _owner.UserNameLabelText;
1334 RequiredFieldValidator UserNameRequired = new RequiredFieldValidator ();
1335 UserNameRequired.ID = "UserNameRequired";
1336 UserNameRequired.ControlToValidate = "UserName";
1337 UserNameRequired.ErrorMessage = _owner.UserNameRequiredErrorMessage;
1338 UserNameRequired.ToolTip = _owner.UserNameRequiredErrorMessage;
1339 UserNameRequired.Text = "*";
1340 UserNameRequired.ValidationGroup = _owner.ID;
1341 UserNameRequired.ApplyStyle (_owner.ValidatorTextStyle);
1343 table.Controls.Add (CreateRow (UserNameLabel, UserName, UserNameRequired, _owner.LabelStyle, null));
1347 TextBox CurrentPassword = new TextBox ();
1348 CurrentPassword.ID = "CurrentPassword";
1349 CurrentPassword.TextMode = TextBoxMode.Password;
1350 CurrentPassword.ApplyStyle (_owner.TextBoxStyle);
1352 Label CurrentPasswordLabel = new Label ();
1353 CurrentPasswordLabel.ID = "CurrentPasswordLabel";
1354 CurrentPasswordLabel.AssociatedControlID = "CurrentPasswordLabel";
1355 CurrentPasswordLabel.Text = _owner.PasswordLabelText;
1357 RequiredFieldValidator CurrentPasswordRequired = new RequiredFieldValidator ();
1358 CurrentPasswordRequired.ID = "CurrentPasswordRequired";
1359 CurrentPasswordRequired.ControlToValidate = "CurrentPassword";
1360 CurrentPasswordRequired.ErrorMessage = _owner.PasswordRequiredErrorMessage;
1361 CurrentPasswordRequired.ToolTip = _owner.PasswordRequiredErrorMessage;
1362 CurrentPasswordRequired.Text = "*";
1363 CurrentPasswordRequired.ValidationGroup = _owner.ID;
1364 CurrentPasswordRequired.ApplyStyle (_owner.ValidatorTextStyle);
1366 table.Controls.Add (CreateRow (CurrentPasswordLabel, CurrentPassword, CurrentPasswordRequired, _owner.LabelStyle, null));
1369 TextBox NewPassword = new TextBox ();
1370 NewPassword.ID = "NewPassword";
1371 NewPassword.TextMode = TextBoxMode.Password;
1372 NewPassword.ApplyStyle (_owner.TextBoxStyle);
1374 Label NewPasswordLabel = new Label ();
1375 NewPasswordLabel.ID = "NewPasswordLabel";
1376 NewPasswordLabel.AssociatedControlID = "NewPassword";
1377 NewPasswordLabel.Text = _owner.NewPasswordLabelText;
1379 RequiredFieldValidator NewPasswordRequired = new RequiredFieldValidator ();
1380 NewPasswordRequired.ID = "NewPasswordRequired";
1381 NewPasswordRequired.ControlToValidate = "NewPassword";
1382 NewPasswordRequired.ErrorMessage = _owner.PasswordRequiredErrorMessage;
1383 NewPasswordRequired.ToolTip = _owner.PasswordRequiredErrorMessage;
1384 NewPasswordRequired.Text = "*";
1385 NewPasswordRequired.ValidationGroup = _owner.ID;
1386 NewPasswordRequired.ApplyStyle (_owner.ValidatorTextStyle);
1388 table.Controls.Add (CreateRow (NewPasswordLabel, NewPassword, NewPasswordRequired, _owner.LabelStyle, null));
1391 if (_owner.PasswordHintText.Length > 0) {
1392 table.Controls.Add (
1393 CreateRow (new LiteralControl (""),
1394 new LiteralControl (_owner.PasswordHintText),
1395 new LiteralControl (""),
1396 null, _owner.PasswordHintStyle));
1400 TextBox ConfirmNewPassword = new TextBox ();
1401 ConfirmNewPassword.ID = "ConfirmNewPassword";
1402 ConfirmNewPassword.TextMode = TextBoxMode.Password;
1403 ConfirmNewPassword.ApplyStyle (_owner.TextBoxStyle);
1405 Label ConfirmNewPasswordLabel = new Label ();
1406 ConfirmNewPasswordLabel.ID = "ConfirmNewPasswordLabel";
1407 ConfirmNewPasswordLabel.AssociatedControlID = "ConfirmNewPasswordLabel";
1408 ConfirmNewPasswordLabel.Text = _owner.ConfirmNewPasswordLabelText;
1410 RequiredFieldValidator ConfirmNewPasswordRequired = new RequiredFieldValidator ();
1411 ConfirmNewPasswordRequired.ID = "ConfirmNewPasswordRequired";
1412 ConfirmNewPasswordRequired.ControlToValidate = "ConfirmNewPassword";
1413 ConfirmNewPasswordRequired.ErrorMessage = _owner.PasswordRequiredErrorMessage;
1414 ConfirmNewPasswordRequired.ToolTip = _owner.PasswordRequiredErrorMessage;
1415 ConfirmNewPasswordRequired.Text = "*";
1416 ConfirmNewPasswordRequired.ValidationGroup = _owner.ID;
1417 ConfirmNewPasswordRequired.ApplyStyle (_owner.ValidatorTextStyle);
1419 table.Controls.Add (CreateRow (ConfirmNewPasswordLabel, ConfirmNewPassword, ConfirmNewPasswordRequired, _owner.LabelStyle, null));
1422 CompareValidator NewPasswordCompare = new CompareValidator ();
1423 NewPasswordCompare.ID = "NewPasswordCompare";
1424 NewPasswordCompare.ControlToCompare = "NewPassword";
1425 NewPasswordCompare.ControlToValidate = "ConfirmNewPassword";
1426 NewPasswordCompare.Display = ValidatorDisplay.Dynamic;
1427 NewPasswordCompare.ErrorMessage = _owner.ConfirmPasswordCompareErrorMessage;
1428 NewPasswordCompare.ValidationGroup = _owner.ID;
1430 table.Controls.Add (CreateRow (NewPasswordCompare, null, null, null, null));
1433 Literal FailureTextLiteral = new Literal ();
1434 FailureTextLiteral.ID = "FailureText";
1435 FailureTextLiteral.EnableViewState = false;
1437 if (_owner.FailureTextStyle.ForeColor.IsEmpty)
1438 _owner.FailureTextStyle.ForeColor = System.Drawing.Color.Red;
1440 table.Controls.Add (CreateRow (FailureTextLiteral, null, null, _owner.FailureTextStyle, null));
1443 WebControl ChangePasswordButton = null;
1444 switch (_owner.ChangePasswordButtonType) {
1445 case ButtonType.Button:
1446 ChangePasswordButton = new Button ();
1448 case ButtonType.Image:
1449 ChangePasswordButton = new ImageButton ();
1451 case ButtonType.Link:
1452 ChangePasswordButton = new LinkButton ();
1456 ChangePasswordButton.ID = "ChangePasswordPushButton";
1457 ChangePasswordButton.ApplyStyle (_owner.ChangePasswordButtonStyle);
1458 ((IButtonControl) ChangePasswordButton).CommandName = ChangePassword.ChangePasswordButtonCommandName;
1459 ((IButtonControl) ChangePasswordButton).Text = _owner.ChangePasswordButtonText;
1460 ((IButtonControl) ChangePasswordButton).ValidationGroup = _owner.ID;
1462 WebControl CancelButton = null;
1463 switch (_owner.CancelButtonType) {
1464 case ButtonType.Button:
1465 CancelButton = new Button ();
1467 case ButtonType.Image:
1468 CancelButton = new ImageButton ();
1470 case ButtonType.Link:
1471 CancelButton = new LinkButton ();
1475 CancelButton.ID = "CancelPushButton";
1476 CancelButton.ApplyStyle (_owner.CancelButtonStyle);
1477 ((IButtonControl) CancelButton).CommandName = ChangePassword.CancelButtonCommandName;
1478 ((IButtonControl) CancelButton).Text = _owner.CancelButtonText;
1479 ((IButtonControl) CancelButton).CausesValidation = false;
1481 table.Controls.Add (CreateRow (ChangePasswordButton, CancelButton, new LiteralControl (""), null, null));
1484 TableRow linksRow = new TableRow ();
1485 TableCell linksCell = new TableCell ();
1486 linksCell.ColumnSpan = 2;
1487 linksCell.ControlStyle.CopyFrom (_owner.HyperLinkStyle);
1489 linksRow.Cells.Add (linksCell);
1491 if (AddLink (_owner.HelpPageUrl, _owner.HelpPageText, _owner.HelpPageIconUrl, linksCell))
1492 linksCell.Controls.Add (new LiteralControl ("<br/>"));
1494 if (AddLink (_owner.CreateUserUrl, _owner.CreateUserText, _owner.CreateUserIconUrl, linksCell))
1495 linksCell.Controls.Add (new LiteralControl ("<br/>"));
1497 if (AddLink (_owner.PasswordRecoveryUrl, _owner.PasswordRecoveryText, _owner.PasswordRecoveryIconUrl, linksCell))
1498 linksCell.Controls.Add (new LiteralControl ("<br/>"));
1500 AddLink (_owner.EditProfileUrl, _owner.EditProfileText, _owner.EditProfileIconUrl, linksCell);
1502 table.Controls.Add (linksRow);
1504 container.Controls.Add (table);
1508 sealed class SuccessDefaultTemplate : ITemplate
1510 readonly ChangePassword _cPassword = null;
1512 internal SuccessDefaultTemplate (ChangePassword cPassword)
1514 _cPassword = cPassword;
1517 TableRow CreateRow (Control c0, Style s0, HorizontalAlign align)
1519 TableRow row = new TableRow ();
1520 TableCell cell0 = new TableCell ();
1522 cell0.Controls.Add (c0);
1523 cell0.HorizontalAlign = align;
1525 cell0.ApplyStyle (s0);
1527 row.Controls.Add (cell0);
1531 public void InstantiateIn (Control container)
1533 Table table = new Table ();
1534 table.ControlStyle.Width = Unit.Percentage (100);
1535 table.ControlStyle.Height = Unit.Percentage (100);
1538 table.Controls.Add (
1539 CreateRow (new LiteralControl (_cPassword.SuccessTitleText), _cPassword.TitleTextStyle, HorizontalAlign.Center));
1542 table.Controls.Add (
1543 CreateRow (new LiteralControl (_cPassword.SuccessText), _cPassword.SuccessTextStyle, HorizontalAlign.Center));
1546 WebControl ContinueButton = null;
1547 switch (_cPassword.ChangePasswordButtonType) {
1548 case ButtonType.Button:
1549 ContinueButton = new Button ();
1551 case ButtonType.Image:
1552 ContinueButton = new ImageButton ();
1554 case ButtonType.Link:
1555 ContinueButton = new LinkButton ();
1559 ContinueButton.ID = "ContinueButton";
1560 ContinueButton.ApplyStyle (_cPassword.ContinueButtonStyle);
1561 ((IButtonControl) ContinueButton).CommandName = ChangePassword.ContinueButtonCommandName;
1562 ((IButtonControl) ContinueButton).Text = _cPassword.ContinueButtonText;
1563 ((IButtonControl) ContinueButton).CausesValidation = false;
1565 table.Controls.Add (
1566 CreateRow (ContinueButton, null, HorizontalAlign.Right));
1568 container.Controls.Add (table);
1572 sealed class SuccessContainer : BaseChangePasswordContainer
1574 public SuccessContainer (ChangePassword owner) : base (owner)
1576 ID = "SuccessContainerID";
1578 public Control ChangePasswordButton
1580 get { return FindControl ("Continue"); }