2010-03-02 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System.Web / System.Web.UI.WebControls / PasswordRecovery.cs
blob708030e50f3c6ed25e6a4246c8b033a8c979d2ed
1 //
2 // System.Web.UI.WebControls.PasswordRecovery.cs
3 //
4 // Authors:
5 // Vladimir Krasnov (vladimirk@mainsoft.com)
6 //
7 // (C) 2006 Mainsoft, Inc (http://www.mainsoft.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 #if NET_2_0
31 using System;
32 using System.Net.Mail;
33 using System.Collections;
34 using System.Collections.Specialized;
35 using System.ComponentModel;
36 using System.Drawing.Design;
37 using System.Web;
38 using System.Web.UI;
39 using System.Web.Security;
41 namespace System.Web.UI.WebControls
43 [Bindable (false)]
44 public class PasswordRecovery : CompositeControl
46 static readonly object answerLookupErrorEvent = new object ();
47 static readonly object sendingMailEvent = new object ();
48 static readonly object sendMailErrorEvent = new object ();
49 static readonly object userLookupErrorEvent = new object ();
50 static readonly object verifyingAnswerEvent = new object ();
51 static readonly object verifyingUserEvent = new object ();
53 public static readonly string SubmitButtonCommandName = "Submit";
55 TableItemStyle _failureTextStyle;
56 TableItemStyle _hyperLinkStyle;
57 TableItemStyle _instructionTextStyle;
58 TableItemStyle _labelStyle;
59 Style _submitButtonStyle;
60 TableItemStyle _successTextStyle;
61 Style _textBoxStyle;
62 TableItemStyle _titleTextStyle;
63 Style _validatorTextStyle;
65 MailDefinition _mailDefinition;
66 MembershipProvider _provider = null;
68 ITemplate _questionTemplate = null;
69 ITemplate _successTemplate = null;
70 ITemplate _userNameTemplate = null;
72 QuestionContainer _questionTemplateContainer = null;
73 SuccessContainer _successTemplateContainer = null;
74 UserNameContainer _userNameTemplateContainer = null;
76 PasswordReciveryStep _currentStep = PasswordReciveryStep.StepUserName;
78 string _username = null;
79 string _answer = null;
81 EventHandlerList events = new EventHandlerList ();
83 #region Events
84 public event EventHandler AnswerLookupError {
85 add { events.AddHandler (answerLookupErrorEvent, value); }
86 remove { events.RemoveHandler (answerLookupErrorEvent, value); }
89 public event MailMessageEventHandler SendingMail {
90 add { events.AddHandler (sendingMailEvent, value); }
91 remove { events.RemoveHandler (sendingMailEvent, value); }
94 public event SendMailErrorEventHandler SendMailError {
95 add { events.AddHandler (sendMailErrorEvent, value); }
96 remove { events.RemoveHandler (sendMailErrorEvent, value); }
99 public event EventHandler UserLookupError {
100 add { events.AddHandler (userLookupErrorEvent, value); }
101 remove { events.RemoveHandler (userLookupErrorEvent, value); }
104 public event LoginCancelEventHandler VerifyingAnswer {
105 add { events.AddHandler (verifyingAnswerEvent, value); }
106 remove { events.RemoveHandler (verifyingAnswerEvent, value); }
109 public event LoginCancelEventHandler VerifyingUser {
110 add { events.AddHandler (verifyingUserEvent, value); }
111 remove { events.RemoveHandler (verifyingUserEvent, value); }
113 #endregion
115 public PasswordRecovery ()
119 [Themeable (false)]
120 public virtual string Answer
122 get { return _answer != null ? _answer : string.Empty; }
125 [Localizable (true)]
126 public virtual string AnswerLabelText
128 get { return ViewState.GetString ("AnswerLabelText", "Answer:"); }
129 set { ViewState ["AnswerLabelText"] = value; }
132 [Localizable (true)]
133 public virtual string AnswerRequiredErrorMessage
135 get { return ViewState.GetString ("AnswerRequiredErrorMessage", "Answer is required."); }
136 set { ViewState ["AnswerRequiredErrorMessage"] = value; }
139 public virtual int BorderPadding
141 get { return ViewState.GetInt ("BorderPadding", 1); }
144 if (value < -1)
145 throw new ArgumentOutOfRangeException ();
146 ViewState ["BorderPadding"] = value;
150 [Localizable (true)]
151 public virtual string GeneralFailureText
153 get { return ViewState.GetString ("GeneralFailureText", "Your attempt to retrieve your password was not successful. Please try again."); }
154 set { ViewState ["GeneralFailureText"] = value; }
157 public virtual string HelpPageIconUrl
159 get { return ViewState.GetString ("HelpPageIconUrl", String.Empty); }
160 set { ViewState ["HelpPageIconUrl"] = value; }
163 [Localizable (true)]
164 public virtual string HelpPageText
166 get { return ViewState.GetString ("HelpPageText", String.Empty); }
167 set { ViewState ["HelpPageText"] = value; }
170 public virtual string HelpPageUrl
172 get { return ViewState.GetString ("HelpPageUrl", String.Empty); }
173 set { ViewState ["HelpPageUrl"] = value; }
176 [Themeable (false)]
177 public MailDefinition MailDefinition
181 if (_mailDefinition == null) {
182 _mailDefinition = new MailDefinition ();
183 if (IsTrackingViewState)
184 ((IStateManager) _mailDefinition).TrackViewState ();
186 return _mailDefinition;
190 [Themeable (false)]
191 public virtual string MembershipProvider
193 get { return ViewState.GetString ("MembershipProvider", String.Empty); }
194 set { ViewState ["MembershipProvider"] = value; }
197 [Themeable (false)]
198 public virtual string Question
200 get { return ViewState.GetString ("Question", ""); }
201 private set { ViewState ["Question"] = value; }
204 [Localizable (true)]
205 public virtual string QuestionFailureText
207 get { return ViewState.GetString ("QuestionFailureText", "Your answer could not be verified. Please try again."); }
208 set { ViewState ["QuestionFailureText"] = value; }
211 [Localizable (true)]
212 public virtual string QuestionInstructionText
214 get { return ViewState.GetString ("QuestionInstructionText", "Answer the following question to receive your password."); }
215 set { ViewState ["QuestionInstructionText"] = value; }
218 [Localizable (true)]
219 public virtual string QuestionLabelText
221 get { return ViewState.GetString ("QuestionLabelText", "Question:"); }
222 set { ViewState ["QuestionLabelText"] = value; }
225 [Localizable (true)]
226 public virtual string QuestionTitleText
228 get { return ViewState.GetString ("QuestionTitleText", "Identity Confirmation"); }
229 set { ViewState ["QuestionTitleText"] = value; }
232 public virtual string SubmitButtonImageUrl
234 get { return ViewState.GetString ("SubmitButtonImageUrl", String.Empty); }
235 set { ViewState ["SubmitButtonImageUrl"] = value; }
238 [Localizable (true)]
239 public virtual string SubmitButtonText
241 get { return ViewState.GetString ("SubmitButtonText", "Submit"); }
242 set { ViewState ["SubmitButtonText"] = value; }
245 public virtual ButtonType SubmitButtonType
249 object o = ViewState ["SubmitButtonType"];
250 return (o == null) ? ButtonType.Button : (ButtonType) o;
254 if ((value < ButtonType.Button) || (value > ButtonType.Link))
255 throw new ArgumentOutOfRangeException ("SubmitButtonType");
256 ViewState ["SubmitButtonType"] = (int) value;
260 [Themeable (false)]
261 public virtual string SuccessPageUrl
263 get { return ViewState.GetString ("SuccessPageUrl", String.Empty); }
264 set { ViewState ["SuccessPageUrl"] = value; }
267 [Localizable (true)]
268 public virtual string SuccessText
270 get { return ViewState.GetString ("SuccessText", "Your password has been sent to you."); }
271 set { ViewState ["SuccessText"] = value; }
274 public virtual LoginTextLayout TextLayout
278 object o = ViewState ["TextLayout"];
279 return (o == null) ? LoginTextLayout.TextOnLeft : (LoginTextLayout) o;
283 if ((value < LoginTextLayout.TextOnLeft) || (value > LoginTextLayout.TextOnTop))
284 throw new ArgumentOutOfRangeException ("TextLayout");
285 ViewState ["TextLayout"] = (int) value;
289 [Localizable (true)]
290 public virtual string UserName
292 get { return _username != null ? _username : ""; }
293 set { _username = value; }
296 [Localizable (true)]
297 public virtual string UserNameFailureText
299 get { return ViewState.GetString ("UserNameFailureText", "We were unable to access your information. Please try again."); }
300 set { ViewState ["UserNameFailureText"] = value; }
303 [Localizable (true)]
304 public virtual string UserNameInstructionText
306 get { return ViewState.GetString ("UserNameInstructionText", "Enter your User Name to receive your password."); }
307 set { ViewState ["UserNameInstructionText"] = value; }
310 [Localizable (true)]
311 public virtual string UserNameLabelText
313 get { return ViewState.GetString ("UserNameLabelText", "User Name:"); }
314 set { ViewState ["UserNameLabelText"] = value; }
317 [Localizable (true)]
318 public virtual string UserNameRequiredErrorMessage
320 get { return ViewState.GetString ("UserNameRequiredErrorMessage", "User Name is required."); }
321 set { ViewState ["UserNameRequiredErrorMessage"] = value; }
324 [Localizable (true)]
325 public virtual string UserNameTitleText
327 get { return ViewState.GetString ("UserNameTitleText", "Forgot Your Password?"); }
328 set { ViewState ["UserNameTitleText"] = value; }
331 [TemplateContainer (typeof (PasswordRecovery))]
332 public virtual ITemplate QuestionTemplate
334 get { return _questionTemplate; }
335 set { _questionTemplate = value; }
338 public Control QuestionTemplateContainer
340 get {
341 if (_questionTemplateContainer == null) {
342 _questionTemplateContainer = new QuestionContainer (this);
343 ITemplate template = QuestionTemplate;
344 if (template != null)
345 _questionTemplateContainer.InstantiateTemplate (template);
348 return _questionTemplateContainer;
352 [TemplateContainer (typeof (PasswordRecovery))]
353 public virtual ITemplate SuccessTemplate
355 get { return _successTemplate; }
356 set { _successTemplate = value; }
359 public Control SuccessTemplateContainer
361 get {
362 if (_successTemplateContainer == null) {
363 _successTemplateContainer = new SuccessContainer (this);
364 ITemplate template = SuccessTemplate;
365 if (template != null)
366 _successTemplateContainer.InstantiateTemplate (template);
369 return _successTemplateContainer;
373 [TemplateContainer (typeof (PasswordRecovery))]
374 public virtual ITemplate UserNameTemplate
376 get { return _userNameTemplate; }
377 set { _userNameTemplate = value; }
380 public Control UserNameTemplateContainer
384 if (_userNameTemplateContainer == null) {
385 _userNameTemplateContainer = new UserNameContainer (this);
386 ITemplate template = UserNameTemplate;
387 if (template != null)
388 _userNameTemplateContainer.InstantiateTemplate (template);
391 return _userNameTemplateContainer;
395 public TableItemStyle FailureTextStyle
399 if (_failureTextStyle == null) {
400 _failureTextStyle = new TableItemStyle ();
401 if (IsTrackingViewState)
402 _failureTextStyle.TrackViewState ();
404 return _failureTextStyle;
408 public TableItemStyle HyperLinkStyle
412 if (_hyperLinkStyle == null) {
413 _hyperLinkStyle = new TableItemStyle ();
414 if (IsTrackingViewState)
415 _hyperLinkStyle.TrackViewState ();
417 return _hyperLinkStyle;
421 public TableItemStyle InstructionTextStyle
425 if (_instructionTextStyle == null) {
426 _instructionTextStyle = new TableItemStyle ();
427 if (IsTrackingViewState)
428 _instructionTextStyle.TrackViewState ();
430 return _instructionTextStyle;
434 public TableItemStyle LabelStyle
438 if (_labelStyle == null) {
439 _labelStyle = new TableItemStyle ();
440 if (IsTrackingViewState)
441 _labelStyle.TrackViewState ();
443 return _labelStyle;
447 public Style SubmitButtonStyle
451 if (_submitButtonStyle == null) {
452 _submitButtonStyle = new TableItemStyle ();
453 if (IsTrackingViewState)
454 _submitButtonStyle.TrackViewState ();
456 return _submitButtonStyle;
460 public TableItemStyle SuccessTextStyle
464 if (_successTextStyle == null) {
465 _successTextStyle = new TableItemStyle ();
466 if (IsTrackingViewState)
467 _successTextStyle.TrackViewState ();
469 return _successTextStyle;
473 public Style TextBoxStyle
477 if (_textBoxStyle == null) {
478 _textBoxStyle = new TableItemStyle ();
479 if (IsTrackingViewState)
480 _textBoxStyle.TrackViewState ();
482 return _textBoxStyle;
486 public TableItemStyle TitleTextStyle
490 if (_titleTextStyle == null) {
491 _titleTextStyle = new TableItemStyle ();
492 if (IsTrackingViewState)
493 _titleTextStyle.TrackViewState ();
495 return _titleTextStyle;
499 public Style ValidatorTextStyle
503 if (_validatorTextStyle == null) {
504 _validatorTextStyle = new TableItemStyle ();
505 if (IsTrackingViewState)
506 _validatorTextStyle.TrackViewState ();
508 return _validatorTextStyle;
512 #region Protected Properties
514 protected override HtmlTextWriterTag TagKey
516 get { return HtmlTextWriterTag.Table; }
519 internal virtual MembershipProvider MembershipProviderInternal
523 if (_provider == null)
524 InitMemberShipProvider ();
526 return _provider;
530 #endregion
532 protected internal override void CreateChildControls ()
534 ITemplate userNameTemplate = UserNameTemplate;
535 if (userNameTemplate == null) {
536 userNameTemplate = new UserNameDefaultTemplate (this);
537 ((UserNameContainer) UserNameTemplateContainer).InstantiateTemplate (userNameTemplate);
540 ITemplate questionTemplate = QuestionTemplate;
541 if (questionTemplate == null) {
542 questionTemplate = new QuestionDefaultTemplate (this);
543 ((QuestionContainer) QuestionTemplateContainer).InstantiateTemplate (questionTemplate);
546 ITemplate successTemplate = SuccessTemplate;
547 if (successTemplate == null) {
548 successTemplate = new SuccessDefaultTemplate (this);
549 ((SuccessContainer) SuccessTemplateContainer).InstantiateTemplate (successTemplate);
552 Controls.AddAt (0, UserNameTemplateContainer);
553 Controls.AddAt (1, QuestionTemplateContainer);
554 Controls.AddAt (2, SuccessTemplateContainer);
556 IEditableTextControl editable;
558 editable = ((UserNameContainer) UserNameTemplateContainer).UserNameTextBox;
559 if (editable != null)
560 editable.TextChanged += new EventHandler (UserName_TextChanged);
562 editable = ((QuestionContainer) QuestionTemplateContainer).AnswerTextBox;
563 if (editable != null)
564 editable.TextChanged += new EventHandler (Answer_TextChanged);
567 #region Protected methods
569 protected internal override void Render (HtmlTextWriter writer)
571 ((QuestionContainer) QuestionTemplateContainer).UpdateChildControls ();
573 for (int i = 0; i < Controls.Count; i++)
574 if (Controls [i].Visible)
575 Controls [i].Render (writer);
578 protected internal override void LoadControlState (object savedState)
580 if (savedState == null) return;
581 object [] state = (object []) savedState;
582 base.LoadControlState (state [0]);
584 _currentStep = (PasswordReciveryStep) state [1];
585 _username = (string) state [2];
588 protected internal override object SaveControlState ()
590 object state = base.SaveControlState ();
591 return new object [] { state, _currentStep, _username };
594 protected override void TrackViewState ()
596 base.TrackViewState ();
598 if (_failureTextStyle != null)
599 _failureTextStyle.TrackViewState ();
601 if (_hyperLinkStyle != null)
602 _hyperLinkStyle.TrackViewState ();
604 if (_instructionTextStyle != null)
605 _instructionTextStyle.TrackViewState ();
607 if (_labelStyle != null)
608 _labelStyle.TrackViewState ();
610 if (_submitButtonStyle != null)
611 _submitButtonStyle.TrackViewState ();
613 if (_successTextStyle != null)
614 _successTextStyle.TrackViewState ();
616 if (_textBoxStyle != null)
617 _textBoxStyle.TrackViewState ();
619 if (_titleTextStyle != null)
620 _titleTextStyle.TrackViewState ();
622 if (_validatorTextStyle != null)
623 _validatorTextStyle.TrackViewState ();
625 if (_mailDefinition != null)
626 ((IStateManager) _mailDefinition).TrackViewState ();
629 protected override void LoadViewState (object savedState)
631 if (savedState == null)
632 return;
634 object [] states = (object []) savedState;
635 base.LoadViewState (states [0]);
637 if (states [1] != null)
638 FailureTextStyle.LoadViewState (states [1]);
640 if (states [2] != null)
641 HyperLinkStyle.LoadViewState (states [2]);
643 if (states [3] != null)
644 InstructionTextStyle.LoadViewState (states [3]);
646 if (states [4] != null)
647 LabelStyle.LoadViewState (states [4]);
649 if (states [5] != null)
650 SubmitButtonStyle.LoadViewState (states [5]);
652 if (states [6] != null)
653 SuccessTextStyle.LoadViewState (states [6]);
655 if (states [7] != null)
656 TextBoxStyle.LoadViewState (states [7]);
658 if (states [8] != null)
659 TitleTextStyle.LoadViewState (states [8]);
661 if (states [9] != null)
662 ValidatorTextStyle.LoadViewState (states [9]);
664 if (states [10] != null)
665 ((IStateManager) MailDefinition).LoadViewState (states [10]);
668 protected override object SaveViewState ()
670 object [] states = new object [11];
671 states [0] = base.SaveViewState ();
673 if (_failureTextStyle != null)
674 states [1] = _failureTextStyle.SaveViewState ();
676 if (_hyperLinkStyle != null)
677 states [2] = _hyperLinkStyle.SaveViewState ();
679 if (_instructionTextStyle != null)
680 states [3] = _instructionTextStyle.SaveViewState ();
682 if (_labelStyle != null)
683 states [4] = _labelStyle.SaveViewState ();
685 if (_submitButtonStyle != null)
686 states [5] = _submitButtonStyle.SaveViewState ();
688 if (_successTextStyle != null)
689 states [6] = _successTextStyle.SaveViewState ();
691 if (_textBoxStyle != null)
692 states [7] = _textBoxStyle.SaveViewState ();
694 if (_titleTextStyle != null)
695 states [8] = _titleTextStyle.SaveViewState ();
697 if (_validatorTextStyle != null)
698 states [9] = _validatorTextStyle.SaveViewState ();
700 if (_mailDefinition != null)
701 states [10] = ((IStateManager) _mailDefinition).SaveViewState ();
703 for (int i = 0; i < states.Length; i++) {
704 if (states [i] != null)
705 return states;
707 return null;
711 #endregion
713 void ProcessCommand (CommandEventArgs args)
715 if (!Page.IsValid)
716 return;
718 switch (_currentStep) {
719 case PasswordReciveryStep.StepUserName:
720 ProcessUserName ();
721 break;
722 case PasswordReciveryStep.StepAnswer:
723 ProcessUserAnswer ();
724 break;
729 void ProcessUserName ()
731 LoginCancelEventArgs args = new LoginCancelEventArgs ();
732 OnVerifyingUser (args);
733 if (args.Cancel)
734 return;
736 MembershipUser user = MembershipProviderInternal.GetUser (UserName, false);
737 if (user == null) {
738 OnUserLookupError (EventArgs.Empty);
739 ((UserNameContainer) UserNameTemplateContainer).FailureTextLiteral.Text = UserNameFailureText;
740 return;
743 if (!MembershipProviderInternal.RequiresQuestionAndAnswer) {
744 GenerateAndSendEmail ();
746 _currentStep = PasswordReciveryStep.StepSuccess;
747 return;
750 Question = user.PasswordQuestion;
751 _currentStep = PasswordReciveryStep.StepAnswer;
752 return;
755 void ProcessUserAnswer ()
757 LoginCancelEventArgs args = new LoginCancelEventArgs ();
758 OnVerifyingAnswer (args);
759 if (args.Cancel)
760 return;
762 MembershipUser user = MembershipProviderInternal.GetUser (UserName, false);
763 if (user == null || string.IsNullOrEmpty (user.Email)) {
764 ((QuestionContainer) QuestionTemplateContainer).FailureTextLiteral.Text = GeneralFailureText;
765 return;
768 GenerateAndSendEmail ();
770 _currentStep = PasswordReciveryStep.StepSuccess;
771 return;
774 void GenerateAndSendEmail ()
776 string newPassword = "";
777 try {
778 if (MembershipProviderInternal.EnablePasswordRetrieval) {
779 newPassword = MembershipProviderInternal.GetPassword (UserName, Answer);
781 else if (MembershipProviderInternal.EnablePasswordReset) {
782 newPassword = MembershipProviderInternal.ResetPassword (UserName, Answer);
784 else
785 throw new HttpException ("Membership provider does not support password retrieval or reset.");
787 catch (MembershipPasswordException) {
788 OnAnswerLookupError (EventArgs.Empty);
789 ((QuestionContainer) QuestionTemplateContainer).FailureTextLiteral.Text = QuestionFailureText;
790 return;
793 SendPasswordByMail (UserName, newPassword);
796 void InitMemberShipProvider ()
798 string mp = MembershipProvider;
799 _provider = (mp.Length == 0) ? _provider = Membership.Provider : Membership.Providers [mp];
800 if (_provider == null)
801 throw new HttpException (Locale.GetText ("No provider named '{0}' could be found.", mp));
804 void SendPasswordByMail (string username, string password)
806 MembershipUser user = MembershipProviderInternal.GetUser (UserName, false);
807 if (user == null)
808 return;
810 // DO NOT change format of the message - it has to be exactly the same as in
811 // .NET as some software (e.g. YetAnotherForum) depends on it.
812 string messageText = "Please return to the site and log in using the following information.\n" +
813 "User Name: <%USERNAME%>\nPassword: <%PASSWORD%>\n";
815 ListDictionary dictionary = new ListDictionary (StringComparer.OrdinalIgnoreCase);
816 dictionary.Add ("<%USERNAME%>", username);
817 dictionary.Add ("<% UserName %>", username);
818 dictionary.Add ("<%PASSWORD%>", password);
819 dictionary.Add ("<% Password %>", password);
821 MailMessage message = null;
823 if (MailDefinition.BodyFileName.Length == 0)
824 message = MailDefinition.CreateMailMessage (user.Email, dictionary, messageText, this);
825 else
826 message = MailDefinition.CreateMailMessage (user.Email, dictionary, this);
828 if (string.IsNullOrEmpty (message.Subject))
829 message.Subject = "Password";
831 MailMessageEventArgs args = new MailMessageEventArgs (message);
832 OnSendingMail (args);
834 SmtpClient smtpClient = new SmtpClient ();
835 try {
836 smtpClient.Send (message);
838 catch (Exception e) {
839 SendMailErrorEventArgs mailArgs = new SendMailErrorEventArgs (e);
840 OnSendMailError (mailArgs);
841 if (!mailArgs.Handled)
842 throw e;
846 #region Event handlers
848 protected virtual void OnAnswerLookupError (EventArgs e)
852 protected override bool OnBubbleEvent (object source, EventArgs e)
854 CommandEventArgs args = e as CommandEventArgs;
855 if (e != null && args.CommandName == SubmitButtonCommandName) {
856 ProcessCommand (args);
857 return true;
859 return base.OnBubbleEvent (source, e);
862 protected internal override void OnInit (EventArgs e)
864 Page.RegisterRequiresControlState (this);
865 base.OnInit (e);
868 protected internal override void OnPreRender (EventArgs e)
870 UserNameTemplateContainer.Visible = false;
871 QuestionTemplateContainer.Visible = false;
872 SuccessTemplateContainer.Visible = false;
874 switch (_currentStep) {
875 case PasswordReciveryStep.StepUserName:
876 UserNameTemplateContainer.Visible = true;
877 break;
878 case PasswordReciveryStep.StepAnswer:
879 QuestionTemplateContainer.Visible = true;
880 break;
881 case PasswordReciveryStep.StepSuccess:
882 SuccessTemplateContainer.Visible = true;
883 break;
886 base.OnPreRender (e);
889 protected virtual void OnSendingMail (MailMessageEventArgs e)
891 MailMessageEventHandler eh = events [sendingMailEvent] as MailMessageEventHandler;
892 if (eh != null)
893 eh (this, e);
896 protected virtual void OnSendMailError (SendMailErrorEventArgs e)
898 SendMailErrorEventHandler eh = events [sendingMailEvent] as SendMailErrorEventHandler;
899 if (eh != null)
900 eh (this, e);
903 protected virtual void OnUserLookupError (EventArgs e)
905 EventHandler eh = events [userLookupErrorEvent] as EventHandler;
906 if (eh != null)
907 eh (this, e);
910 protected virtual void OnVerifyingAnswer (LoginCancelEventArgs e)
912 LoginCancelEventHandler eh = events [verifyingAnswerEvent] as LoginCancelEventHandler;
913 if (eh != null)
914 eh (this, e);
917 protected virtual void OnVerifyingUser (LoginCancelEventArgs e)
919 LoginCancelEventHandler eh = events [verifyingUserEvent] as LoginCancelEventHandler;
920 if (eh != null)
921 eh (this, e);
924 #endregion
926 #region Private Event Handlers
928 void UserName_TextChanged (object sender, EventArgs e)
930 UserName = ((ITextControl) sender).Text;
933 void Answer_TextChanged (object sender, EventArgs e)
935 _answer = ((ITextControl) sender).Text;
938 #endregion
940 [MonoTODO ("Not implemented")]
941 protected override void SetDesignModeState (IDictionary data)
943 throw new NotImplementedException ();
947 abstract class BasePasswordRecoveryContainer : Table, INamingContainer
949 protected readonly PasswordRecovery _owner = null;
950 TableCell _containerCell = null;
952 public BasePasswordRecoveryContainer (PasswordRecovery owner)
954 _owner = owner;
955 InitTable ();
958 public void InstantiateTemplate (ITemplate template)
960 template.InstantiateIn (_containerCell);
963 void InitTable ()
965 Attributes.Add ("ID", _owner.ID);
967 CellSpacing = 0;
968 CellPadding = _owner.BorderPadding;
970 _containerCell = new TableCell ();
972 TableRow row = new TableRow ();
973 row.Cells.Add (_containerCell);
974 Rows.Add (row);
977 protected internal override void OnPreRender (EventArgs e)
979 ApplyStyle (_owner.ControlStyle);
980 base.OnPreRender (e);
983 public abstract void UpdateChildControls();
987 sealed class QuestionContainer : BasePasswordRecoveryContainer
989 public QuestionContainer (PasswordRecovery owner)
990 : base (owner)
993 // Requried controls
994 public IEditableTextControl AnswerTextBox
998 Control c = FindControl ("Answer");
999 if (c == null)
1000 throw new HttpException ("QuestionTemplate does not contain an IEditableTextControl with ID Answer for the username.");
1001 return c as IEditableTextControl;
1004 // Optional controls
1005 public Literal UserNameLiteral
1007 get { return FindControl ("UserName") as Literal; }
1009 public Literal QuestionLiteral
1011 get { return FindControl ("Question") as Literal; }
1013 public Literal FailureTextLiteral
1015 get { return FindControl ("FailureText") as Literal; }
1018 public override void UpdateChildControls ()
1020 if (UserNameLiteral != null)
1021 UserNameLiteral.Text = _owner.UserName;
1023 if (QuestionLiteral != null)
1024 QuestionLiteral.Text = _owner.Question;
1028 sealed class SuccessContainer : BasePasswordRecoveryContainer
1030 public SuccessContainer (PasswordRecovery owner)
1031 : base (owner)
1035 public override void UpdateChildControls ()
1040 sealed class UserNameContainer : BasePasswordRecoveryContainer
1042 public UserNameContainer (PasswordRecovery owner)
1043 : base (owner)
1046 // Requried controls
1047 public IEditableTextControl UserNameTextBox
1051 Control c = FindControl ("UserName");
1052 if (c == null)
1053 throw new HttpException ("UserNameTemplate does not contain an IEditableTextControl with ID UserName for the username.");
1054 return c as IEditableTextControl;
1057 // Optional controls
1058 public ITextControl FailureTextLiteral
1060 get { return FindControl ("FailureText") as ITextControl; }
1063 public override void UpdateChildControls ()
1068 class TemplateUtils
1070 public static TableRow CreateRow(Control c1, Control c2, Style s1, Style s2, bool twoCells)
1072 TableRow row = new TableRow ();
1073 TableCell cell1 = new TableCell ();
1075 cell1.Controls.Add (c1);
1076 if (s1 != null)
1077 cell1.ApplyStyle (s1);
1079 row.Cells.Add (cell1);
1081 if (c2 != null) {
1082 TableCell cell2 = new TableCell ();
1083 cell2.Controls.Add (c2);
1085 if (s2 != null)
1086 cell2.ApplyStyle (s2);
1088 row.Cells.Add (cell2);
1090 cell1.HorizontalAlign = HorizontalAlign.Right;
1091 cell2.HorizontalAlign = HorizontalAlign.Left;
1093 else {
1094 cell1.HorizontalAlign = HorizontalAlign.Center;
1095 if (twoCells)
1096 cell1.ColumnSpan = 2;
1099 return row;
1102 public static TableRow CreateHelpRow (string pageUrl, string linkText, string linkIcon, Style linkStyle, bool twoCells)
1104 TableRow row = new TableRow ();
1105 TableCell cell1 = new TableCell ();
1107 if (linkIcon.Length > 0) {
1108 Image img = new Image ();
1109 img.ImageUrl = linkIcon;
1110 cell1.Controls.Add (img);
1112 if (linkText.Length > 0) {
1113 HyperLink link = new HyperLink ();
1114 link.NavigateUrl = pageUrl;
1115 link.Text = linkText;
1116 link.ControlStyle.CopyTextStylesFrom (linkStyle);
1117 cell1.Controls.Add (link);
1120 if (twoCells)
1121 cell1.ColumnSpan = 2;
1123 row.ControlStyle.CopyFrom (linkStyle);
1124 row.Cells.Add (cell1);
1125 return row;
1129 sealed class UserNameDefaultTemplate : ITemplate
1131 readonly PasswordRecovery _owner = null;
1133 public UserNameDefaultTemplate (PasswordRecovery _owner)
1135 this._owner = _owner;
1138 public void InstantiateIn (Control container)
1140 Table table = new Table ();
1141 table.CellPadding = 0;
1143 bool twoCells = _owner.TextLayout == LoginTextLayout.TextOnLeft;
1145 // row 0
1146 table.Rows.Add (
1147 TemplateUtils.CreateRow (new LiteralControl (_owner.UserNameTitleText), null, _owner.TitleTextStyle, null, twoCells));
1149 // row 1
1150 table.Rows.Add (
1151 TemplateUtils.CreateRow (new LiteralControl (_owner.UserNameInstructionText), null, _owner.InstructionTextStyle, null, twoCells));
1153 // row 2
1154 TextBox UserNameTextBox = new TextBox ();
1155 UserNameTextBox.ID = "UserName";
1156 UserNameTextBox.Text = _owner.UserName;
1157 UserNameTextBox.ApplyStyle (_owner.TextBoxStyle);
1159 Label UserNameLabel = new Label ();
1160 UserNameLabel.ID = "UserNameLabel";
1161 UserNameLabel.AssociatedControlID = "UserName";
1162 UserNameLabel.Text = _owner.UserNameLabelText;
1163 UserNameLabel.ApplyStyle (_owner.LabelStyle);
1165 RequiredFieldValidator UserNameRequired = new RequiredFieldValidator ();
1166 UserNameRequired.ID = "UserNameRequired";
1167 UserNameRequired.ControlToValidate = "UserName";
1168 UserNameRequired.ErrorMessage = _owner.UserNameRequiredErrorMessage;
1169 UserNameRequired.ToolTip = _owner.UserNameRequiredErrorMessage;
1170 UserNameRequired.Text = "*";
1171 UserNameRequired.ValidationGroup = _owner.ID;
1172 UserNameRequired.ApplyStyle (_owner.ValidatorTextStyle);
1174 if (twoCells) {
1175 TableRow row = TemplateUtils.CreateRow (UserNameLabel, UserNameTextBox, null, null, twoCells);
1176 row.Cells [1].Controls.Add (UserNameRequired);
1177 table.Rows.Add (row);
1179 else {
1180 table.Rows.Add (TemplateUtils.CreateRow (UserNameLabel, null, null, null, twoCells));
1181 TableRow row = TemplateUtils.CreateRow (UserNameTextBox, null, null, null, twoCells);
1182 row.Cells [0].Controls.Add (UserNameRequired);
1183 table.Rows.Add (row);
1186 // row 3
1187 Literal FailureText = new Literal ();
1188 FailureText.ID = "FailureText";
1189 if (_owner.FailureTextStyle.ForeColor.IsEmpty)
1190 _owner.FailureTextStyle.ForeColor = System.Drawing.Color.Red;
1191 table.Rows.Add (TemplateUtils.CreateRow (FailureText, null, _owner.FailureTextStyle, null, twoCells));
1193 // row 4
1194 WebControl SubmitButton = null;
1195 switch (_owner.SubmitButtonType) {
1196 case ButtonType.Button:
1197 SubmitButton = new Button ();
1198 break;
1199 case ButtonType.Image:
1200 SubmitButton = new ImageButton ();
1201 break;
1202 case ButtonType.Link:
1203 SubmitButton = new LinkButton ();
1204 break;
1207 SubmitButton.ID = "SubmitButton";
1208 SubmitButton.ApplyStyle (_owner.SubmitButtonStyle);
1209 ((IButtonControl) SubmitButton).CommandName = PasswordRecovery.SubmitButtonCommandName;
1210 ((IButtonControl) SubmitButton).Text = _owner.SubmitButtonText;
1211 ((IButtonControl) SubmitButton).ValidationGroup = _owner.ID;
1213 TableRow buttonRow = TemplateUtils.CreateRow (SubmitButton, null, null, null, twoCells);
1214 buttonRow.Cells [0].HorizontalAlign = HorizontalAlign.Right;
1215 table.Rows.Add (buttonRow);
1217 // row 5
1218 table.Rows.Add (
1219 TemplateUtils.CreateHelpRow (
1220 _owner.HelpPageUrl, _owner.HelpPageText, _owner.HelpPageIconUrl, _owner.HyperLinkStyle, twoCells));
1222 container.Controls.Add (table);
1226 sealed class QuestionDefaultTemplate : ITemplate
1228 readonly PasswordRecovery _owner = null;
1230 public QuestionDefaultTemplate (PasswordRecovery _owner)
1232 this._owner = _owner;
1235 public void InstantiateIn (Control container)
1237 Table table = new Table ();
1238 table.CellPadding = 0;
1240 bool twoCells = _owner.TextLayout == LoginTextLayout.TextOnLeft;
1242 // row 0
1243 table.Rows.Add (
1244 TemplateUtils.CreateRow (new LiteralControl (_owner.QuestionTitleText), null, _owner.TitleTextStyle, null, twoCells));
1246 // row 1
1247 table.Rows.Add (
1248 TemplateUtils.CreateRow (new LiteralControl (_owner.QuestionInstructionText), null, _owner.InstructionTextStyle, null, twoCells));
1250 // row 2
1251 Literal UserNameLiteral = new Literal ();
1252 UserNameLiteral.ID = "UserName";
1254 table.Rows.Add (
1255 TemplateUtils.CreateRow (new LiteralControl (_owner.UserNameLabelText), UserNameLiteral, _owner.LabelStyle, _owner.LabelStyle, twoCells));
1257 // row 3
1258 Literal QuestionLiteral = new Literal ();
1259 QuestionLiteral.ID = "Question";
1261 table.Rows.Add (
1262 TemplateUtils.CreateRow (new LiteralControl (_owner.QuestionLabelText), QuestionLiteral, _owner.LabelStyle, _owner.LabelStyle, twoCells));
1264 // row 5
1265 TextBox AnswerTextBox = new TextBox ();
1266 AnswerTextBox.ID = "Answer";
1267 AnswerTextBox.ApplyStyle (_owner.TextBoxStyle);
1269 Label AnswerLabel = new Label ();
1270 AnswerLabel.ID = "AnswerLabel";
1271 AnswerLabel.AssociatedControlID = "Answer";
1272 AnswerLabel.Text = _owner.AnswerLabelText;
1273 AnswerLabel.ApplyStyle (_owner.LabelStyle);
1275 RequiredFieldValidator AnswerRequired = new RequiredFieldValidator ();
1276 AnswerRequired.ID = "AnswerRequired";
1277 AnswerRequired.ControlToValidate = "Answer";
1278 AnswerRequired.ErrorMessage = _owner.AnswerRequiredErrorMessage;
1279 AnswerRequired.ToolTip = _owner.AnswerRequiredErrorMessage;
1280 AnswerRequired.Text = "*";
1281 AnswerRequired.ValidationGroup = _owner.ID;
1282 AnswerRequired.ApplyStyle (_owner.ValidatorTextStyle);
1284 if (twoCells) {
1285 TableRow row = TemplateUtils.CreateRow (AnswerLabel, AnswerTextBox, null, null, twoCells);
1286 row.Cells [1].Controls.Add (AnswerRequired);
1287 table.Rows.Add (row);
1289 else {
1290 table.Rows.Add (TemplateUtils.CreateRow (AnswerLabel, null, null, null, twoCells));
1291 TableRow row = TemplateUtils.CreateRow (AnswerTextBox, null, null, null, twoCells);
1292 row.Cells [0].Controls.Add (AnswerRequired);
1293 table.Rows.Add (row);
1296 // row 6
1297 Literal FailureText = new Literal ();
1298 FailureText.ID = "FailureText";
1299 if (_owner.FailureTextStyle.ForeColor.IsEmpty)
1300 _owner.FailureTextStyle.ForeColor = System.Drawing.Color.Red;
1301 table.Rows.Add (TemplateUtils.CreateRow (FailureText, null, _owner.FailureTextStyle, null, twoCells));
1303 // row 7
1304 WebControl SubmitButton = null;
1305 switch (_owner.SubmitButtonType) {
1306 case ButtonType.Button:
1307 SubmitButton = new Button ();
1308 break;
1309 case ButtonType.Image:
1310 SubmitButton = new ImageButton ();
1311 break;
1312 case ButtonType.Link:
1313 SubmitButton = new LinkButton ();
1314 break;
1317 SubmitButton.ID = "SubmitButton";
1318 SubmitButton.ApplyStyle (_owner.SubmitButtonStyle);
1319 ((IButtonControl) SubmitButton).CommandName = PasswordRecovery.SubmitButtonCommandName;
1320 ((IButtonControl) SubmitButton).Text = _owner.SubmitButtonText;
1321 ((IButtonControl) SubmitButton).ValidationGroup = _owner.ID;
1323 TableRow buttonRow = TemplateUtils.CreateRow (SubmitButton, null, null, null, twoCells);
1324 buttonRow.Cells [0].HorizontalAlign = HorizontalAlign.Right;
1325 table.Rows.Add (buttonRow);
1327 // row 8
1328 table.Rows.Add (
1329 TemplateUtils.CreateHelpRow (
1330 _owner.HelpPageUrl, _owner.HelpPageText, _owner.HelpPageIconUrl, _owner.HyperLinkStyle, twoCells));
1332 container.Controls.Add (table);
1336 sealed class SuccessDefaultTemplate : ITemplate
1338 readonly PasswordRecovery _owner = null;
1340 public SuccessDefaultTemplate (PasswordRecovery _owner)
1342 this._owner = _owner;
1345 public void InstantiateIn (Control container)
1347 Table table = new Table ();
1348 table.CellPadding = 0;
1350 bool twoCells = _owner.TextLayout == LoginTextLayout.TextOnLeft;
1352 // row 0
1353 table.Rows.Add (
1354 TemplateUtils.CreateRow (new LiteralControl (_owner.SuccessText), null, _owner.SuccessTextStyle, null, twoCells));
1356 container.Controls.Add (table);
1360 enum PasswordReciveryStep
1362 StepUserName,
1363 StepAnswer,
1364 StepSuccess,
1369 #endif