2 // System.Web.UI.WebControls.PasswordRecovery.cs
5 // Vladimir Krasnov (vladimirk@mainsoft.com)
7 // (C) 2006 Mainsoft, Inc (http://www.mainsoft.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
32 using System.Net.Mail;
33 using System.Collections;
34 using System.Collections.Specialized;
35 using System.ComponentModel;
36 using System.Drawing.Design;
39 using System.Web.Security;
41 namespace System.Web.UI.WebControls
44 public class PasswordRecovery : CompositeControl
46 public static readonly string SubmitButtonCommandName = "Submit";
48 TableItemStyle _failureTextStyle;
49 TableItemStyle _hyperLinkStyle;
50 TableItemStyle _instructionTextStyle;
51 TableItemStyle _labelStyle;
52 Style _submitButtonStyle;
53 TableItemStyle _successTextStyle;
55 TableItemStyle _titleTextStyle;
56 Style _validatorTextStyle;
58 MailDefinition _mailDefinition;
59 MembershipProvider _provider = null;
61 ITemplate _questionTemplate = null;
62 ITemplate _successTemplate = null;
63 ITemplate _userNameTemplate = null;
65 QuestionContainer _questionTemplateContainer = null;
66 SuccessContainer _successTemplateContainer = null;
67 UserNameContainer _userNameTemplateContainer = null;
69 PasswordReciveryStep _currentStep = PasswordReciveryStep.StepUserName;
71 string _username = null;
72 string _answer = null;
74 public PasswordRecovery ()
79 public virtual string Answer
81 get { return _answer != null ? _answer : string.Empty; }
85 public virtual string AnswerLabelText
87 get { return ViewState.GetString ("AnswerLabelText", "Answer:"); }
88 set { ViewState ["AnswerLabelText"] = value; }
92 public virtual string AnswerRequiredErrorMessage
94 get { return ViewState.GetString ("AnswerRequiredErrorMessage", "Answer is required."); }
95 set { ViewState ["AnswerRequiredErrorMessage"] = value; }
98 public virtual int BorderPadding
100 get { return ViewState.GetInt ("BorderPadding", 1); }
104 throw new ArgumentOutOfRangeException ();
105 ViewState ["BorderPadding"] = value;
110 public virtual string GeneralFailureText
112 get { return ViewState.GetString ("GeneralFailureText", "Your attempt to retrieve your password was not successful. Please try again."); }
113 set { ViewState ["GeneralFailureText"] = value; }
116 public virtual string HelpPageIconUrl
118 get { return ViewState.GetString ("HelpPageIconUrl", String.Empty); }
119 set { ViewState ["HelpPageIconUrl"] = value; }
123 public virtual string HelpPageText
125 get { return ViewState.GetString ("HelpPageText", String.Empty); }
126 set { ViewState ["HelpPageText"] = value; }
129 public virtual string HelpPageUrl
131 get { return ViewState.GetString ("HelpPageUrl", String.Empty); }
132 set { ViewState ["HelpPageUrl"] = value; }
136 public MailDefinition MailDefinition
140 if (_mailDefinition == null) {
141 _mailDefinition = new MailDefinition ();
142 if (IsTrackingViewState)
143 ((IStateManager) _mailDefinition).TrackViewState ();
145 return _mailDefinition;
150 public virtual string MembershipProvider
152 get { return ViewState.GetString ("MembershipProvider", String.Empty); }
153 set { ViewState ["MembershipProvider"] = value; }
157 public virtual string Question
159 get { return ViewState.GetString ("Question", ""); }
160 private set { ViewState ["Question"] = value; }
164 public virtual string QuestionFailureText
166 get { return ViewState.GetString ("QuestionFailureText", "Your answer could not be verified. Please try again."); }
167 set { ViewState ["QuestionFailureText"] = value; }
171 public virtual string QuestionInstructionText
173 get { return ViewState.GetString ("QuestionInstructionText", "Answer the following question to receive your password."); }
174 set { ViewState ["QuestionInstructionText"] = value; }
178 public virtual string QuestionLabelText
180 get { return ViewState.GetString ("QuestionLabelText", "Question:"); }
181 set { ViewState ["QuestionLabelText"] = value; }
185 public virtual string QuestionTitleText
187 get { return ViewState.GetString ("QuestionTitleText", "Identity Confirmation"); }
188 set { ViewState ["QuestionTitleText"] = value; }
191 public virtual string SubmitButtonImageUrl
193 get { return ViewState.GetString ("SubmitButtonImageUrl", String.Empty); }
194 set { ViewState ["SubmitButtonImageUrl"] = value; }
198 public virtual string SubmitButtonText
200 get { return ViewState.GetString ("SubmitButtonText", "Submit"); }
201 set { ViewState ["SubmitButtonText"] = value; }
204 public virtual ButtonType SubmitButtonType
208 object o = ViewState ["SubmitButtonType"];
209 return (o == null) ? ButtonType.Button : (ButtonType) o;
213 if ((value < ButtonType.Button) || (value > ButtonType.Link))
214 throw new ArgumentOutOfRangeException ("SubmitButtonType");
215 ViewState ["SubmitButtonType"] = (int) value;
220 public virtual string SuccessPageUrl
222 get { return ViewState.GetString ("SuccessPageUrl", String.Empty); }
223 set { ViewState ["SuccessPageUrl"] = value; }
227 public virtual string SuccessText
229 get { return ViewState.GetString ("SuccessText", "Your password has been sent to you."); }
230 set { ViewState ["SuccessText"] = value; }
233 public virtual LoginTextLayout TextLayout
237 object o = ViewState ["TextLayout"];
238 return (o == null) ? LoginTextLayout.TextOnLeft : (LoginTextLayout) o;
242 if ((value < LoginTextLayout.TextOnLeft) || (value > LoginTextLayout.TextOnTop))
243 throw new ArgumentOutOfRangeException ("TextLayout");
244 ViewState ["TextLayout"] = (int) value;
249 public virtual string UserName
251 get { return _username != null ? _username : ""; }
252 set { _username = value; }
256 public virtual string UserNameFailureText
258 get { return ViewState.GetString ("UserNameFailureText", "We were unable to access your information. Please try again."); }
259 set { ViewState ["UserNameFailureText"] = value; }
263 public virtual string UserNameInstructionText
265 get { return ViewState.GetString ("UserNameInstructionText", "Enter your User Name to receive your password."); }
266 set { ViewState ["UserNameInstructionText"] = value; }
270 public virtual string UserNameLabelText
272 get { return ViewState.GetString ("UserNameLabelText", "User Name:"); }
273 set { ViewState ["UserNameLabelText"] = value; }
277 public virtual string UserNameRequiredErrorMessage
279 get { return ViewState.GetString ("UserNameRequiredErrorMessage", "User Name is required."); }
280 set { ViewState ["UserNameRequiredErrorMessage"] = value; }
284 public virtual string UserNameTitleText
286 get { return ViewState.GetString ("UserNameTitleText", "Forgot Your Password?"); }
287 set { ViewState ["UserNameTitleText"] = value; }
290 [TemplateContainer (typeof (PasswordRecovery))]
291 public virtual ITemplate QuestionTemplate
293 get { return _questionTemplate; }
294 set { _questionTemplate = value; }
297 public Control QuestionTemplateContainer
301 if (_questionTemplateContainer == null)
302 _questionTemplateContainer = new QuestionContainer (this);
303 return _questionTemplateContainer;
307 [TemplateContainer (typeof (PasswordRecovery))]
308 public virtual ITemplate SuccessTemplate
310 get { return _successTemplate; }
311 set { _successTemplate = value; }
314 public Control SuccessTemplateContainer
318 if (_successTemplateContainer == null)
319 _successTemplateContainer = new SuccessContainer (this);
320 return _successTemplateContainer;
324 [TemplateContainer (typeof (PasswordRecovery))]
325 public virtual ITemplate UserNameTemplate
327 get { return _userNameTemplate; }
328 set { _userNameTemplate = value; }
331 public Control UserNameTemplateContainer
335 if (_userNameTemplateContainer == null)
336 _userNameTemplateContainer = new UserNameContainer (this);
337 return _userNameTemplateContainer;
341 public TableItemStyle FailureTextStyle
345 if (_failureTextStyle == null) {
346 _failureTextStyle = new TableItemStyle ();
347 if (IsTrackingViewState)
348 _failureTextStyle.TrackViewState ();
350 return _failureTextStyle;
354 public TableItemStyle HyperLinkStyle
358 if (_hyperLinkStyle == null) {
359 _hyperLinkStyle = new TableItemStyle ();
360 if (IsTrackingViewState)
361 _hyperLinkStyle.TrackViewState ();
363 return _hyperLinkStyle;
367 public TableItemStyle InstructionTextStyle
371 if (_instructionTextStyle == null) {
372 _instructionTextStyle = new TableItemStyle ();
373 if (IsTrackingViewState)
374 _instructionTextStyle.TrackViewState ();
376 return _instructionTextStyle;
380 public TableItemStyle LabelStyle
384 if (_labelStyle == null) {
385 _labelStyle = new TableItemStyle ();
386 if (IsTrackingViewState)
387 _labelStyle.TrackViewState ();
393 public Style SubmitButtonStyle
397 if (_submitButtonStyle == null) {
398 _submitButtonStyle = new TableItemStyle ();
399 if (IsTrackingViewState)
400 _submitButtonStyle.TrackViewState ();
402 return _submitButtonStyle;
406 public TableItemStyle SuccessTextStyle
410 if (_successTextStyle == null) {
411 _successTextStyle = new TableItemStyle ();
412 if (IsTrackingViewState)
413 _successTextStyle.TrackViewState ();
415 return _successTextStyle;
419 public Style TextBoxStyle
423 if (_textBoxStyle == null) {
424 _textBoxStyle = new TableItemStyle ();
425 if (IsTrackingViewState)
426 _textBoxStyle.TrackViewState ();
428 return _textBoxStyle;
432 public TableItemStyle TitleTextStyle
436 if (_titleTextStyle == null) {
437 _titleTextStyle = new TableItemStyle ();
438 if (IsTrackingViewState)
439 _titleTextStyle.TrackViewState ();
441 return _titleTextStyle;
445 public Style ValidatorTextStyle
449 if (_validatorTextStyle == null) {
450 _validatorTextStyle = new TableItemStyle ();
451 if (IsTrackingViewState)
452 _validatorTextStyle.TrackViewState ();
454 return _validatorTextStyle;
458 #region Protected Properties
460 protected override HtmlTextWriterTag TagKey
462 get { return HtmlTextWriterTag.Table; }
465 internal virtual MembershipProvider MembershipProviderInternal
469 if (_provider == null)
470 InitMemberShipProvider ();
480 public event EventHandler AnswerLookupError;
481 public event MailMessageEventHandler SendingMail;
482 public event SendMailErrorEventHandler SendMailError;
483 public event EventHandler UserLookupError;
484 public event LoginCancelEventHandler VerifyingAnswer;
485 public event LoginCancelEventHandler VerifyingUser;
489 protected internal override void CreateChildControls ()
491 ITemplate userNameTemplate = UserNameTemplate;
492 if (userNameTemplate == null)
493 userNameTemplate = new UserNameDefaultTemplate (this);
494 ((UserNameContainer) UserNameTemplateContainer).InstantiateTemplate (userNameTemplate);
496 ITemplate questionTemplate = QuestionTemplate;
497 if (questionTemplate == null)
498 questionTemplate = new QuestionDefaultTemplate (this);
499 ((QuestionContainer) QuestionTemplateContainer).InstantiateTemplate (questionTemplate);
501 ITemplate successTemplate = SuccessTemplate;
502 if (successTemplate == null)
503 successTemplate = new SuccessDefaultTemplate (this);
504 ((SuccessContainer) SuccessTemplateContainer).InstantiateTemplate (successTemplate);
506 Controls.AddAt (0, UserNameTemplateContainer);
507 Controls.AddAt (1, QuestionTemplateContainer);
508 Controls.AddAt (2, SuccessTemplateContainer);
510 IEditableTextControl editable;
512 editable = ((UserNameContainer) UserNameTemplateContainer).UserNameTextBox;
513 if (editable != null)
514 editable.TextChanged += new EventHandler (UserName_TextChanged);
516 editable = ((QuestionContainer) QuestionTemplateContainer).AnswerTextBox;
517 if (editable != null)
518 editable.TextChanged += new EventHandler (Answer_TextChanged);
521 #region Protected methods
523 protected internal override void Render (HtmlTextWriter writer)
525 ((QuestionContainer) QuestionTemplateContainer).UpdateChildControls ();
527 for (int i = 0; i < Controls.Count; i++)
528 if (Controls [i].Visible)
529 Controls [i].Render (writer);
532 protected internal override void LoadControlState (object savedState)
534 if (savedState == null) return;
535 object [] state = (object []) savedState;
536 base.LoadControlState (state [0]);
538 _currentStep = (PasswordReciveryStep) state [1];
539 _username = (string) state [2];
542 protected internal override object SaveControlState ()
544 object state = base.SaveControlState ();
545 return new object [] { state, _currentStep, _username };
548 protected override void TrackViewState ()
550 base.TrackViewState ();
552 if (_failureTextStyle != null)
553 _failureTextStyle.TrackViewState ();
555 if (_hyperLinkStyle != null)
556 _hyperLinkStyle.TrackViewState ();
558 if (_instructionTextStyle != null)
559 _instructionTextStyle.TrackViewState ();
561 if (_labelStyle != null)
562 _labelStyle.TrackViewState ();
564 if (_submitButtonStyle != null)
565 _submitButtonStyle.TrackViewState ();
567 if (_successTextStyle != null)
568 _successTextStyle.TrackViewState ();
570 if (_textBoxStyle != null)
571 _textBoxStyle.TrackViewState ();
573 if (_titleTextStyle != null)
574 _titleTextStyle.TrackViewState ();
576 if (_validatorTextStyle != null)
577 _validatorTextStyle.TrackViewState ();
579 if (_mailDefinition != null)
580 ((IStateManager) _mailDefinition).TrackViewState ();
583 protected override void LoadViewState (object savedState)
585 if (savedState == null)
588 object [] states = (object []) savedState;
589 base.LoadViewState (states [0]);
591 if (states [1] != null)
592 FailureTextStyle.LoadViewState (states [1]);
594 if (states [2] != null)
595 HyperLinkStyle.LoadViewState (states [2]);
597 if (states [3] != null)
598 InstructionTextStyle.LoadViewState (states [3]);
600 if (states [4] != null)
601 LabelStyle.LoadViewState (states [4]);
603 if (states [5] != null)
604 SubmitButtonStyle.LoadViewState (states [5]);
606 if (states [6] != null)
607 SuccessTextStyle.LoadViewState (states [6]);
609 if (states [7] != null)
610 TextBoxStyle.LoadViewState (states [7]);
612 if (states [8] != null)
613 TitleTextStyle.LoadViewState (states [8]);
615 if (states [9] != null)
616 ValidatorTextStyle.LoadViewState (states [9]);
618 if (states [10] != null)
619 ((IStateManager) MailDefinition).LoadViewState (states [10]);
622 protected override object SaveViewState ()
624 object [] states = new object [11];
625 states [0] = base.SaveViewState ();
627 if (_failureTextStyle != null)
628 states [1] = _failureTextStyle.SaveViewState ();
630 if (_hyperLinkStyle != null)
631 states [2] = _hyperLinkStyle.SaveViewState ();
633 if (_instructionTextStyle != null)
634 states [3] = _instructionTextStyle.SaveViewState ();
636 if (_labelStyle != null)
637 states [4] = _labelStyle.SaveViewState ();
639 if (_submitButtonStyle != null)
640 states [5] = _submitButtonStyle.SaveViewState ();
642 if (_successTextStyle != null)
643 states [6] = _successTextStyle.SaveViewState ();
645 if (_textBoxStyle != null)
646 states [7] = _textBoxStyle.SaveViewState ();
648 if (_titleTextStyle != null)
649 states [8] = _titleTextStyle.SaveViewState ();
651 if (_validatorTextStyle != null)
652 states [9] = _validatorTextStyle.SaveViewState ();
654 if (_mailDefinition != null)
655 states [10] = ((IStateManager) _mailDefinition).SaveViewState ();
657 for (int i = 0; i < states.Length; i++) {
658 if (states [i] != null)
667 void ProcessCommand (CommandEventArgs args)
672 switch (_currentStep) {
673 case PasswordReciveryStep.StepUserName:
676 case PasswordReciveryStep.StepAnswer:
677 ProcessUserAnswer ();
683 void ProcessUserName ()
685 LoginCancelEventArgs args = new LoginCancelEventArgs ();
686 OnVerifyingUser (args);
690 MembershipUser user = MembershipProviderInternal.GetUser (UserName, false);
692 OnUserLookupError (EventArgs.Empty);
693 ((UserNameContainer) UserNameTemplateContainer).FailureTextLiteral.Text = UserNameFailureText;
697 Question = user.PasswordQuestion;
698 _currentStep = PasswordReciveryStep.StepAnswer;
702 private void ProcessUserAnswer ()
704 LoginCancelEventArgs args = new LoginCancelEventArgs ();
705 OnVerifyingAnswer (args);
709 MembershipUser user = MembershipProviderInternal.GetUser (UserName, false);
710 if (user == null || string.IsNullOrEmpty (user.Email)) {
711 ((QuestionContainer) QuestionTemplateContainer).FailureTextLiteral.Text = GeneralFailureText;
715 string newPassword = "";
717 if (MembershipProviderInternal.EnablePasswordRetrieval) {
718 newPassword = MembershipProviderInternal.GetPassword (UserName, Answer);
720 else if (MembershipProviderInternal.EnablePasswordReset) {
721 newPassword = MembershipProviderInternal.ResetPassword (UserName, Answer);
724 throw new HttpException ("Membership provider does not support password retrieval or reset.");
726 catch (MembershipPasswordException) {
727 OnAnswerLookupError (EventArgs.Empty);
728 ((QuestionContainer) QuestionTemplateContainer).FailureTextLiteral.Text = QuestionFailureText;
732 SendPasswordByMail (UserName, newPassword);
734 _currentStep = PasswordReciveryStep.StepSuccess;
738 private void InitMemberShipProvider ()
740 string mp = MembershipProvider;
741 _provider = (mp.Length == 0) ? _provider = Membership.Provider : Membership.Providers [mp];
742 if (_provider == null)
743 throw new HttpException (Locale.GetText ("No provider named '{0}' could be found.", mp));
746 private void SendPasswordByMail (string username, string password)
748 MembershipUser user = MembershipProviderInternal.GetUser (UserName, false);
752 string messageText = "Please return to the site and log in using the following information.\n" +
753 "User Name: <%USERNAME%>\nPassword: <%PASSWORD%>";
755 ListDictionary dictionary = new ListDictionary ();
756 dictionary.Add ("<%USERNAME%>", username);
757 dictionary.Add ("<%PASSWORD%>", password);
759 MailMessage message = null;
761 if (MailDefinition.BodyFileName.Length == 0)
762 message = MailDefinition.CreateMailMessage (user.Email, dictionary, messageText, this);
764 message = MailDefinition.CreateMailMessage (user.Email, dictionary, this);
766 if (string.IsNullOrEmpty (message.Subject))
767 message.Subject = "Password";
769 MailMessageEventArgs args = new MailMessageEventArgs (message);
770 OnSendingMail (args);
772 SmtpClient smtpClient = new SmtpClient ();
774 smtpClient.Send (message);
776 catch (Exception e) {
777 SendMailErrorEventArgs mailArgs = new SendMailErrorEventArgs (e);
778 OnSendMailError (mailArgs);
779 if (!mailArgs.Handled)
784 #region Event handlers
786 protected virtual void OnAnswerLookupError (EventArgs e)
790 protected override bool OnBubbleEvent (object source, EventArgs e)
792 CommandEventArgs args = e as CommandEventArgs;
793 if (e != null && args.CommandName == SubmitButtonCommandName) {
794 ProcessCommand (args);
797 return base.OnBubbleEvent (source, e);
800 protected internal override void OnInit (EventArgs e)
802 Page.RegisterRequiresControlState (this);
806 protected internal override void OnPreRender (EventArgs e)
808 UserNameTemplateContainer.Visible = false;
809 QuestionTemplateContainer.Visible = false;
810 SuccessTemplateContainer.Visible = false;
812 switch (_currentStep) {
813 case PasswordReciveryStep.StepUserName:
814 UserNameTemplateContainer.Visible = true;
816 case PasswordReciveryStep.StepAnswer:
817 QuestionTemplateContainer.Visible = true;
819 case PasswordReciveryStep.StepSuccess:
820 SuccessTemplateContainer.Visible = true;
824 base.OnPreRender (e);
827 protected virtual void OnSendingMail (MailMessageEventArgs e)
829 if (SendingMail != null)
830 SendingMail (this, e);
833 protected virtual void OnSendMailError (SendMailErrorEventArgs e)
835 if (SendMailError != null)
836 SendMailError (this, e);
839 protected virtual void OnUserLookupError (EventArgs e)
841 if (UserLookupError != null)
842 UserLookupError (this, e);
845 protected virtual void OnVerifyingAnswer (LoginCancelEventArgs e)
847 if (VerifyingAnswer != null)
848 VerifyingAnswer (this, e);
851 protected virtual void OnVerifyingUser (LoginCancelEventArgs e)
853 if (VerifyingUser != null)
854 VerifyingUser (this, e);
859 #region Private Event Handlers
861 private void UserName_TextChanged (object sender, EventArgs e)
863 UserName = ((ITextControl) sender).Text;
866 private void Answer_TextChanged (object sender, EventArgs e)
868 _answer = ((ITextControl) sender).Text;
873 [MonoTODO ("Not implemented")]
874 protected override void SetDesignModeState (IDictionary data)
876 throw new NotImplementedException ();
880 abstract class BasePasswordRecoveryContainer : Table, INamingContainer
882 protected readonly PasswordRecovery _owner = null;
883 TableCell _containerCell = null;
885 public BasePasswordRecoveryContainer (PasswordRecovery owner)
891 public void InstantiateTemplate (ITemplate template)
893 template.InstantiateIn (_containerCell);
896 private void InitTable ()
898 Attributes.Add ("ID", _owner.ID);
901 CellPadding = _owner.BorderPadding;
903 _containerCell = new TableCell ();
905 TableRow row = new TableRow ();
906 row.Cells.Add (_containerCell);
910 protected internal override void OnPreRender (EventArgs e)
912 ApplyStyle (_owner.ControlStyle);
913 base.OnPreRender (e);
916 public abstract void UpdateChildControls();
920 sealed class QuestionContainer : BasePasswordRecoveryContainer
922 public QuestionContainer (PasswordRecovery owner)
927 public IEditableTextControl AnswerTextBox
931 Control c = FindControl ("Answer");
933 throw new HttpException ("QuestionTemplate does not contain an IEditableTextControl with ID Answer for the username.");
934 return c as IEditableTextControl;
938 public Literal UserNameLiteral
940 get { return FindControl ("UserName") as Literal; }
942 public Literal QuestionLiteral
944 get { return FindControl ("Question") as Literal; }
946 public Literal FailureTextLiteral
948 get { return FindControl ("FailureText") as Literal; }
951 public override void UpdateChildControls ()
953 if (UserNameLiteral != null)
954 UserNameLiteral.Text = _owner.UserName;
956 if (QuestionLiteral != null)
957 QuestionLiteral.Text = _owner.Question;
961 sealed class SuccessContainer : BasePasswordRecoveryContainer
963 public SuccessContainer (PasswordRecovery owner)
968 public override void UpdateChildControls ()
973 sealed class UserNameContainer : BasePasswordRecoveryContainer
975 public UserNameContainer (PasswordRecovery owner)
980 public IEditableTextControl UserNameTextBox
984 Control c = FindControl ("UserName");
986 throw new HttpException ("UserNameTemplate does not contain an IEditableTextControl with ID UserName for the username.");
987 return c as IEditableTextControl;
991 public ITextControl FailureTextLiteral
993 get { return FindControl ("FailureText") as ITextControl; }
996 public override void UpdateChildControls ()
1003 public static TableRow CreateRow(Control c1, Control c2, Style s1, Style s2, bool twoCells)
1005 TableRow row = new TableRow ();
1006 TableCell cell1 = new TableCell ();
1008 cell1.Controls.Add (c1);
1010 cell1.ApplyStyle (s1);
1012 row.Cells.Add (cell1);
1015 TableCell cell2 = new TableCell ();
1016 cell2.Controls.Add (c2);
1019 cell2.ApplyStyle (s2);
1021 row.Cells.Add (cell2);
1023 cell1.HorizontalAlign = HorizontalAlign.Right;
1024 cell2.HorizontalAlign = HorizontalAlign.Left;
1027 cell1.HorizontalAlign = HorizontalAlign.Center;
1029 cell1.ColumnSpan = 2;
1035 public static TableRow CreateHelpRow (string pageUrl, string linkText, string linkIcon, Style linkStyle, bool twoCells)
1037 TableRow row = new TableRow ();
1038 TableCell cell1 = new TableCell ();
1040 if (linkIcon.Length > 0) {
1041 Image img = new Image ();
1042 img.ImageUrl = linkIcon;
1043 cell1.Controls.Add (img);
1045 if (linkText.Length > 0) {
1046 HyperLink link = new HyperLink ();
1047 link.NavigateUrl = pageUrl;
1048 link.Text = linkText;
1049 link.ControlStyle.CopyTextStylesFrom (linkStyle);
1050 cell1.Controls.Add (link);
1054 cell1.ColumnSpan = 2;
1056 row.ControlStyle.CopyFrom (linkStyle);
1057 row.Cells.Add (cell1);
1062 sealed class UserNameDefaultTemplate : ITemplate
1064 readonly PasswordRecovery _owner = null;
1066 public UserNameDefaultTemplate (PasswordRecovery _owner)
1068 this._owner = _owner;
1071 public void InstantiateIn (Control container)
1073 Table table = new Table ();
1074 table.CellPadding = 0;
1076 bool twoCells = _owner.TextLayout == LoginTextLayout.TextOnLeft;
1080 TemplateUtils.CreateRow (new LiteralControl (_owner.UserNameTitleText), null, _owner.TitleTextStyle, null, twoCells));
1084 TemplateUtils.CreateRow (new LiteralControl (_owner.UserNameInstructionText), null, _owner.InstructionTextStyle, null, twoCells));
1087 TextBox UserNameTextBox = new TextBox ();
1088 UserNameTextBox.ID = "UserName";
1089 UserNameTextBox.Text = _owner.UserName;
1090 UserNameTextBox.ApplyStyle (_owner.TextBoxStyle);
1092 Label UserNameLabel = new Label ();
1093 UserNameLabel.ID = "UserNameLabel";
1094 UserNameLabel.AssociatedControlID = "UserName";
1095 UserNameLabel.Text = _owner.UserNameLabelText;
1096 UserNameLabel.ApplyStyle (_owner.LabelStyle);
1098 RequiredFieldValidator UserNameRequired = new RequiredFieldValidator ();
1099 UserNameRequired.ID = "UserNameRequired";
1100 UserNameRequired.ControlToValidate = "UserName";
1101 UserNameRequired.ErrorMessage = _owner.UserNameRequiredErrorMessage;
1102 UserNameRequired.ToolTip = _owner.UserNameRequiredErrorMessage;
1103 UserNameRequired.Text = "*";
1104 UserNameRequired.ValidationGroup = _owner.ID;
1105 UserNameRequired.ApplyStyle (_owner.ValidatorTextStyle);
1108 TableRow row = TemplateUtils.CreateRow (UserNameLabel, UserNameTextBox, null, null, twoCells);
1109 row.Cells [1].Controls.Add (UserNameRequired);
1110 table.Rows.Add (row);
1113 table.Rows.Add (TemplateUtils.CreateRow (UserNameLabel, null, null, null, twoCells));
1114 TableRow row = TemplateUtils.CreateRow (UserNameTextBox, null, null, null, twoCells);
1115 row.Cells [0].Controls.Add (UserNameRequired);
1116 table.Rows.Add (row);
1120 Literal FailureText = new Literal ();
1121 FailureText.ID = "FailureText";
1122 if (_owner.FailureTextStyle.ForeColor.IsEmpty)
1123 _owner.FailureTextStyle.ForeColor = System.Drawing.Color.Red;
1124 table.Rows.Add (TemplateUtils.CreateRow (FailureText, null, _owner.FailureTextStyle, null, twoCells));
1127 WebControl SubmitButton = null;
1128 switch (_owner.SubmitButtonType) {
1129 case ButtonType.Button:
1130 SubmitButton = new Button ();
1132 case ButtonType.Image:
1133 SubmitButton = new ImageButton ();
1135 case ButtonType.Link:
1136 SubmitButton = new LinkButton ();
1140 SubmitButton.ID = "SubmitButton";
1141 SubmitButton.ApplyStyle (_owner.SubmitButtonStyle);
1142 ((IButtonControl) SubmitButton).CommandName = PasswordRecovery.SubmitButtonCommandName;
1143 ((IButtonControl) SubmitButton).Text = _owner.SubmitButtonText;
1144 ((IButtonControl) SubmitButton).ValidationGroup = _owner.ID;
1146 TableRow buttonRow = TemplateUtils.CreateRow (SubmitButton, null, null, null, twoCells);
1147 buttonRow.Cells [0].HorizontalAlign = HorizontalAlign.Right;
1148 table.Rows.Add (buttonRow);
1152 TemplateUtils.CreateHelpRow (
1153 _owner.HelpPageUrl, _owner.HelpPageText, _owner.HelpPageIconUrl, _owner.HyperLinkStyle, twoCells));
1155 container.Controls.Add (table);
1159 sealed class QuestionDefaultTemplate : ITemplate
1161 readonly PasswordRecovery _owner = null;
1163 public QuestionDefaultTemplate (PasswordRecovery _owner)
1165 this._owner = _owner;
1168 public void InstantiateIn (Control container)
1170 Table table = new Table ();
1171 table.CellPadding = 0;
1173 bool twoCells = _owner.TextLayout == LoginTextLayout.TextOnLeft;
1177 TemplateUtils.CreateRow (new LiteralControl (_owner.QuestionTitleText), null, _owner.TitleTextStyle, null, twoCells));
1181 TemplateUtils.CreateRow (new LiteralControl (_owner.QuestionInstructionText), null, _owner.InstructionTextStyle, null, twoCells));
1184 Literal UserNameLiteral = new Literal ();
1185 UserNameLiteral.ID = "UserName";
1188 TemplateUtils.CreateRow (new LiteralControl (_owner.UserNameLabelText), UserNameLiteral, _owner.LabelStyle, _owner.LabelStyle, twoCells));
1191 Literal QuestionLiteral = new Literal ();
1192 QuestionLiteral.ID = "Question";
1195 TemplateUtils.CreateRow (new LiteralControl (_owner.QuestionLabelText), QuestionLiteral, _owner.LabelStyle, _owner.LabelStyle, twoCells));
1198 TextBox AnswerTextBox = new TextBox ();
1199 AnswerTextBox.ID = "Answer";
1200 AnswerTextBox.ApplyStyle (_owner.TextBoxStyle);
1202 Label AnswerLabel = new Label ();
1203 AnswerLabel.ID = "AnswerLabel";
1204 AnswerLabel.AssociatedControlID = "Answer";
1205 AnswerLabel.Text = _owner.AnswerLabelText;
1206 AnswerLabel.ApplyStyle (_owner.LabelStyle);
1208 RequiredFieldValidator AnswerRequired = new RequiredFieldValidator ();
1209 AnswerRequired.ID = "AnswerRequired";
1210 AnswerRequired.ControlToValidate = "Answer";
1211 AnswerRequired.ErrorMessage = _owner.AnswerRequiredErrorMessage;
1212 AnswerRequired.ToolTip = _owner.AnswerRequiredErrorMessage;
1213 AnswerRequired.Text = "*";
1214 AnswerRequired.ValidationGroup = _owner.ID;
1215 AnswerRequired.ApplyStyle (_owner.ValidatorTextStyle);
1218 TableRow row = TemplateUtils.CreateRow (AnswerLabel, AnswerTextBox, null, null, twoCells);
1219 row.Cells [1].Controls.Add (AnswerRequired);
1220 table.Rows.Add (row);
1223 table.Rows.Add (TemplateUtils.CreateRow (AnswerLabel, null, null, null, twoCells));
1224 TableRow row = TemplateUtils.CreateRow (AnswerTextBox, null, null, null, twoCells);
1225 row.Cells [0].Controls.Add (AnswerRequired);
1226 table.Rows.Add (row);
1230 Literal FailureText = new Literal ();
1231 FailureText.ID = "FailureText";
1232 if (_owner.FailureTextStyle.ForeColor.IsEmpty)
1233 _owner.FailureTextStyle.ForeColor = System.Drawing.Color.Red;
1234 table.Rows.Add (TemplateUtils.CreateRow (FailureText, null, _owner.FailureTextStyle, null, twoCells));
1237 WebControl SubmitButton = null;
1238 switch (_owner.SubmitButtonType) {
1239 case ButtonType.Button:
1240 SubmitButton = new Button ();
1242 case ButtonType.Image:
1243 SubmitButton = new ImageButton ();
1245 case ButtonType.Link:
1246 SubmitButton = new LinkButton ();
1250 SubmitButton.ID = "SubmitButton";
1251 SubmitButton.ApplyStyle (_owner.SubmitButtonStyle);
1252 ((IButtonControl) SubmitButton).CommandName = PasswordRecovery.SubmitButtonCommandName;
1253 ((IButtonControl) SubmitButton).Text = _owner.SubmitButtonText;
1254 ((IButtonControl) SubmitButton).ValidationGroup = _owner.ID;
1256 TableRow buttonRow = TemplateUtils.CreateRow (SubmitButton, null, null, null, twoCells);
1257 buttonRow.Cells [0].HorizontalAlign = HorizontalAlign.Right;
1258 table.Rows.Add (buttonRow);
1262 TemplateUtils.CreateHelpRow (
1263 _owner.HelpPageUrl, _owner.HelpPageText, _owner.HelpPageIconUrl, _owner.HyperLinkStyle, twoCells));
1265 container.Controls.Add (table);
1269 sealed class SuccessDefaultTemplate : ITemplate
1271 readonly PasswordRecovery _owner = null;
1273 public SuccessDefaultTemplate (PasswordRecovery _owner)
1275 this._owner = _owner;
1278 public void InstantiateIn (Control container)
1280 Table table = new Table ();
1281 table.CellPadding = 0;
1283 bool twoCells = _owner.TextLayout == LoginTextLayout.TextOnLeft;
1287 TemplateUtils.CreateRow (new LiteralControl (_owner.SuccessText), null, _owner.SuccessTextStyle, null, twoCells));
1289 container.Controls.Add (table);
1293 private enum PasswordReciveryStep