[LoongArch64] Part-5:add loongarch support in some files for LoongArch64. (#21769)
[mono-project.git] / mcs / class / System.Web / System.Web.UI.WebControls / Login.cs
blob392a665c57a962e5ccba736c02ae1de720eb8471
1 //
2 // System.Web.UI.WebControls.Login class
3 //
4 // Author:
5 // Sebastien Pouliot <sebastien@ximian.com>
6 // Konstantin Triger <kostat@mainsoft.com>
7 //
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9 //
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:
17 //
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 //
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 {
40 // CAS
41 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
42 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
43 // attributes
44 [Bindable (false)]
45 [DefaultEvent ("Authenticate")]
46 [Designer ("System.Web.UI.Design.WebControls.LoginDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
47 public class Login : CompositeControl
48 , IRenderOuterTable
50 #region LoginContainer
51 // TODO: This class should probably be folded into a generic one with BaseChangePasswordContainer
52 sealed class LoginContainer : Control
54 readonly Login _owner;
55 bool renderOuterTable;
56 Table _table;
57 TableCell _containerCell;
59 public LoginContainer (Login owner)
61 _owner = owner;
62 renderOuterTable = _owner.RenderOuterTable;
64 if (renderOuterTable)
65 InitTable ();
68 public override string ID {
69 get {
70 return _owner.ID;
72 set {
73 _owner.ID = value;
77 public override string ClientID {
78 get {
79 return _owner.ClientID;
83 public void InstantiateTemplate (ITemplate template)
85 if (!renderOuterTable)
86 template.InstantiateIn (this);
87 else
88 template.InstantiateIn (_containerCell);
91 void InitTable ()
93 _table = new Table ();
94 _containerCell = new TableCell ();
96 TableRow row = new TableRow ();
97 row.Cells.Add (_containerCell);
98 _table.Rows.Add (row);
100 Controls.AddAt (0, _table);
103 protected internal override void Render (HtmlTextWriter writer)
105 if (_table != null) {
106 _table.CellSpacing = 0;
107 _table.CellPadding = _owner.BorderPadding;
108 _table.ApplyStyle (_owner.ControlStyle);
109 _table.Attributes.CopyFrom (_owner.Attributes);
112 base.Render (writer);
115 public Control UserNameTextBox {
116 get {
117 return FindControl("UserName");
121 public Control PasswordTextBox {
122 get {
123 return FindControl("Password");
127 public Control RememberMeCheckBox {
128 get {
129 return FindControl("RememberMe");
133 public ITextControl FailureTextLiteral
135 get {
136 return FindControl ("FailureText") as ITextControl;
141 #endregion
143 #region LoginTemplate
145 sealed class LoginTemplate : WebControl, ITemplate
147 readonly Login _login;
149 public LoginTemplate (Login login)
151 _login = login;
154 void ITemplate.InstantiateIn (Control container)
157 LiteralControl TitleText = new LiteralControl (_login.TitleText);
160 LiteralControl InstructionText = new LiteralControl (_login.InstructionText);
163 TextBox UserName = new TextBox ();
164 UserName.ID = "UserName";
165 UserName.Text = _login.UserName;
166 _login.RegisterApplyStyle (UserName, _login.TextBoxStyle);
168 Label UserNameLabel = new Label ();
169 UserNameLabel.ID = "UserNameLabel";
170 UserNameLabel.AssociatedControlID = "UserName";
171 UserNameLabel.Text = _login.UserNameLabelText;
173 RequiredFieldValidator UserNameRequired = new RequiredFieldValidator ();
174 UserNameRequired.ID = "UserNameRequired";
175 UserNameRequired.ControlToValidate = "UserName";
176 UserNameRequired.ErrorMessage = _login.UserNameRequiredErrorMessage;
177 UserNameRequired.ToolTip = _login.UserNameRequiredErrorMessage;
178 UserNameRequired.Text = "*";
179 UserNameRequired.ValidationGroup = _login.ID;
180 _login.RegisterApplyStyle (UserNameRequired, _login.ValidatorTextStyle);
183 TextBox Password = new TextBox ();
184 Password.ID = "Password";
185 Password.TextMode = TextBoxMode.Password;
186 _login.RegisterApplyStyle (Password, _login.TextBoxStyle);
188 Label PasswordLabel = new Label ();
189 PasswordLabel.ID = "PasswordLabel";
190 PasswordLabel.AssociatedControlID = "PasswordLabel";
191 PasswordLabel.Text = _login.PasswordLabelText;
193 RequiredFieldValidator PasswordRequired = new RequiredFieldValidator ();
194 PasswordRequired.ID = "PasswordRequired";
195 PasswordRequired.ControlToValidate = "Password";
196 PasswordRequired.ErrorMessage = _login.PasswordRequiredErrorMessage;
197 PasswordRequired.ToolTip = _login.PasswordRequiredErrorMessage;
198 PasswordRequired.Text = "*";
199 PasswordRequired.ValidationGroup = _login.ID;
200 _login.RegisterApplyStyle (PasswordRequired, _login.ValidatorTextStyle);
202 bool useRememberMe = _login != null ? _login.DisplayRememberMe : true;
203 CheckBox RememberMe;
205 if (useRememberMe) {
206 RememberMe = new CheckBox ();
207 RememberMe.ID = "RememberMe";
208 RememberMe.Checked = _login.RememberMeSet;
209 RememberMe.Text = _login.RememberMeText;
210 _login.RegisterApplyStyle (RememberMe, _login.CheckBoxStyle);
211 } else
212 RememberMe = null;
214 // TODO: Error text
215 Literal FailureText = new Literal ();
216 FailureText.ID = "FailureText";
217 FailureText.EnableViewState = false;
220 WebControl LoginButton = null;
221 switch (_login.LoginButtonType) {
222 case ButtonType.Button:
223 LoginButton = new Button ();
224 LoginButton.ID = "LoginButton";
225 break;
226 case ButtonType.Link:
227 LoginButton = new LinkButton ();
228 LoginButton.ID = "LoginLinkButton";
229 break;
230 case ButtonType.Image:
231 LoginButton = new ImageButton ();
232 LoginButton.ID = "LoginImageButton";
233 break;
235 _login.RegisterApplyStyle (LoginButton, _login.LoginButtonStyle);
236 LoginButton.ID = "LoginButton";
237 ((IButtonControl) LoginButton).Text = _login.LoginButtonText;
238 ((IButtonControl) LoginButton).CommandName = Login.LoginButtonCommandName;
239 ((IButtonControl) LoginButton).Command += new CommandEventHandler (_login.LoginClick);
240 ((IButtonControl) LoginButton).ValidationGroup = _login.ID;
242 // Create layout table
243 Table table = new Table ();
244 table.CellPadding = 0;
246 // Title row
247 table.Rows.Add (
248 CreateRow (
249 CreateCell (TitleText, null, _login.TitleTextStyle, HorizontalAlign.Center)));
251 // Instruction row
252 if (_login.InstructionText.Length > 0) {
253 table.Rows.Add (
254 CreateRow (
255 CreateCell (InstructionText, null, _login.instructionTextStyle, HorizontalAlign.Center)));
258 if (_login.Orientation == Orientation.Horizontal) {
259 // Main row
260 TableRow row1 = new TableRow ();
261 TableRow row2 = new TableRow ();
262 if (_login.TextLayout == LoginTextLayout.TextOnTop)
263 row1.Cells.Add (CreateCell (UserNameLabel, null, _login.LabelStyle));
264 else
265 row2.Cells.Add (CreateCell (UserNameLabel, null, _login.LabelStyle));
266 row2.Cells.Add (CreateCell (UserName, UserNameRequired, null));
267 if (_login.TextLayout == LoginTextLayout.TextOnTop)
268 row1.Cells.Add (CreateCell (PasswordLabel, null, _login.LabelStyle));
269 else
270 row2.Cells.Add (CreateCell (PasswordLabel, null, _login.LabelStyle));
271 row2.Cells.Add (CreateCell (Password, PasswordRequired, null));
272 if (useRememberMe)
273 row2.Cells.Add (CreateCell (RememberMe, null, null));
274 row2.Cells.Add (CreateCell (LoginButton, null, null));
275 if (row1.Cells.Count > 0)
276 table.Rows.Add (row1);
277 table.Rows.Add (row2);
279 else { // Orientation.Vertical
280 if (_login.TextLayout == LoginTextLayout.TextOnLeft)
281 table.Rows.Add (CreateRow (UserNameLabel, UserName, UserNameRequired, _login.LabelStyle));
282 else {
283 table.Rows.Add (CreateRow (UserNameLabel, null, null, _login.LabelStyle));
284 table.Rows.Add (CreateRow (null, UserName, UserNameRequired, null));
286 if (_login.TextLayout == LoginTextLayout.TextOnLeft)
287 table.Rows.Add (CreateRow (PasswordLabel, Password, PasswordRequired, _login.LabelStyle));
288 else {
289 table.Rows.Add (CreateRow (PasswordLabel, null, null, _login.LabelStyle));
290 table.Rows.Add (CreateRow (null, Password, PasswordRequired, null));
292 if (useRememberMe)
293 table.Rows.Add (CreateRow (CreateCell (RememberMe, null, null)));
294 table.Rows.Add (CreateRow (CreateCell (LoginButton, null, null, HorizontalAlign.Right)));
297 // Error message row
298 if (_login.FailureTextStyle.ForeColor.IsEmpty)
299 _login.FailureTextStyle.ForeColor = System.Drawing.Color.Red;
301 table.Rows.Add (
302 CreateRow (
303 CreateCell (FailureText, null, _login.FailureTextStyle)));
305 // Links row
306 TableCell linksCell = new TableCell ();
307 _login.RegisterApplyStyle (linksCell, _login.HyperLinkStyle);
309 if (AddLink (_login.CreateUserUrl, _login.CreateUserText, _login.CreateUserIconUrl, linksCell, _login.HyperLinkStyle))
310 if (_login.Orientation == Orientation.Vertical)
311 linksCell.Controls.Add (new LiteralControl ("<br/>"));
312 else
313 linksCell.Controls.Add (new LiteralControl (" "));
315 if (AddLink (_login.PasswordRecoveryUrl, _login.PasswordRecoveryText, _login.PasswordRecoveryIconUrl, linksCell, _login.HyperLinkStyle))
316 if (_login.Orientation == Orientation.Vertical)
317 linksCell.Controls.Add (new LiteralControl ("<br/>"));
318 else
319 linksCell.Controls.Add (new LiteralControl (" "));
321 AddLink (_login.HelpPageUrl, _login.HelpPageText, _login.HelpPageIconUrl, linksCell, _login.HyperLinkStyle);
322 table.Rows.Add (CreateRow (linksCell));
324 FixTableColumnSpans (table);
325 container.Controls.Add (table);
328 TableRow CreateRow (TableCell cell)
330 TableRow row = new TableRow ();
331 row.Cells.Add (cell);
332 return row;
335 TableRow CreateRow (Control c0, Control c1, Control c2, Style s)
337 TableRow row = new TableRow ();
338 TableCell cell0 = new TableCell ();
339 TableCell cell1 = new TableCell ();
341 if (c0 != null) {
342 cell0.Controls.Add (c0);
343 row.Controls.Add (cell0);
346 if (s != null)
347 cell0.ApplyStyle (s);
349 if ((c1 != null) && (c2 != null)) {
350 cell1.Controls.Add (c1);
351 cell1.Controls.Add (c2);
352 cell0.HorizontalAlign = HorizontalAlign.Right;
353 row.Controls.Add (cell1);
355 return row;
358 TableCell CreateCell (Control c0, Control c1, Style s, HorizontalAlign align)
360 TableCell cell = CreateCell (c0, c1, s);
361 cell.HorizontalAlign = align;
362 return cell;
365 TableCell CreateCell (Control c0, Control c1, Style s)
367 TableCell cell = new TableCell ();
368 if (s != null)
369 cell.ApplyStyle (s);
371 cell.Controls.Add (c0);
372 if (c1 != null)
373 cell.Controls.Add (c1);
375 return cell;
378 bool AddLink (string pageUrl, string linkText, string linkIcon, WebControl container, Style style)
380 bool added = false;
381 if (linkIcon.Length > 0) {
382 Image img = new Image ();
383 img.ImageUrl = linkIcon;
384 container.Controls.Add (img);
385 added = true;
387 if (linkText.Length > 0) {
388 HyperLink link = new HyperLink ();
389 link.NavigateUrl = pageUrl;
390 link.Text = linkText;
391 _login.RegisterApplyStyle (link, style);
392 container.Controls.Add (link);
393 added = true;
395 return added;
398 void FixTableColumnSpans (Table table)
400 int maxCols = 0;
401 for (int row = 0; row < table.Rows.Count; row++) {
402 if (maxCols < table.Rows [row].Cells.Count)
403 maxCols = table.Rows [row].Cells.Count;
405 for (int row = 0; row < table.Rows.Count; row++) {
406 if (table.Rows [row].Cells.Count == 1 && maxCols > 1)
407 table.Rows [row].Cells [0].ColumnSpan = maxCols;
412 #endregion
414 public static readonly string LoginButtonCommandName = "Login";
416 static readonly object authenticateEvent = new object ();
417 static readonly object loggedInEvent = new object ();
418 static readonly object loggingInEvent = new object ();
419 static readonly object loginErrorEvent = new object ();
421 TableItemStyle checkBoxStyle;
422 TableItemStyle failureTextStyle;
423 TableItemStyle hyperLinkStyle;
424 TableItemStyle instructionTextStyle;
425 TableItemStyle labelStyle;
426 Style logonButtonStyle;
427 Style textBoxStyle;
428 TableItemStyle titleTextStyle;
429 Style validatorTextStyle;
430 ArrayList styles = new ArrayList ();
432 ITemplate layoutTemplate;
433 LoginContainer container;
435 string _password;
436 bool renderOuterTable = true;
437 public Login ()
441 [DefaultValue (1)]
442 public virtual int BorderPadding {
443 get {
444 object o = ViewState ["BorderPadding"];
445 return (o == null) ? 1 : (int) o;
447 set {
448 if (value < -1)
449 throw new ArgumentOutOfRangeException ("BorderPadding", "< -1");
450 else
451 ViewState ["BorderPadding"] = value;
455 [DefaultValue (null)]
456 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
457 [NotifyParentProperty (true)]
458 [PersistenceMode (PersistenceMode.InnerProperty)]
459 public TableItemStyle CheckBoxStyle {
460 get {
461 if (checkBoxStyle == null) {
462 checkBoxStyle = new TableItemStyle ();
463 if (IsTrackingViewState) {
464 (checkBoxStyle as IStateManager).TrackViewState ();
467 return checkBoxStyle;
471 [DefaultValue ("")]
472 [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
473 [UrlProperty]
474 public virtual string CreateUserIconUrl {
475 get {
476 object o = ViewState ["CreateUserIconUrl"];
477 return (o == null) ? String.Empty : (string) o;
479 set {
480 if (value == null)
481 ViewState.Remove ("CreateUserIconUrl");
482 else
483 ViewState ["CreateUserIconUrl"] = value;
487 [DefaultValue ("")]
488 [Localizable (true)]
489 public virtual string CreateUserText {
490 get {
491 object o = ViewState ["CreateUserText"];
492 return (o == null) ? String.Empty : (string) o;
494 set {
495 if (value == null)
496 ViewState.Remove ("CreateUserText");
497 else
498 ViewState ["CreateUserText"] = value;
502 [DefaultValue ("")]
503 [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
504 [UrlProperty]
505 public virtual string CreateUserUrl {
506 get {
507 object o = ViewState ["CreateUserUrl"];
508 return (o == null) ? String.Empty : (string) o;
510 set {
511 if (value == null)
512 ViewState.Remove ("CreateUserUrl");
513 else
514 ViewState ["CreateUserUrl"] = value;
518 [DefaultValue ("")]
519 [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
520 [Themeable (false)]
521 [UrlProperty]
522 public virtual string DestinationPageUrl {
523 get {
524 object o = ViewState ["DestinationPageUrl"];
525 return (o == null) ? String.Empty : (string) o;
527 set {
528 if (value == null)
529 ViewState.Remove ("DestinationPageUrl");
530 else
531 ViewState ["DestinationPageUrl"] = value;
535 [DefaultValue (true)]
536 [Themeable (false)]
537 public virtual bool DisplayRememberMe {
538 get {
539 object o = ViewState ["DisplayRememberMe"];
540 return (o == null) ? true : (bool) o;
542 set {
543 ViewState ["DisplayRememberMe"] = value;
547 [DefaultValue (LoginFailureAction.Refresh)]
548 [Themeable (false)]
549 [MonoTODO ("RedirectToLoginPage not yet implemented in FormsAuthentication")]
550 public virtual LoginFailureAction FailureAction {
551 get {
552 object o = ViewState ["FailureAction"];
553 return (o == null) ? LoginFailureAction.Refresh : (LoginFailureAction) o;
555 set {
556 if ((value < LoginFailureAction.Refresh) || (value > LoginFailureAction.RedirectToLoginPage))
557 throw new ArgumentOutOfRangeException ("FailureAction");
558 ViewState ["FailureAction"] = (int) value;
562 [Localizable (true)]
563 public virtual string FailureText {
564 get {
565 object o = ViewState ["FailureText"];
566 return (o == null) ? Locale.GetText ("Your login attempt was not successful. Please try again.") : (string) o;
568 set {
569 if (value == null)
570 ViewState.Remove ("FailureText");
571 else
572 ViewState ["FailureText"] = value;
576 [DefaultValue (null)]
577 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
578 [NotifyParentProperty (true)]
579 [PersistenceMode (PersistenceMode.InnerProperty)]
580 public TableItemStyle FailureTextStyle {
581 get {
582 if (failureTextStyle == null) {
583 failureTextStyle = new TableItemStyle ();
584 if (IsTrackingViewState) {
585 (failureTextStyle as IStateManager).TrackViewState ();
588 return failureTextStyle;
592 [DefaultValue ("")]
593 [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
594 [UrlProperty]
595 public virtual string HelpPageIconUrl {
596 get {
597 object o = ViewState ["HelpPageIconUrl"];
598 return (o == null) ? String.Empty : (string) o;
600 set {
601 if (value == null)
602 ViewState.Remove ("HelpPageIconUrl");
603 else
604 ViewState ["HelpPageIconUrl"] = value;
608 [DefaultValue ("")]
609 [Localizable (true)]
610 public virtual string HelpPageText {
611 get {
612 object o = ViewState ["HelpPageText"];
613 return (o == null) ? String.Empty : (string) o;
615 set {
616 if (value == null)
617 ViewState.Remove ("HelpPageText");
618 else
619 ViewState ["HelpPageText"] = value;
623 [DefaultValue ("")]
624 [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
625 [UrlProperty]
626 public virtual string HelpPageUrl {
627 get {
628 object o = ViewState ["HelpPageUrl"];
629 return (o == null) ? String.Empty : (string) o;
631 set {
632 if (value == null)
633 ViewState.Remove ("HelpPageUrl");
634 else
635 ViewState ["HelpPageUrl"] = value;
639 [DefaultValue (null)]
640 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
641 [NotifyParentProperty (true)]
642 [PersistenceMode (PersistenceMode.InnerProperty)]
643 public TableItemStyle HyperLinkStyle {
644 get {
645 if (hyperLinkStyle == null) {
646 hyperLinkStyle = new TableItemStyle ();
647 if (IsTrackingViewState) {
648 (hyperLinkStyle as IStateManager).TrackViewState ();
651 return hyperLinkStyle;
655 [DefaultValue ("")]
656 [Localizable (true)]
657 public virtual string InstructionText {
658 get {
659 object o = ViewState ["InstructionText"];
660 return (o == null) ? String.Empty : (string) o;
662 set {
663 if (value == null)
664 ViewState.Remove ("InstructionText");
665 else
666 ViewState ["InstructionText"] = value;
670 [DefaultValue (null)]
671 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
672 [NotifyParentProperty (true)]
673 [PersistenceMode (PersistenceMode.InnerProperty)]
674 public TableItemStyle InstructionTextStyle {
675 get {
676 if (instructionTextStyle == null) {
677 instructionTextStyle = new TableItemStyle ();
678 if (IsTrackingViewState) {
679 (instructionTextStyle as IStateManager).TrackViewState ();
682 return instructionTextStyle;
686 [DefaultValue (null)]
687 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
688 [NotifyParentProperty (true)]
689 [PersistenceMode (PersistenceMode.InnerProperty)]
690 public TableItemStyle LabelStyle {
691 get {
692 if (labelStyle == null) {
693 labelStyle = new TableItemStyle ();
694 if (IsTrackingViewState) {
695 (labelStyle as IStateManager).TrackViewState ();
698 return labelStyle;
702 [Browsable (false)]
703 [TemplateContainer (typeof (Login))]
704 [PersistenceMode (PersistenceMode.InnerProperty)]
705 public virtual ITemplate LayoutTemplate {
706 get { return layoutTemplate; }
707 set { layoutTemplate = value; }
710 [DefaultValue ("")]
711 [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
712 [UrlProperty]
713 public virtual string LoginButtonImageUrl {
714 get {
715 object o = ViewState ["LoginButtonImageUrl"];
716 return (o == null) ? String.Empty : (string) o;
718 set {
719 if (value == null)
720 ViewState.Remove ("LoginButtonImageUrl");
721 else
722 ViewState ["LoginButtonImageUrl"] = value;
726 [DefaultValue (null)]
727 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
728 [NotifyParentProperty (true)]
729 [PersistenceMode (PersistenceMode.InnerProperty)]
730 public Style LoginButtonStyle {
731 get {
732 if (logonButtonStyle == null) {
733 logonButtonStyle = new Style ();
734 if (IsTrackingViewState) {
735 (logonButtonStyle as IStateManager).TrackViewState ();
738 return logonButtonStyle;
742 [Localizable (true)]
743 public virtual string LoginButtonText {
744 get {
745 object o = ViewState ["LoginButtonText"];
746 return (o == null) ? Locale.GetText ("Log In") : (string) o;
748 set {
749 if (value == null)
750 ViewState.Remove ("LoginButtonText");
751 else
752 ViewState ["LoginButtonText"] = value;
756 [DefaultValue (ButtonType.Button)]
757 public virtual ButtonType LoginButtonType {
758 get {
759 object o = ViewState ["LoginButtonType"];
760 return (o == null) ? ButtonType.Button : (ButtonType) o;
762 set {
763 if ((value < ButtonType.Button) || (value > ButtonType.Link))
764 throw new ArgumentOutOfRangeException ("LoginButtonType");
765 ViewState ["LoginButtonType"] = (int) value;
769 [DefaultValue ("")]
770 [Themeable (false)]
771 public virtual string MembershipProvider {
772 get {
773 object o = ViewState ["MembershipProvider"];
774 return (o == null) ? String.Empty : (string) o;
776 set {
777 if (value == null)
778 ViewState.Remove ("MembershipProvider");
779 else
780 ViewState ["MembershipProvider"] = value;
784 [DefaultValue (Orientation.Vertical)]
785 public virtual Orientation Orientation {
786 get {
787 object o = ViewState ["Orientation"];
788 return (o == null) ? Orientation.Vertical : (Orientation) o;
790 set {
791 if ((value < Orientation.Horizontal) || (value > Orientation.Vertical))
792 throw new ArgumentOutOfRangeException ("Orientation");
793 ViewState ["Orientation"] = (int) value;
797 [Browsable (false)]
798 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
799 public virtual string Password {
800 get {
801 return _password != null ? _password : String.Empty;
805 [Localizable (true)]
806 public virtual string PasswordLabelText {
807 get {
808 object o = ViewState ["PasswordLabelText"];
809 return (o == null) ? "Password:" : (string) o;
811 set {
812 if (value == null)
813 ViewState.Remove ("PasswordLabelText");
814 else
815 ViewState ["PasswordLabelText"] = value;
819 [DefaultValue ("")]
820 [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
821 [UrlProperty]
822 public virtual string PasswordRecoveryIconUrl {
823 get {
824 object o = ViewState ["PasswordRecoveryIconUrl"];
825 return (o == null) ? String.Empty : (string) o;
827 set {
828 if (value == null)
829 ViewState.Remove ("PasswordRecoveryIconUrl");
830 else
831 ViewState ["PasswordRecoveryIconUrl"] = value;
835 [DefaultValue ("")]
836 [Localizable (true)]
837 public virtual string PasswordRecoveryText {
838 get {
839 object o = ViewState ["PasswordRecoveryText"];
840 return (o == null) ? String.Empty : (string) o;
842 set {
843 if (value == null)
844 ViewState.Remove ("PasswordRecoveryText");
845 else
846 ViewState ["PasswordRecoveryText"] = value;
850 [DefaultValue ("")]
851 [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
852 [UrlProperty]
853 public virtual string PasswordRecoveryUrl {
854 get {
855 object o = ViewState ["PasswordRecoveryUrl"];
856 return (o == null) ? String.Empty : (string) o;
858 set {
859 if (value == null)
860 ViewState.Remove ("PasswordRecoveryUrl");
861 else
862 ViewState ["PasswordRecoveryUrl"] = value;
866 [Localizable (true)]
867 public virtual string PasswordRequiredErrorMessage {
868 get {
869 object o = ViewState ["PasswordRequiredErrorMessage"];
870 return (o == null) ? Locale.GetText ("Password is required.") : (string) o;
872 set {
873 if (value == null)
874 ViewState.Remove ("PasswordRequiredErrorMessage");
875 else
876 ViewState ["PasswordRequiredErrorMessage"] = value;
879 [DefaultValue (true)]
880 public virtual bool RenderOuterTable {
881 get { return renderOuterTable; }
882 set { renderOuterTable = value; }
884 [DefaultValue (false)]
885 [Themeable (false)]
886 public virtual bool RememberMeSet {
887 get {
888 object o = ViewState ["RememberMeSet"];
889 return (o == null) ? false : (bool) o;
891 set {
892 ViewState ["RememberMeSet"] = value;
896 [Localizable (true)]
897 public virtual string RememberMeText {
898 get {
899 object o = ViewState ["RememberMeText"];
900 return (o == null) ? Locale.GetText ("Remember me next time.") : (string) o;
902 set {
903 if (value == null)
904 ViewState.Remove ("RememberMeText");
905 else
906 ViewState ["RememberMeText"] = value;
910 protected override HtmlTextWriterTag TagKey {
911 get { return HtmlTextWriterTag.Table; }
914 [DefaultValue (null)]
915 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
916 [NotifyParentProperty (true)]
917 [PersistenceMode (PersistenceMode.InnerProperty)]
918 public Style TextBoxStyle {
919 get {
920 if (textBoxStyle == null) {
921 textBoxStyle = new Style ();
922 if (IsTrackingViewState) {
923 (textBoxStyle as IStateManager).TrackViewState ();
926 return textBoxStyle;
930 [DefaultValue (LoginTextLayout.TextOnLeft)]
931 public virtual LoginTextLayout TextLayout {
932 get {
933 object o = ViewState ["TextLayout"];
934 return (o == null) ? LoginTextLayout.TextOnLeft : (LoginTextLayout) o;
936 set {
937 if ((value < LoginTextLayout.TextOnLeft) || (value > LoginTextLayout.TextOnTop))
938 throw new ArgumentOutOfRangeException ("TextLayout");
939 ViewState ["TextLayout"] = (int) value;
943 [Localizable (true)]
944 public virtual string TitleText {
945 get {
946 object o = ViewState ["TitleText"];
947 return (o == null) ? Locale.GetText ("Log In") : (string) o;
949 set {
950 if (value == null)
951 ViewState.Remove ("TitleText");
952 else
953 ViewState ["TitleText"] = value;
957 [DefaultValue (null)]
958 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
959 [NotifyParentProperty (true)]
960 [PersistenceMode (PersistenceMode.InnerProperty)]
961 public TableItemStyle TitleTextStyle {
962 get {
963 if (titleTextStyle == null) {
964 titleTextStyle = new TableItemStyle ();
965 if (IsTrackingViewState) {
966 (titleTextStyle as IStateManager).TrackViewState ();
969 return titleTextStyle;
973 [DefaultValue ("")]
974 public virtual string UserName {
975 get {
976 object o = ViewState ["UserName"];
977 return (o == null) ? String.Empty : (string) o;
979 set {
980 if (value == null)
981 ViewState.Remove ("UserName");
982 else
983 ViewState ["UserName"] = value;
987 [Localizable (true)]
988 public virtual string UserNameLabelText {
989 get {
990 object o = ViewState ["UserNameLabelText"];
991 return (o == null) ? Locale.GetText ("User Name:") : (string) o;
993 set {
994 if (value == null)
995 ViewState.Remove ("UserNameLabelText");
996 else
997 ViewState ["UserNameLabelText"] = value;
1001 [Localizable (true)]
1002 public virtual string UserNameRequiredErrorMessage {
1003 get {
1004 object o = ViewState ["UserNameRequiredErrorMessage"];
1005 return (o == null) ? Locale.GetText ("User Name is required.") : (string) o;
1007 set {
1008 if (value == null)
1009 ViewState.Remove ("UserNameRequiredErrorMessage");
1010 else
1011 ViewState ["UserNameRequiredErrorMessage"] = value;
1015 [DefaultValue (null)]
1016 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
1017 [NotifyParentProperty (true)]
1018 [PersistenceMode (PersistenceMode.InnerProperty)]
1019 public Style ValidatorTextStyle {
1020 get {
1021 if (validatorTextStyle == null) {
1022 validatorTextStyle = new Style ();
1023 if (IsTrackingViewState) {
1024 (validatorTextStyle as IStateManager).TrackViewState ();
1027 return validatorTextStyle;
1031 [DefaultValue (true)]
1032 [Themeable (false)]
1033 public virtual bool VisibleWhenLoggedIn {
1034 get {
1035 object o = ViewState ["VisibleWhenLoggedIn"];
1036 return (o == null) ? true : (bool) o;
1038 set {
1039 ViewState ["VisibleWhenLoggedIn"] = value;
1043 LoginContainer LoginTemplateContainer
1045 get {
1046 if (container == null)
1047 container = new LoginContainer (this);
1048 return container;
1053 // methods
1055 protected internal override void CreateChildControls ()
1057 Controls.Clear ();
1059 ITemplate template = LayoutTemplate;
1060 if (template == null)
1061 template = new LoginTemplate (this);
1063 LoginTemplateContainer.InstantiateTemplate (template);
1065 Controls.Add (container);
1067 IEditableTextControl editable;
1068 editable = container.UserNameTextBox as IEditableTextControl;
1070 if (editable != null) {
1071 editable.Text = UserName;
1072 editable.TextChanged += new EventHandler (UserName_TextChanged);
1074 else
1075 throw new HttpException ("LayoutTemplate does not contain an IEditableTextControl with ID UserName for the username.");
1077 editable = container.PasswordTextBox as IEditableTextControl;
1079 if (editable != null)
1080 editable.TextChanged += new EventHandler (Password_TextChanged);
1081 else
1082 throw new HttpException ("LayoutTemplate does not contain an IEditableTextControl with ID Password for the password.");
1084 ICheckBoxControl checkBox = container.RememberMeCheckBox as ICheckBoxControl;
1086 if (checkBox != null)
1087 checkBox.CheckedChanged += new EventHandler (RememberMe_CheckedChanged);
1090 protected override void LoadViewState (object savedState)
1092 if (savedState == null) {
1093 base.LoadViewState (null);
1094 return;
1097 object[] state = (object[]) savedState;
1098 base.LoadViewState (state [0]);
1099 if (state [1] != null)
1100 (LoginButtonStyle as IStateManager).LoadViewState (state [1]);
1101 if (state [2] != null)
1102 (LabelStyle as IStateManager).LoadViewState (state [2]);
1103 if (state [3] != null)
1104 (TextBoxStyle as IStateManager).LoadViewState (state [3]);
1105 if (state [4] != null)
1106 (HyperLinkStyle as IStateManager).LoadViewState (state [4]);
1107 if (state [5] != null)
1108 (InstructionTextStyle as IStateManager).LoadViewState (state [5]);
1109 if (state [6] != null)
1110 (TitleTextStyle as IStateManager).LoadViewState (state [6]);
1111 if (state [7] != null)
1112 (CheckBoxStyle as IStateManager).LoadViewState (state [7]);
1113 if (state [8] != null)
1114 (FailureTextStyle as IStateManager).LoadViewState (state [8]);
1115 if (state [9] != null)
1116 (ValidatorTextStyle as IStateManager).LoadViewState (state [9]);
1119 bool HasOnAuthenticateHandler ()
1121 return Events [authenticateEvent] != null;
1124 protected virtual void OnAuthenticate (AuthenticateEventArgs e)
1126 // this gets called after OnLoggingIn and the authentication so we can change the result
1127 AuthenticateEventHandler authenticate = (AuthenticateEventHandler) Events [authenticateEvent];
1128 if (authenticate != null)
1129 authenticate (this, e);
1132 protected override bool OnBubbleEvent (object source, EventArgs e)
1134 // check for submit button
1135 CommandEventArgs cea = (e as CommandEventArgs);
1136 if ((cea != null) &&
1137 String.Equals (cea.CommandName, LoginButtonCommandName, StringComparison.InvariantCultureIgnoreCase)) {
1138 if (!AuthenticateUser ()) {
1139 ITextControl failureText = LoginTemplateContainer.FailureTextLiteral;
1140 if (failureText != null)
1141 failureText.Text = FailureText;
1143 return true;
1145 return false;
1148 protected virtual void OnLoggedIn (EventArgs e)
1150 // this gets called only if the authentication was successful
1151 EventHandler loggedIn = (EventHandler) Events [loggedInEvent];
1152 if (loggedIn != null)
1153 loggedIn (this, e);
1156 protected virtual void OnLoggingIn (LoginCancelEventArgs e)
1158 // this gets called before OnAuthenticate so we can abort the authentication process
1159 LoginCancelEventHandler loggingIn = (LoginCancelEventHandler) Events [loggingInEvent];
1160 if (loggingIn != null)
1161 loggingIn (this, e);
1164 protected virtual void OnLoginError (EventArgs e)
1166 // this gets called only if the authentication wasn't successful
1167 EventHandler loginError = (EventHandler) Events [loginErrorEvent];
1168 if (loginError != null)
1169 loginError (this, e);
1172 [MonoTODO ("overriden for ?")]
1173 protected internal override void OnPreRender (EventArgs e)
1175 base.OnPreRender (e);
1176 // note: doc says that UserName and Password aren't available at
1177 // PageLoad but are during PreRender phase, so... ???
1180 protected internal override void Render (HtmlTextWriter writer)
1182 VerifyInlinePropertiesNotSet ();
1183 // VisibleWhenLoggedIn isn't applicable to the default login page
1184 if (!VisibleWhenLoggedIn && !IsDefaultLoginPage () && IsLoggedIn ())
1185 return;
1187 Page page = Page;
1188 if (page != null)
1189 page.VerifyRenderingInServerForm (this);
1191 EnsureChildControls ();
1193 foreach (object [] styleDef in styles)
1194 ((WebControl) styleDef [0]).ApplyStyle ((Style) styleDef [1]);
1196 RenderContents(writer);
1199 protected override object SaveViewState ()
1201 object[] state = new object [10];
1202 state [0] = base.SaveViewState ();
1203 if (logonButtonStyle != null)
1204 state [1] = (logonButtonStyle as IStateManager).SaveViewState ();
1205 if (labelStyle != null)
1206 state [2] = (labelStyle as IStateManager).SaveViewState ();
1207 if (textBoxStyle != null)
1208 state [3] = (textBoxStyle as IStateManager).SaveViewState ();
1209 if (hyperLinkStyle != null)
1210 state [4] = (hyperLinkStyle as IStateManager).SaveViewState ();
1211 if (instructionTextStyle != null)
1212 state [5] = (instructionTextStyle as IStateManager).SaveViewState ();
1213 if (titleTextStyle != null)
1214 state [6] = (titleTextStyle as IStateManager).SaveViewState ();
1215 if (checkBoxStyle != null)
1216 state [7] = (checkBoxStyle as IStateManager).SaveViewState ();
1217 if (failureTextStyle != null)
1218 state [8] = (failureTextStyle as IStateManager).SaveViewState ();
1219 if (validatorTextStyle != null)
1220 state [9] = (validatorTextStyle as IStateManager).SaveViewState ();
1222 for (int i=0; i < state.Length; i++) {
1223 if (state [0] != null)
1224 return (object) state;
1226 return null; // reduce view state
1229 [MonoTODO ("for design-time usage - no more details available")]
1230 protected override void SetDesignModeState (IDictionary data)
1232 base.SetDesignModeState (data);
1235 protected override void TrackViewState ()
1237 base.TrackViewState ();
1238 if (logonButtonStyle != null)
1239 (logonButtonStyle as IStateManager).TrackViewState ();
1240 if (labelStyle != null)
1241 (labelStyle as IStateManager).TrackViewState ();
1242 if (textBoxStyle != null)
1243 (textBoxStyle as IStateManager).TrackViewState ();
1244 if (hyperLinkStyle != null)
1245 (hyperLinkStyle as IStateManager).TrackViewState ();
1246 if (instructionTextStyle != null)
1247 (instructionTextStyle as IStateManager).TrackViewState ();
1248 if (titleTextStyle != null)
1249 (titleTextStyle as IStateManager).TrackViewState ();
1250 if (checkBoxStyle != null)
1251 (checkBoxStyle as IStateManager).TrackViewState ();
1252 if (failureTextStyle != null)
1253 (failureTextStyle as IStateManager).TrackViewState ();
1254 if (validatorTextStyle != null)
1255 (validatorTextStyle as IStateManager).TrackViewState ();
1259 // events
1261 public event AuthenticateEventHandler Authenticate {
1262 add { Events.AddHandler (authenticateEvent, value); }
1263 remove { Events.RemoveHandler (authenticateEvent, value); }
1266 public event EventHandler LoggedIn {
1267 add { Events.AddHandler (loggedInEvent, value); }
1268 remove { Events.RemoveHandler (loggedInEvent, value); }
1271 public event LoginCancelEventHandler LoggingIn {
1272 add { Events.AddHandler (loggingInEvent, value); }
1273 remove { Events.RemoveHandler (loggingInEvent, value); }
1276 public event EventHandler LoginError {
1277 add { Events.AddHandler (loginErrorEvent, value); }
1278 remove { Events.RemoveHandler (loginErrorEvent, value); }
1282 // private stuff
1284 internal void RegisterApplyStyle (WebControl control, Style style)
1286 styles.Add (new object [] { control, style });
1289 bool AuthenticateUser ()
1291 if (!Page.IsValid)
1292 return true;
1294 LoginCancelEventArgs lcea = new LoginCancelEventArgs ();
1295 OnLoggingIn (lcea);
1296 if (lcea.Cancel)
1297 return true;
1299 AuthenticateEventArgs aea = new AuthenticateEventArgs ();
1301 if (!HasOnAuthenticateHandler ()) {
1302 string mp = MembershipProvider;
1303 MembershipProvider provider = (mp.Length == 0) ?
1304 provider = Membership.Provider : Membership.Providers [mp];
1305 if (provider == null) {
1306 throw new HttpException (Locale.GetText ("No provider named '{0}' could be found.", mp));
1309 aea.Authenticated = provider.ValidateUser (UserName, Password);
1311 OnAuthenticate (aea);
1313 if (aea.Authenticated) {
1314 FormsAuthentication.SetAuthCookie (UserName, RememberMeSet);
1315 OnLoggedIn (EventArgs.Empty);
1317 string url = DestinationPageUrl;
1318 if (Page.Request.Path.StartsWith (FormsAuthentication.LoginUrl, StringComparison.InvariantCultureIgnoreCase)) {
1319 if (!String.IsNullOrEmpty (FormsAuthentication.ReturnUrl))
1320 Redirect (FormsAuthentication.ReturnUrl);
1321 else if (!String.IsNullOrEmpty (DestinationPageUrl))
1322 Redirect (url);
1323 else if (!String.IsNullOrEmpty (FormsAuthentication.DefaultUrl))
1324 Redirect (FormsAuthentication.DefaultUrl);
1325 else if (url.Length == 0)
1326 Refresh ();
1328 else if (!String.IsNullOrEmpty (DestinationPageUrl)) {
1329 Redirect (url);
1331 else {
1332 Refresh ();
1334 return true;
1336 else {
1337 OnLoginError (EventArgs.Empty);
1338 if (FailureAction == LoginFailureAction.RedirectToLoginPage) {
1339 // login page is defined in web.config
1340 FormsAuthentication.RedirectToLoginPage ();
1342 return false;
1346 // TODO: its called from default template only, not usefully, OnBubbleEvent
1347 // do handle command, need be removed
1348 [MonoTODO()]
1349 void LoginClick (object sender, CommandEventArgs e)
1351 RaiseBubbleEvent (sender, (EventArgs)e);
1354 bool IsDefaultLoginPage ()
1356 if ((Page == null) || (Page.Request == null))
1357 return false;
1358 string defaultLogin = FormsAuthentication.LoginUrl;
1359 if (defaultLogin == null)
1360 return false;
1361 string url = Page.Request.Url.AbsolutePath;
1362 return (String.Compare (defaultLogin, 0, url, url.Length - defaultLogin.Length, defaultLogin.Length,
1363 true, Helpers.InvariantCulture) == 0);
1366 bool IsLoggedIn ()
1368 if ((Page == null) || (Page.Request == null))
1369 return false;
1370 return Page.Request.IsAuthenticated;
1373 void Redirect (string url)
1375 if ((Page != null) && (Page.Response != null))
1376 Page.Response.Redirect (url);
1379 void Refresh () {
1380 if ((Page != null) && (Page.Response != null))
1381 Page.Response.Redirect (Page.Request.RawUrl);
1384 void UserName_TextChanged (object sender, EventArgs e)
1386 UserName = ((ITextControl)sender).Text;
1389 void Password_TextChanged (object sender, EventArgs e)
1391 _password = ((ITextControl)sender).Text;
1394 void RememberMe_CheckedChanged (object sender, EventArgs e)
1396 RememberMeSet = ((ICheckBoxControl)sender).Checked;