2 // System.Web.UI.WebControls.Login class
5 // Sebastien Pouliot <sebastien@ximian.com>
6 // Konstantin Triger <kostat@mainsoft.com>
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.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.
31 using System.Collections;
32 using System.Globalization;
33 using System.ComponentModel;
34 using System.Security.Permissions;
35 using System.Web.Security;
36 using System.Web.Util;
38 namespace System.Web.UI.WebControls {
41 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
42 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
45 [DefaultEvent ("Authenticate")]
46 [Designer ("System.Web.UI.Design.WebControls.LoginDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
47 public class Login : CompositeControl
52 #region LoginContainer
53 // TODO: This class should probably be folded into a generic one with BaseChangePasswordContainer
54 sealed class LoginContainer : Control
56 readonly Login _owner;
58 bool renderOuterTable;
61 TableCell _containerCell;
63 public LoginContainer (Login owner)
67 renderOuterTable = _owner.RenderOuterTable;
74 public override string ID {
83 public override string ClientID {
85 return _owner.ClientID;
89 public void InstantiateTemplate (ITemplate template)
92 if (!renderOuterTable)
93 template.InstantiateIn (this);
96 template.InstantiateIn (_containerCell);
101 _table = new Table ();
102 _containerCell = new TableCell ();
104 TableRow row = new TableRow ();
105 row.Cells.Add (_containerCell);
106 _table.Rows.Add (row);
108 Controls.AddAt (0, _table);
111 protected internal override void Render (HtmlTextWriter writer)
113 if (_table != null) {
114 _table.CellSpacing = 0;
115 _table.CellPadding = _owner.BorderPadding;
116 _table.ApplyStyle (_owner.ControlStyle);
117 _table.Attributes.CopyFrom (_owner.Attributes);
120 base.Render (writer);
123 public Control UserNameTextBox {
125 return FindControl("UserName");
129 public Control PasswordTextBox {
131 return FindControl("Password");
135 public Control RememberMeCheckBox {
137 return FindControl("RememberMe");
141 public ITextControl FailureTextLiteral
144 return FindControl ("FailureText") as ITextControl;
151 #region LoginTemplate
153 sealed class LoginTemplate : WebControl, ITemplate
155 readonly Login _login;
157 public LoginTemplate (Login login)
162 void ITemplate.InstantiateIn (Control container)
165 LiteralControl TitleText = new LiteralControl (_login.TitleText);
168 LiteralControl InstructionText = new LiteralControl (_login.InstructionText);
171 TextBox UserName = new TextBox ();
172 UserName.ID = "UserName";
173 UserName.Text = _login.UserName;
174 _login.RegisterApplyStyle (UserName, _login.TextBoxStyle);
176 Label UserNameLabel = new Label ();
177 UserNameLabel.ID = "UserNameLabel";
178 UserNameLabel.AssociatedControlID = "UserName";
179 UserNameLabel.Text = _login.UserNameLabelText;
181 RequiredFieldValidator UserNameRequired = new RequiredFieldValidator ();
182 UserNameRequired.ID = "UserNameRequired";
183 UserNameRequired.ControlToValidate = "UserName";
184 UserNameRequired.ErrorMessage = _login.UserNameRequiredErrorMessage;
185 UserNameRequired.ToolTip = _login.UserNameRequiredErrorMessage;
186 UserNameRequired.Text = "*";
187 UserNameRequired.ValidationGroup = _login.ID;
188 _login.RegisterApplyStyle (UserNameRequired, _login.ValidatorTextStyle);
191 TextBox Password = new TextBox ();
192 Password.ID = "Password";
193 Password.TextMode = TextBoxMode.Password;
194 _login.RegisterApplyStyle (Password, _login.TextBoxStyle);
196 Label PasswordLabel = new Label ();
197 PasswordLabel.ID = "PasswordLabel";
198 PasswordLabel.AssociatedControlID = "PasswordLabel";
199 PasswordLabel.Text = _login.PasswordLabelText;
201 RequiredFieldValidator PasswordRequired = new RequiredFieldValidator ();
202 PasswordRequired.ID = "PasswordRequired";
203 PasswordRequired.ControlToValidate = "Password";
204 PasswordRequired.ErrorMessage = _login.PasswordRequiredErrorMessage;
205 PasswordRequired.ToolTip = _login.PasswordRequiredErrorMessage;
206 PasswordRequired.Text = "*";
207 PasswordRequired.ValidationGroup = _login.ID;
208 _login.RegisterApplyStyle (PasswordRequired, _login.ValidatorTextStyle);
210 bool useRememberMe = _login != null ? _login.DisplayRememberMe : true;
214 RememberMe = new CheckBox ();
215 RememberMe.ID = "RememberMe";
216 RememberMe.Checked = _login.RememberMeSet;
217 RememberMe.Text = _login.RememberMeText;
218 _login.RegisterApplyStyle (RememberMe, _login.CheckBoxStyle);
223 Literal FailureText = new Literal ();
224 FailureText.ID = "FailureText";
225 FailureText.EnableViewState = false;
228 WebControl LoginButton = null;
229 switch (_login.LoginButtonType) {
230 case ButtonType.Button:
231 LoginButton = new Button ();
232 LoginButton.ID = "LoginButton";
234 case ButtonType.Link:
235 LoginButton = new LinkButton ();
236 LoginButton.ID = "LoginLinkButton";
238 case ButtonType.Image:
239 LoginButton = new ImageButton ();
240 LoginButton.ID = "LoginImageButton";
243 _login.RegisterApplyStyle (LoginButton, _login.LoginButtonStyle);
244 LoginButton.ID = "LoginButton";
245 ((IButtonControl) LoginButton).Text = _login.LoginButtonText;
246 ((IButtonControl) LoginButton).CommandName = Login.LoginButtonCommandName;
247 ((IButtonControl) LoginButton).Command += new CommandEventHandler (_login.LoginClick);
248 ((IButtonControl) LoginButton).ValidationGroup = _login.ID;
250 // Create layout table
251 Table table = new Table ();
252 table.CellPadding = 0;
257 CreateCell (TitleText, null, _login.TitleTextStyle, HorizontalAlign.Center)));
260 if (_login.InstructionText.Length > 0) {
263 CreateCell (InstructionText, null, _login.instructionTextStyle, HorizontalAlign.Center)));
266 if (_login.Orientation == Orientation.Horizontal) {
268 TableRow row1 = new TableRow ();
269 TableRow row2 = new TableRow ();
270 if (_login.TextLayout == LoginTextLayout.TextOnTop)
271 row1.Cells.Add (CreateCell (UserNameLabel, null, _login.LabelStyle));
273 row2.Cells.Add (CreateCell (UserNameLabel, null, _login.LabelStyle));
274 row2.Cells.Add (CreateCell (UserName, UserNameRequired, null));
275 if (_login.TextLayout == LoginTextLayout.TextOnTop)
276 row1.Cells.Add (CreateCell (PasswordLabel, null, _login.LabelStyle));
278 row2.Cells.Add (CreateCell (PasswordLabel, null, _login.LabelStyle));
279 row2.Cells.Add (CreateCell (Password, PasswordRequired, null));
281 row2.Cells.Add (CreateCell (RememberMe, null, null));
282 row2.Cells.Add (CreateCell (LoginButton, null, null));
283 if (row1.Cells.Count > 0)
284 table.Rows.Add (row1);
285 table.Rows.Add (row2);
287 else { // Orientation.Vertical
288 if (_login.TextLayout == LoginTextLayout.TextOnLeft)
289 table.Rows.Add (CreateRow (UserNameLabel, UserName, UserNameRequired, _login.LabelStyle));
291 table.Rows.Add (CreateRow (UserNameLabel, null, null, _login.LabelStyle));
292 table.Rows.Add (CreateRow (null, UserName, UserNameRequired, null));
294 if (_login.TextLayout == LoginTextLayout.TextOnLeft)
295 table.Rows.Add (CreateRow (PasswordLabel, Password, PasswordRequired, _login.LabelStyle));
297 table.Rows.Add (CreateRow (PasswordLabel, null, null, _login.LabelStyle));
298 table.Rows.Add (CreateRow (null, Password, PasswordRequired, null));
301 table.Rows.Add (CreateRow (CreateCell (RememberMe, null, null)));
302 table.Rows.Add (CreateRow (CreateCell (LoginButton, null, null, HorizontalAlign.Right)));
306 if (_login.FailureTextStyle.ForeColor.IsEmpty)
307 _login.FailureTextStyle.ForeColor = System.Drawing.Color.Red;
311 CreateCell (FailureText, null, _login.FailureTextStyle)));
314 TableCell linksCell = new TableCell ();
315 _login.RegisterApplyStyle (linksCell, _login.HyperLinkStyle);
317 if (AddLink (_login.CreateUserUrl, _login.CreateUserText, _login.CreateUserIconUrl, linksCell, _login.HyperLinkStyle))
318 if (_login.Orientation == Orientation.Vertical)
319 linksCell.Controls.Add (new LiteralControl ("<br/>"));
321 linksCell.Controls.Add (new LiteralControl (" "));
323 if (AddLink (_login.PasswordRecoveryUrl, _login.PasswordRecoveryText, _login.PasswordRecoveryIconUrl, linksCell, _login.HyperLinkStyle))
324 if (_login.Orientation == Orientation.Vertical)
325 linksCell.Controls.Add (new LiteralControl ("<br/>"));
327 linksCell.Controls.Add (new LiteralControl (" "));
329 AddLink (_login.HelpPageUrl, _login.HelpPageText, _login.HelpPageIconUrl, linksCell, _login.HyperLinkStyle);
330 table.Rows.Add (CreateRow (linksCell));
332 FixTableColumnSpans (table);
333 container.Controls.Add (table);
336 TableRow CreateRow (TableCell cell)
338 TableRow row = new TableRow ();
339 row.Cells.Add (cell);
343 TableRow CreateRow (Control c0, Control c1, Control c2, Style s)
345 TableRow row = new TableRow ();
346 TableCell cell0 = new TableCell ();
347 TableCell cell1 = new TableCell ();
350 cell0.Controls.Add (c0);
351 row.Controls.Add (cell0);
355 cell0.ApplyStyle (s);
357 if ((c1 != null) && (c2 != null)) {
358 cell1.Controls.Add (c1);
359 cell1.Controls.Add (c2);
360 cell0.HorizontalAlign = HorizontalAlign.Right;
361 row.Controls.Add (cell1);
366 TableCell CreateCell (Control c0, Control c1, Style s, HorizontalAlign align)
368 TableCell cell = CreateCell (c0, c1, s);
369 cell.HorizontalAlign = align;
373 TableCell CreateCell (Control c0, Control c1, Style s)
375 TableCell cell = new TableCell ();
379 cell.Controls.Add (c0);
381 cell.Controls.Add (c1);
386 bool AddLink (string pageUrl, string linkText, string linkIcon, WebControl container, Style style)
389 if (linkIcon.Length > 0) {
390 Image img = new Image ();
391 img.ImageUrl = linkIcon;
392 container.Controls.Add (img);
395 if (linkText.Length > 0) {
396 HyperLink link = new HyperLink ();
397 link.NavigateUrl = pageUrl;
398 link.Text = linkText;
399 _login.RegisterApplyStyle (link, style);
400 container.Controls.Add (link);
406 void FixTableColumnSpans (Table table)
409 for (int row = 0; row < table.Rows.Count; row++) {
410 if (maxCols < table.Rows [row].Cells.Count)
411 maxCols = table.Rows [row].Cells.Count;
413 for (int row = 0; row < table.Rows.Count; row++) {
414 if (table.Rows [row].Cells.Count == 1 && maxCols > 1)
415 table.Rows [row].Cells [0].ColumnSpan = maxCols;
422 public static readonly string LoginButtonCommandName = "Login";
424 static readonly object authenticateEvent = new object ();
425 static readonly object loggedInEvent = new object ();
426 static readonly object loggingInEvent = new object ();
427 static readonly object loginErrorEvent = new object ();
429 TableItemStyle checkBoxStyle;
430 TableItemStyle failureTextStyle;
431 TableItemStyle hyperLinkStyle;
432 TableItemStyle instructionTextStyle;
433 TableItemStyle labelStyle;
434 Style logonButtonStyle;
436 TableItemStyle titleTextStyle;
437 Style validatorTextStyle;
438 ArrayList styles = new ArrayList ();
440 ITemplate layoutTemplate;
441 LoginContainer container;
445 bool renderOuterTable = true;
452 public virtual int BorderPadding {
454 object o = ViewState ["BorderPadding"];
455 return (o == null) ? 1 : (int) o;
459 throw new ArgumentOutOfRangeException ("BorderPadding", "< -1");
461 ViewState ["BorderPadding"] = value;
465 [DefaultValue (null)]
466 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
467 [NotifyParentProperty (true)]
468 [PersistenceMode (PersistenceMode.InnerProperty)]
469 public TableItemStyle CheckBoxStyle {
471 if (checkBoxStyle == null) {
472 checkBoxStyle = new TableItemStyle ();
473 if (IsTrackingViewState) {
474 (checkBoxStyle as IStateManager).TrackViewState ();
477 return checkBoxStyle;
482 [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
484 public virtual string CreateUserIconUrl {
486 object o = ViewState ["CreateUserIconUrl"];
487 return (o == null) ? String.Empty : (string) o;
491 ViewState.Remove ("CreateUserIconUrl");
493 ViewState ["CreateUserIconUrl"] = value;
499 public virtual string CreateUserText {
501 object o = ViewState ["CreateUserText"];
502 return (o == null) ? String.Empty : (string) o;
506 ViewState.Remove ("CreateUserText");
508 ViewState ["CreateUserText"] = value;
513 [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
515 public virtual string CreateUserUrl {
517 object o = ViewState ["CreateUserUrl"];
518 return (o == null) ? String.Empty : (string) o;
522 ViewState.Remove ("CreateUserUrl");
524 ViewState ["CreateUserUrl"] = value;
529 [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
532 public virtual string DestinationPageUrl {
534 object o = ViewState ["DestinationPageUrl"];
535 return (o == null) ? String.Empty : (string) o;
539 ViewState.Remove ("DestinationPageUrl");
541 ViewState ["DestinationPageUrl"] = value;
545 [DefaultValue (true)]
547 public virtual bool DisplayRememberMe {
549 object o = ViewState ["DisplayRememberMe"];
550 return (o == null) ? true : (bool) o;
553 ViewState ["DisplayRememberMe"] = value;
557 [DefaultValue (LoginFailureAction.Refresh)]
559 [MonoTODO ("RedirectToLoginPage not yet implemented in FormsAuthentication")]
560 public virtual LoginFailureAction FailureAction {
562 object o = ViewState ["FailureAction"];
563 return (o == null) ? LoginFailureAction.Refresh : (LoginFailureAction) o;
566 if ((value < LoginFailureAction.Refresh) || (value > LoginFailureAction.RedirectToLoginPage))
567 throw new ArgumentOutOfRangeException ("FailureAction");
568 ViewState ["FailureAction"] = (int) value;
573 public virtual string FailureText {
575 object o = ViewState ["FailureText"];
576 return (o == null) ? Locale.GetText ("Your login attempt was not successful. Please try again.") : (string) o;
580 ViewState.Remove ("FailureText");
582 ViewState ["FailureText"] = value;
586 [DefaultValue (null)]
587 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
588 [NotifyParentProperty (true)]
589 [PersistenceMode (PersistenceMode.InnerProperty)]
590 public TableItemStyle FailureTextStyle {
592 if (failureTextStyle == null) {
593 failureTextStyle = new TableItemStyle ();
594 if (IsTrackingViewState) {
595 (failureTextStyle as IStateManager).TrackViewState ();
598 return failureTextStyle;
603 [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
605 public virtual string HelpPageIconUrl {
607 object o = ViewState ["HelpPageIconUrl"];
608 return (o == null) ? String.Empty : (string) o;
612 ViewState.Remove ("HelpPageIconUrl");
614 ViewState ["HelpPageIconUrl"] = value;
620 public virtual string HelpPageText {
622 object o = ViewState ["HelpPageText"];
623 return (o == null) ? String.Empty : (string) o;
627 ViewState.Remove ("HelpPageText");
629 ViewState ["HelpPageText"] = value;
634 [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
636 public virtual string HelpPageUrl {
638 object o = ViewState ["HelpPageUrl"];
639 return (o == null) ? String.Empty : (string) o;
643 ViewState.Remove ("HelpPageUrl");
645 ViewState ["HelpPageUrl"] = value;
649 [DefaultValue (null)]
650 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
651 [NotifyParentProperty (true)]
652 [PersistenceMode (PersistenceMode.InnerProperty)]
653 public TableItemStyle HyperLinkStyle {
655 if (hyperLinkStyle == null) {
656 hyperLinkStyle = new TableItemStyle ();
657 if (IsTrackingViewState) {
658 (hyperLinkStyle as IStateManager).TrackViewState ();
661 return hyperLinkStyle;
667 public virtual string InstructionText {
669 object o = ViewState ["InstructionText"];
670 return (o == null) ? String.Empty : (string) o;
674 ViewState.Remove ("InstructionText");
676 ViewState ["InstructionText"] = value;
680 [DefaultValue (null)]
681 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
682 [NotifyParentProperty (true)]
683 [PersistenceMode (PersistenceMode.InnerProperty)]
684 public TableItemStyle InstructionTextStyle {
686 if (instructionTextStyle == null) {
687 instructionTextStyle = new TableItemStyle ();
688 if (IsTrackingViewState) {
689 (instructionTextStyle as IStateManager).TrackViewState ();
692 return instructionTextStyle;
696 [DefaultValue (null)]
697 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
698 [NotifyParentProperty (true)]
699 [PersistenceMode (PersistenceMode.InnerProperty)]
700 public TableItemStyle LabelStyle {
702 if (labelStyle == null) {
703 labelStyle = new TableItemStyle ();
704 if (IsTrackingViewState) {
705 (labelStyle as IStateManager).TrackViewState ();
713 [TemplateContainer (typeof (Login))]
714 [PersistenceMode (PersistenceMode.InnerProperty)]
715 public virtual ITemplate LayoutTemplate {
716 get { return layoutTemplate; }
717 set { layoutTemplate = value; }
721 [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
723 public virtual string LoginButtonImageUrl {
725 object o = ViewState ["LoginButtonImageUrl"];
726 return (o == null) ? String.Empty : (string) o;
730 ViewState.Remove ("LoginButtonImageUrl");
732 ViewState ["LoginButtonImageUrl"] = value;
736 [DefaultValue (null)]
737 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
738 [NotifyParentProperty (true)]
739 [PersistenceMode (PersistenceMode.InnerProperty)]
740 public Style LoginButtonStyle {
742 if (logonButtonStyle == null) {
743 logonButtonStyle = new Style ();
744 if (IsTrackingViewState) {
745 (logonButtonStyle as IStateManager).TrackViewState ();
748 return logonButtonStyle;
753 public virtual string LoginButtonText {
755 object o = ViewState ["LoginButtonText"];
756 return (o == null) ? Locale.GetText ("Log In") : (string) o;
760 ViewState.Remove ("LoginButtonText");
762 ViewState ["LoginButtonText"] = value;
766 [DefaultValue (ButtonType.Button)]
767 public virtual ButtonType LoginButtonType {
769 object o = ViewState ["LoginButtonType"];
770 return (o == null) ? ButtonType.Button : (ButtonType) o;
773 if ((value < ButtonType.Button) || (value > ButtonType.Link))
774 throw new ArgumentOutOfRangeException ("LoginButtonType");
775 ViewState ["LoginButtonType"] = (int) value;
781 public virtual string MembershipProvider {
783 object o = ViewState ["MembershipProvider"];
784 return (o == null) ? String.Empty : (string) o;
788 ViewState.Remove ("MembershipProvider");
790 ViewState ["MembershipProvider"] = value;
794 [DefaultValue (Orientation.Vertical)]
795 public virtual Orientation Orientation {
797 object o = ViewState ["Orientation"];
798 return (o == null) ? Orientation.Vertical : (Orientation) o;
801 if ((value < Orientation.Horizontal) || (value > Orientation.Vertical))
802 throw new ArgumentOutOfRangeException ("Orientation");
803 ViewState ["Orientation"] = (int) value;
808 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
809 public virtual string Password {
811 return _password != null ? _password : String.Empty;
816 public virtual string PasswordLabelText {
818 object o = ViewState ["PasswordLabelText"];
819 return (o == null) ? "Password:" : (string) o;
823 ViewState.Remove ("PasswordLabelText");
825 ViewState ["PasswordLabelText"] = value;
830 [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
832 public virtual string PasswordRecoveryIconUrl {
834 object o = ViewState ["PasswordRecoveryIconUrl"];
835 return (o == null) ? String.Empty : (string) o;
839 ViewState.Remove ("PasswordRecoveryIconUrl");
841 ViewState ["PasswordRecoveryIconUrl"] = value;
847 public virtual string PasswordRecoveryText {
849 object o = ViewState ["PasswordRecoveryText"];
850 return (o == null) ? String.Empty : (string) o;
854 ViewState.Remove ("PasswordRecoveryText");
856 ViewState ["PasswordRecoveryText"] = value;
861 [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
863 public virtual string PasswordRecoveryUrl {
865 object o = ViewState ["PasswordRecoveryUrl"];
866 return (o == null) ? String.Empty : (string) o;
870 ViewState.Remove ("PasswordRecoveryUrl");
872 ViewState ["PasswordRecoveryUrl"] = value;
877 public virtual string PasswordRequiredErrorMessage {
879 object o = ViewState ["PasswordRequiredErrorMessage"];
880 return (o == null) ? Locale.GetText ("Password is required.") : (string) o;
884 ViewState.Remove ("PasswordRequiredErrorMessage");
886 ViewState ["PasswordRequiredErrorMessage"] = value;
890 [DefaultValue (true)]
891 public virtual bool RenderOuterTable {
892 get { return renderOuterTable; }
893 set { renderOuterTable = value; }
896 [DefaultValue (false)]
898 public virtual bool RememberMeSet {
900 object o = ViewState ["RememberMeSet"];
901 return (o == null) ? false : (bool) o;
904 ViewState ["RememberMeSet"] = value;
909 public virtual string RememberMeText {
911 object o = ViewState ["RememberMeText"];
912 return (o == null) ? Locale.GetText ("Remember me next time.") : (string) o;
916 ViewState.Remove ("RememberMeText");
918 ViewState ["RememberMeText"] = value;
922 protected override HtmlTextWriterTag TagKey {
923 get { return HtmlTextWriterTag.Table; }
926 [DefaultValue (null)]
927 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
928 [NotifyParentProperty (true)]
929 [PersistenceMode (PersistenceMode.InnerProperty)]
930 public Style TextBoxStyle {
932 if (textBoxStyle == null) {
933 textBoxStyle = new Style ();
934 if (IsTrackingViewState) {
935 (textBoxStyle as IStateManager).TrackViewState ();
942 [DefaultValue (LoginTextLayout.TextOnLeft)]
943 public virtual LoginTextLayout TextLayout {
945 object o = ViewState ["TextLayout"];
946 return (o == null) ? LoginTextLayout.TextOnLeft : (LoginTextLayout) o;
949 if ((value < LoginTextLayout.TextOnLeft) || (value > LoginTextLayout.TextOnTop))
950 throw new ArgumentOutOfRangeException ("TextLayout");
951 ViewState ["TextLayout"] = (int) value;
956 public virtual string TitleText {
958 object o = ViewState ["TitleText"];
959 return (o == null) ? Locale.GetText ("Log In") : (string) o;
963 ViewState.Remove ("TitleText");
965 ViewState ["TitleText"] = value;
969 [DefaultValue (null)]
970 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
971 [NotifyParentProperty (true)]
972 [PersistenceMode (PersistenceMode.InnerProperty)]
973 public TableItemStyle TitleTextStyle {
975 if (titleTextStyle == null) {
976 titleTextStyle = new TableItemStyle ();
977 if (IsTrackingViewState) {
978 (titleTextStyle as IStateManager).TrackViewState ();
981 return titleTextStyle;
986 public virtual string UserName {
988 object o = ViewState ["UserName"];
989 return (o == null) ? String.Empty : (string) o;
993 ViewState.Remove ("UserName");
995 ViewState ["UserName"] = value;
1000 public virtual string UserNameLabelText {
1002 object o = ViewState ["UserNameLabelText"];
1003 return (o == null) ? Locale.GetText ("User Name:") : (string) o;
1007 ViewState.Remove ("UserNameLabelText");
1009 ViewState ["UserNameLabelText"] = value;
1013 [Localizable (true)]
1014 public virtual string UserNameRequiredErrorMessage {
1016 object o = ViewState ["UserNameRequiredErrorMessage"];
1017 return (o == null) ? Locale.GetText ("User Name is required.") : (string) o;
1021 ViewState.Remove ("UserNameRequiredErrorMessage");
1023 ViewState ["UserNameRequiredErrorMessage"] = value;
1027 [DefaultValue (null)]
1028 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
1029 [NotifyParentProperty (true)]
1030 [PersistenceMode (PersistenceMode.InnerProperty)]
1031 public Style ValidatorTextStyle {
1033 if (validatorTextStyle == null) {
1034 validatorTextStyle = new Style ();
1035 if (IsTrackingViewState) {
1036 (validatorTextStyle as IStateManager).TrackViewState ();
1039 return validatorTextStyle;
1043 [DefaultValue (true)]
1045 public virtual bool VisibleWhenLoggedIn {
1047 object o = ViewState ["VisibleWhenLoggedIn"];
1048 return (o == null) ? true : (bool) o;
1051 ViewState ["VisibleWhenLoggedIn"] = value;
1055 LoginContainer LoginTemplateContainer
1058 if (container == null)
1059 container = new LoginContainer (this);
1067 protected internal override void CreateChildControls ()
1071 ITemplate template = LayoutTemplate;
1072 if (template == null)
1073 template = new LoginTemplate (this);
1075 LoginTemplateContainer.InstantiateTemplate (template);
1077 Controls.Add (container);
1079 IEditableTextControl editable;
1080 editable = container.UserNameTextBox as IEditableTextControl;
1082 if (editable != null) {
1083 editable.Text = UserName;
1084 editable.TextChanged += new EventHandler (UserName_TextChanged);
1087 throw new HttpException ("LayoutTemplate does not contain an IEditableTextControl with ID UserName for the username.");
1089 editable = container.PasswordTextBox as IEditableTextControl;
1091 if (editable != null)
1092 editable.TextChanged += new EventHandler (Password_TextChanged);
1094 throw new HttpException ("LayoutTemplate does not contain an IEditableTextControl with ID Password for the password.");
1096 ICheckBoxControl checkBox = container.RememberMeCheckBox as ICheckBoxControl;
1098 if (checkBox != null)
1099 checkBox.CheckedChanged += new EventHandler (RememberMe_CheckedChanged);
1102 protected override void LoadViewState (object savedState)
1104 if (savedState == null) {
1105 base.LoadViewState (null);
1109 object[] state = (object[]) savedState;
1110 base.LoadViewState (state [0]);
1111 if (state [1] != null)
1112 (LoginButtonStyle as IStateManager).LoadViewState (state [1]);
1113 if (state [2] != null)
1114 (LabelStyle as IStateManager).LoadViewState (state [2]);
1115 if (state [3] != null)
1116 (TextBoxStyle as IStateManager).LoadViewState (state [3]);
1117 if (state [4] != null)
1118 (HyperLinkStyle as IStateManager).LoadViewState (state [4]);
1119 if (state [5] != null)
1120 (InstructionTextStyle as IStateManager).LoadViewState (state [5]);
1121 if (state [6] != null)
1122 (TitleTextStyle as IStateManager).LoadViewState (state [6]);
1123 if (state [7] != null)
1124 (CheckBoxStyle as IStateManager).LoadViewState (state [7]);
1125 if (state [8] != null)
1126 (FailureTextStyle as IStateManager).LoadViewState (state [8]);
1127 if (state [9] != null)
1128 (ValidatorTextStyle as IStateManager).LoadViewState (state [9]);
1131 bool HasOnAuthenticateHandler ()
1133 return Events [authenticateEvent] != null;
1136 protected virtual void OnAuthenticate (AuthenticateEventArgs e)
1138 // this gets called after OnLoggingIn and the authentication so we can change the result
1139 AuthenticateEventHandler authenticate = (AuthenticateEventHandler) Events [authenticateEvent];
1140 if (authenticate != null)
1141 authenticate (this, e);
1144 protected override bool OnBubbleEvent (object source, EventArgs e)
1146 // check for submit button
1147 CommandEventArgs cea = (e as CommandEventArgs);
1148 if ((cea != null) &&
1149 String.Equals (cea.CommandName, LoginButtonCommandName, StringComparison.InvariantCultureIgnoreCase)) {
1150 if (!AuthenticateUser ()) {
1151 ITextControl failureText = LoginTemplateContainer.FailureTextLiteral;
1152 if (failureText != null)
1153 failureText.Text = FailureText;
1160 protected virtual void OnLoggedIn (EventArgs e)
1162 // this gets called only if the authentication was successful
1163 EventHandler loggedIn = (EventHandler) Events [loggedInEvent];
1164 if (loggedIn != null)
1168 protected virtual void OnLoggingIn (LoginCancelEventArgs e)
1170 // this gets called before OnAuthenticate so we can abort the authentication process
1171 LoginCancelEventHandler loggingIn = (LoginCancelEventHandler) Events [loggingInEvent];
1172 if (loggingIn != null)
1173 loggingIn (this, e);
1176 protected virtual void OnLoginError (EventArgs e)
1178 // this gets called only if the authentication wasn't successful
1179 EventHandler loginError = (EventHandler) Events [loginErrorEvent];
1180 if (loginError != null)
1181 loginError (this, e);
1184 [MonoTODO ("overriden for ?")]
1185 protected internal override void OnPreRender (EventArgs e)
1187 base.OnPreRender (e);
1188 // note: doc says that UserName and Password aren't available at
1189 // PageLoad but are during PreRender phase, so... ???
1192 protected internal override void Render (HtmlTextWriter writer)
1195 VerifyInlinePropertiesNotSet ();
1197 // VisibleWhenLoggedIn isn't applicable to the default login page
1198 if (!VisibleWhenLoggedIn && !IsDefaultLoginPage () && IsLoggedIn ())
1203 page.VerifyRenderingInServerForm (this);
1205 EnsureChildControls ();
1207 foreach (object [] styleDef in styles)
1208 ((WebControl) styleDef [0]).ApplyStyle ((Style) styleDef [1]);
1210 RenderContents(writer);
1213 protected override object SaveViewState ()
1215 object[] state = new object [10];
1216 state [0] = base.SaveViewState ();
1217 if (logonButtonStyle != null)
1218 state [1] = (logonButtonStyle as IStateManager).SaveViewState ();
1219 if (labelStyle != null)
1220 state [2] = (labelStyle as IStateManager).SaveViewState ();
1221 if (textBoxStyle != null)
1222 state [3] = (textBoxStyle as IStateManager).SaveViewState ();
1223 if (hyperLinkStyle != null)
1224 state [4] = (hyperLinkStyle as IStateManager).SaveViewState ();
1225 if (instructionTextStyle != null)
1226 state [5] = (instructionTextStyle as IStateManager).SaveViewState ();
1227 if (titleTextStyle != null)
1228 state [6] = (titleTextStyle as IStateManager).SaveViewState ();
1229 if (checkBoxStyle != null)
1230 state [7] = (checkBoxStyle as IStateManager).SaveViewState ();
1231 if (failureTextStyle != null)
1232 state [8] = (failureTextStyle as IStateManager).SaveViewState ();
1233 if (validatorTextStyle != null)
1234 state [9] = (validatorTextStyle as IStateManager).SaveViewState ();
1236 for (int i=0; i < state.Length; i++) {
1237 if (state [0] != null)
1238 return (object) state;
1240 return null; // reduce view state
1243 [MonoTODO ("for design-time usage - no more details available")]
1244 protected override void SetDesignModeState (IDictionary data)
1246 base.SetDesignModeState (data);
1249 protected override void TrackViewState ()
1251 base.TrackViewState ();
1252 if (logonButtonStyle != null)
1253 (logonButtonStyle as IStateManager).TrackViewState ();
1254 if (labelStyle != null)
1255 (labelStyle as IStateManager).TrackViewState ();
1256 if (textBoxStyle != null)
1257 (textBoxStyle as IStateManager).TrackViewState ();
1258 if (hyperLinkStyle != null)
1259 (hyperLinkStyle as IStateManager).TrackViewState ();
1260 if (instructionTextStyle != null)
1261 (instructionTextStyle as IStateManager).TrackViewState ();
1262 if (titleTextStyle != null)
1263 (titleTextStyle as IStateManager).TrackViewState ();
1264 if (checkBoxStyle != null)
1265 (checkBoxStyle as IStateManager).TrackViewState ();
1266 if (failureTextStyle != null)
1267 (failureTextStyle as IStateManager).TrackViewState ();
1268 if (validatorTextStyle != null)
1269 (validatorTextStyle as IStateManager).TrackViewState ();
1275 public event AuthenticateEventHandler Authenticate {
1276 add { Events.AddHandler (authenticateEvent, value); }
1277 remove { Events.RemoveHandler (authenticateEvent, value); }
1280 public event EventHandler LoggedIn {
1281 add { Events.AddHandler (loggedInEvent, value); }
1282 remove { Events.RemoveHandler (loggedInEvent, value); }
1285 public event LoginCancelEventHandler LoggingIn {
1286 add { Events.AddHandler (loggingInEvent, value); }
1287 remove { Events.RemoveHandler (loggingInEvent, value); }
1290 public event EventHandler LoginError {
1291 add { Events.AddHandler (loginErrorEvent, value); }
1292 remove { Events.RemoveHandler (loginErrorEvent, value); }
1298 internal void RegisterApplyStyle (WebControl control, Style style)
1300 styles.Add (new object [] { control, style });
1303 bool AuthenticateUser ()
1308 LoginCancelEventArgs lcea = new LoginCancelEventArgs ();
1313 AuthenticateEventArgs aea = new AuthenticateEventArgs ();
1315 if (!HasOnAuthenticateHandler ()) {
1316 string mp = MembershipProvider;
1317 MembershipProvider provider = (mp.Length == 0) ?
1318 provider = Membership.Provider : Membership.Providers [mp];
1319 if (provider == null) {
1320 throw new HttpException (Locale.GetText ("No provider named '{0}' could be found.", mp));
1323 aea.Authenticated = provider.ValidateUser (UserName, Password);
1325 OnAuthenticate (aea);
1327 if (aea.Authenticated) {
1328 FormsAuthentication.SetAuthCookie (UserName, RememberMeSet);
1329 OnLoggedIn (EventArgs.Empty);
1331 string url = DestinationPageUrl;
1332 if (Page.Request.Path.StartsWith (FormsAuthentication.LoginUrl, StringComparison.InvariantCultureIgnoreCase)) {
1333 if (!String.IsNullOrEmpty (FormsAuthentication.ReturnUrl))
1334 Redirect (FormsAuthentication.ReturnUrl);
1335 else if (!String.IsNullOrEmpty (DestinationPageUrl))
1337 else if (!String.IsNullOrEmpty (FormsAuthentication.DefaultUrl))
1338 Redirect (FormsAuthentication.DefaultUrl);
1339 else if (url.Length == 0)
1342 else if (!String.IsNullOrEmpty (DestinationPageUrl)) {
1351 OnLoginError (EventArgs.Empty);
1352 if (FailureAction == LoginFailureAction.RedirectToLoginPage) {
1353 // login page is defined in web.config
1354 FormsAuthentication.RedirectToLoginPage ();
1360 // TODO: its called from default template only, not usefully, OnBubbleEvent
1361 // do handle command, need be removed
1363 void LoginClick (object sender, CommandEventArgs e)
1365 RaiseBubbleEvent (sender, (EventArgs)e);
1368 bool IsDefaultLoginPage ()
1370 if ((Page == null) || (Page.Request == null))
1372 string defaultLogin = FormsAuthentication.LoginUrl;
1373 if (defaultLogin == null)
1375 string url = Page.Request.Url.AbsolutePath;
1376 return (String.Compare (defaultLogin, 0, url, url.Length - defaultLogin.Length, defaultLogin.Length,
1377 true, Helpers.InvariantCulture) == 0);
1382 if ((Page == null) || (Page.Request == null))
1384 return Page.Request.IsAuthenticated;
1387 void Redirect (string url)
1389 if ((Page != null) && (Page.Response != null))
1390 Page.Response.Redirect (url);
1394 if ((Page != null) && (Page.Response != null))
1395 Page.Response.Redirect (Page.Request.RawUrl);
1398 void UserName_TextChanged (object sender, EventArgs e)
1400 UserName = ((ITextControl)sender).Text;
1403 void Password_TextChanged (object sender, EventArgs e)
1405 _password = ((ITextControl)sender).Text;
1408 void RememberMe_CheckedChanged (object sender, EventArgs e)
1410 RememberMeSet = ((ICheckBoxControl)sender).Checked;