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.
32 using System.Collections;
33 using System.Globalization;
34 using System.ComponentModel;
35 using System.Security.Permissions;
36 using System.Web.Security;
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 {
49 #region LoginContainer
51 sealed class LoginContainer : Table
53 private readonly Login _owner = null;
54 TableCell _containerCell = null;
56 public LoginContainer (Login owner)
62 public override string ID {
71 public override string ClientID {
73 return _owner.ClientID;
77 public void InstantiateTemplate (ITemplate template)
79 template.InstantiateIn (_containerCell);
82 private void InitTable ()
84 _containerCell = new TableCell ();
86 TableRow row = new TableRow ();
87 row.Cells.Add (_containerCell);
91 protected internal override void Render (HtmlTextWriter writer) {
93 CellPadding = _owner.BorderPadding;
94 ApplyStyle (_owner.ControlStyle);
95 Attributes.CopyFrom (_owner.Attributes);
99 public Control UserNameTextBox {
101 return FindControl("UserName");
105 public Control PasswordTextBox {
107 return FindControl("Password");
111 public Control RememberMeCheckBox {
113 return FindControl("RememberMe");
117 public ITextControl FailureTextLiteral
120 return FindControl ("FailureText") as ITextControl;
127 #region LoginTemplate
129 sealed class LoginTemplate : WebControl, ITemplate
131 readonly Login _login;
133 public LoginTemplate (Login login)
138 void ITemplate.InstantiateIn (Control container)
141 LiteralControl TitleText = new LiteralControl (_login.TitleText);
144 LiteralControl InstructionText = new LiteralControl (_login.InstructionText);
147 TextBox UserName = new TextBox ();
148 UserName.ID = "UserName";
149 UserName.Text = _login.UserName;
150 _login.RegisterApplyStyle (UserName, _login.TextBoxStyle);
152 Label UserNameLabel = new Label ();
153 UserNameLabel.ID = "UserNameLabel";
154 UserNameLabel.AssociatedControlID = "UserName";
155 UserNameLabel.Text = _login.UserNameLabelText;
157 RequiredFieldValidator UserNameRequired = new RequiredFieldValidator ();
158 UserNameRequired.ID = "UserNameRequired";
159 UserNameRequired.ControlToValidate = "UserName";
160 UserNameRequired.ErrorMessage = _login.UserNameRequiredErrorMessage;
161 UserNameRequired.ToolTip = _login.UserNameRequiredErrorMessage;
162 UserNameRequired.Text = "*";
163 UserNameRequired.ValidationGroup = _login.ID;
164 _login.RegisterApplyStyle (UserNameRequired, _login.ValidatorTextStyle);
167 TextBox Password = new TextBox ();
168 Password.ID = "Password";
169 Password.TextMode = TextBoxMode.Password;
170 _login.RegisterApplyStyle (Password, _login.TextBoxStyle);
172 Label PasswordLabel = new Label ();
173 PasswordLabel.ID = "PasswordLabel";
174 PasswordLabel.AssociatedControlID = "PasswordLabel";
175 PasswordLabel.Text = _login.PasswordLabelText;
177 RequiredFieldValidator PasswordRequired = new RequiredFieldValidator ();
178 PasswordRequired.ID = "PasswordRequired";
179 PasswordRequired.ControlToValidate = "Password";
180 PasswordRequired.ErrorMessage = _login.PasswordRequiredErrorMessage;
181 PasswordRequired.ToolTip = _login.PasswordRequiredErrorMessage;
182 PasswordRequired.Text = "*";
183 PasswordRequired.ValidationGroup = _login.ID;
184 _login.RegisterApplyStyle (PasswordRequired, _login.ValidatorTextStyle);
187 CheckBox RememberMe = new CheckBox ();
188 RememberMe.ID = "RememberMe";
189 RememberMe.Checked = _login.RememberMeSet;
190 RememberMe.Text = _login.RememberMeText;
191 _login.RegisterApplyStyle (RememberMe, _login.CheckBoxStyle);
194 Literal FailureText = new Literal ();
195 FailureText.ID = "FailureText";
196 FailureText.EnableViewState = false;
199 WebControl LoginButton = null;
200 switch (_login.LoginButtonType) {
201 case ButtonType.Button:
202 LoginButton = new Button ();
203 LoginButton.ID = "LoginButton";
205 case ButtonType.Link:
206 LoginButton = new LinkButton ();
207 LoginButton.ID = "LoginLinkButton";
209 case ButtonType.Image:
210 LoginButton = new ImageButton ();
211 LoginButton.ID = "LoginImageButton";
214 _login.RegisterApplyStyle (LoginButton, _login.LoginButtonStyle);
215 LoginButton.ID = "LoginButton";
216 ((IButtonControl) LoginButton).Text = _login.LoginButtonText;
217 ((IButtonControl) LoginButton).CommandName = Login.LoginButtonCommandName;
218 ((IButtonControl) LoginButton).Command += new CommandEventHandler (_login.LoginClick);
219 ((IButtonControl) LoginButton).ValidationGroup = _login.ID;
221 // Create layout table
222 Table table = new Table ();
223 table.CellPadding = 0;
228 CreateCell (TitleText, null, _login.TitleTextStyle, HorizontalAlign.Center)));
231 if (_login.InstructionText.Length > 0) {
234 CreateCell (InstructionText, null, _login.instructionTextStyle, HorizontalAlign.Center)));
237 if (_login.Orientation == Orientation.Horizontal) {
239 TableRow row1 = new TableRow ();
240 TableRow row2 = new TableRow ();
241 if (_login.TextLayout == LoginTextLayout.TextOnTop)
242 row1.Cells.Add (CreateCell (UserNameLabel, null, _login.LabelStyle));
244 row2.Cells.Add (CreateCell (UserNameLabel, null, _login.LabelStyle));
245 row2.Cells.Add (CreateCell (UserName, UserNameRequired, null));
246 if (_login.TextLayout == LoginTextLayout.TextOnTop)
247 row1.Cells.Add (CreateCell (PasswordLabel, null, _login.LabelStyle));
249 row2.Cells.Add (CreateCell (PasswordLabel, null, _login.LabelStyle));
250 row2.Cells.Add (CreateCell (Password, PasswordRequired, null));
251 row2.Cells.Add (CreateCell (RememberMe, null, null));
252 row2.Cells.Add (CreateCell (LoginButton, null, null));
253 if (row1.Cells.Count > 0)
254 table.Rows.Add (row1);
255 table.Rows.Add (row2);
257 else { // Orientation.Vertical
258 if (_login.TextLayout == LoginTextLayout.TextOnLeft)
259 table.Rows.Add (CreateRow (UserNameLabel, UserName, UserNameRequired, _login.LabelStyle));
261 table.Rows.Add (CreateRow (UserNameLabel, null, null, _login.LabelStyle));
262 table.Rows.Add (CreateRow (null, UserName, UserNameRequired, null));
264 if (_login.TextLayout == LoginTextLayout.TextOnLeft)
265 table.Rows.Add (CreateRow (PasswordLabel, Password, PasswordRequired, _login.LabelStyle));
267 table.Rows.Add (CreateRow (PasswordLabel, null, null, _login.LabelStyle));
268 table.Rows.Add (CreateRow (null, Password, PasswordRequired, null));
270 table.Rows.Add (CreateRow (CreateCell (RememberMe, null, null)));
271 table.Rows.Add (CreateRow (CreateCell (LoginButton, null, null, HorizontalAlign.Right)));
275 if (_login.FailureTextStyle.ForeColor.IsEmpty)
276 _login.FailureTextStyle.ForeColor = System.Drawing.Color.Red;
280 CreateCell (FailureText, null, _login.FailureTextStyle)));
283 TableCell linksCell = new TableCell ();
284 _login.RegisterApplyStyle (linksCell, _login.HyperLinkStyle);
286 if (AddLink (_login.CreateUserUrl, _login.CreateUserText, _login.CreateUserIconUrl, linksCell, _login.HyperLinkStyle))
287 if (_login.Orientation == Orientation.Vertical)
288 linksCell.Controls.Add (new LiteralControl ("<br/>"));
290 linksCell.Controls.Add (new LiteralControl (" "));
292 if (AddLink (_login.PasswordRecoveryUrl, _login.PasswordRecoveryText, _login.PasswordRecoveryIconUrl, linksCell, _login.HyperLinkStyle))
293 if (_login.Orientation == Orientation.Vertical)
294 linksCell.Controls.Add (new LiteralControl ("<br/>"));
296 linksCell.Controls.Add (new LiteralControl (" "));
298 AddLink (_login.HelpPageUrl, _login.HelpPageText, _login.HelpPageIconUrl, linksCell, _login.HyperLinkStyle);
299 table.Rows.Add (CreateRow (linksCell));
301 FixTableColumnSpans (table);
302 container.Controls.Add (table);
305 private TableRow CreateRow (TableCell cell)
307 TableRow row = new TableRow ();
308 row.Cells.Add (cell);
312 TableRow CreateRow (Control c0, Control c1, Control c2, Style s)
314 TableRow row = new TableRow ();
315 TableCell cell0 = new TableCell ();
316 TableCell cell1 = new TableCell ();
319 cell0.Controls.Add (c0);
320 row.Controls.Add (cell0);
324 cell0.ApplyStyle (s);
326 if ((c1 != null) && (c2 != null)) {
327 cell1.Controls.Add (c1);
328 cell1.Controls.Add (c2);
329 cell0.HorizontalAlign = HorizontalAlign.Right;
330 row.Controls.Add (cell1);
335 private TableCell CreateCell (Control c0, Control c1, Style s, HorizontalAlign align)
337 TableCell cell = CreateCell (c0, c1, s);
338 cell.HorizontalAlign = align;
342 private TableCell CreateCell (Control c0, Control c1, Style s)
344 TableCell cell = new TableCell ();
348 cell.Controls.Add (c0);
350 cell.Controls.Add (c1);
355 bool AddLink (string pageUrl, string linkText, string linkIcon, WebControl container, Style style)
358 if (linkIcon.Length > 0) {
359 Image img = new Image ();
360 img.ImageUrl = linkIcon;
361 container.Controls.Add (img);
364 if (linkText.Length > 0) {
365 HyperLink link = new HyperLink ();
366 link.NavigateUrl = pageUrl;
367 link.Text = linkText;
368 _login.RegisterApplyStyle (link, style);
369 container.Controls.Add (link);
375 void FixTableColumnSpans (Table table)
378 for (int row = 0; row < table.Rows.Count; row++) {
379 if (maxCols < table.Rows [row].Cells.Count)
380 maxCols = table.Rows [row].Cells.Count;
382 for (int row = 0; row < table.Rows.Count; row++) {
383 if (table.Rows [row].Cells.Count == 1 && maxCols > 1)
384 table.Rows [row].Cells [0].ColumnSpan = maxCols;
391 public static readonly string LoginButtonCommandName = "Login";
393 private static readonly object authenticateEvent = new object ();
394 private static readonly object loggedInEvent = new object ();
395 private static readonly object loggingInEvent = new object ();
396 private static readonly object loginErrorEvent = new object ();
398 private TableItemStyle checkBoxStyle;
399 private TableItemStyle failureTextStyle;
400 private TableItemStyle hyperLinkStyle;
401 private TableItemStyle instructionTextStyle;
402 private TableItemStyle labelStyle;
403 private Style logonButtonStyle;
404 private Style textBoxStyle;
405 private TableItemStyle titleTextStyle;
406 private Style validatorTextStyle;
407 private ArrayList styles = new ArrayList ();
409 private ITemplate layoutTemplate;
410 private LoginContainer container;
412 private string _password;
421 public virtual int BorderPadding {
423 object o = ViewState ["BorderPadding"];
424 return (o == null) ? 1 : (int) o;
428 throw new ArgumentOutOfRangeException ("BorderPadding", "< -1");
430 ViewState ["BorderPadding"] = value;
434 [DefaultValue (null)]
435 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
436 [NotifyParentProperty (true)]
437 [PersistenceMode (PersistenceMode.InnerProperty)]
438 public TableItemStyle CheckBoxStyle {
440 if (checkBoxStyle == null) {
441 checkBoxStyle = new TableItemStyle ();
442 if (IsTrackingViewState) {
443 (checkBoxStyle as IStateManager).TrackViewState ();
446 return checkBoxStyle;
451 [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
453 public virtual string CreateUserIconUrl {
455 object o = ViewState ["CreateUserIconUrl"];
456 return (o == null) ? String.Empty : (string) o;
460 ViewState.Remove ("CreateUserIconUrl");
462 ViewState ["CreateUserIconUrl"] = value;
468 public virtual string CreateUserText {
470 object o = ViewState ["CreateUserText"];
471 return (o == null) ? String.Empty : (string) o;
475 ViewState.Remove ("CreateUserText");
477 ViewState ["CreateUserText"] = value;
482 [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
484 public virtual string CreateUserUrl {
486 object o = ViewState ["CreateUserUrl"];
487 return (o == null) ? String.Empty : (string) o;
491 ViewState.Remove ("CreateUserUrl");
493 ViewState ["CreateUserUrl"] = value;
498 [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
501 public virtual string DestinationPageUrl {
503 object o = ViewState ["DestinationPageUrl"];
504 return (o == null) ? String.Empty : (string) o;
508 ViewState.Remove ("DestinationPageUrl");
510 ViewState ["DestinationPageUrl"] = value;
514 [DefaultValue (true)]
516 public virtual bool DisplayRememberMe {
518 object o = ViewState ["DisplayRememberMe"];
519 return (o == null) ? true : (bool) o;
522 ViewState ["DisplayRememberMe"] = value;
526 [DefaultValue (LoginFailureAction.Refresh)]
528 [MonoTODO ("RedirectToLoginPage not yet implemented in FormsAuthentication")]
529 public virtual LoginFailureAction FailureAction {
531 object o = ViewState ["FailureAction"];
532 return (o == null) ? LoginFailureAction.Refresh : (LoginFailureAction) o;
535 if ((value < LoginFailureAction.Refresh) || (value > LoginFailureAction.RedirectToLoginPage))
536 throw new ArgumentOutOfRangeException ("FailureAction");
537 ViewState ["FailureAction"] = (int) value;
542 public virtual string FailureText {
544 object o = ViewState ["FailureText"];
545 return (o == null) ? Locale.GetText ("Your login attempt was not successful. Please try again.") : (string) o;
549 ViewState.Remove ("FailureText");
551 ViewState ["FailureText"] = value;
555 [DefaultValue (null)]
556 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
557 [NotifyParentProperty (true)]
558 [PersistenceMode (PersistenceMode.InnerProperty)]
559 public TableItemStyle FailureTextStyle {
561 if (failureTextStyle == null) {
562 failureTextStyle = new TableItemStyle ();
563 if (IsTrackingViewState) {
564 (failureTextStyle as IStateManager).TrackViewState ();
567 return failureTextStyle;
572 [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
574 public virtual string HelpPageIconUrl {
576 object o = ViewState ["HelpPageIconUrl"];
577 return (o == null) ? String.Empty : (string) o;
581 ViewState.Remove ("HelpPageIconUrl");
583 ViewState ["HelpPageIconUrl"] = value;
589 public virtual string HelpPageText {
591 object o = ViewState ["HelpPageText"];
592 return (o == null) ? String.Empty : (string) o;
596 ViewState.Remove ("HelpPageText");
598 ViewState ["HelpPageText"] = value;
603 [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
605 public virtual string HelpPageUrl {
607 object o = ViewState ["HelpPageUrl"];
608 return (o == null) ? String.Empty : (string) o;
612 ViewState.Remove ("HelpPageUrl");
614 ViewState ["HelpPageUrl"] = value;
618 [DefaultValue (null)]
619 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
620 [NotifyParentProperty (true)]
621 [PersistenceMode (PersistenceMode.InnerProperty)]
622 public TableItemStyle HyperLinkStyle {
624 if (hyperLinkStyle == null) {
625 hyperLinkStyle = new TableItemStyle ();
626 if (IsTrackingViewState) {
627 (hyperLinkStyle as IStateManager).TrackViewState ();
630 return hyperLinkStyle;
636 public virtual string InstructionText {
638 object o = ViewState ["InstructionText"];
639 return (o == null) ? String.Empty : (string) o;
643 ViewState.Remove ("InstructionText");
645 ViewState ["InstructionText"] = value;
649 [DefaultValue (null)]
650 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
651 [NotifyParentProperty (true)]
652 [PersistenceMode (PersistenceMode.InnerProperty)]
653 public TableItemStyle InstructionTextStyle {
655 if (instructionTextStyle == null) {
656 instructionTextStyle = new TableItemStyle ();
657 if (IsTrackingViewState) {
658 (instructionTextStyle as IStateManager).TrackViewState ();
661 return instructionTextStyle;
665 [DefaultValue (null)]
666 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
667 [NotifyParentProperty (true)]
668 [PersistenceMode (PersistenceMode.InnerProperty)]
669 public TableItemStyle LabelStyle {
671 if (labelStyle == null) {
672 labelStyle = new TableItemStyle ();
673 if (IsTrackingViewState) {
674 (labelStyle as IStateManager).TrackViewState ();
682 [TemplateContainer (typeof (Login))]
683 [PersistenceMode (PersistenceMode.InnerProperty)]
684 public virtual ITemplate LayoutTemplate {
685 get { return layoutTemplate; }
686 set { layoutTemplate = value; }
690 [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
692 public virtual string LoginButtonImageUrl {
694 object o = ViewState ["LoginButtonImageUrl"];
695 return (o == null) ? String.Empty : (string) o;
699 ViewState.Remove ("LoginButtonImageUrl");
701 ViewState ["LoginButtonImageUrl"] = value;
705 [DefaultValue (null)]
706 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
707 [NotifyParentProperty (true)]
708 [PersistenceMode (PersistenceMode.InnerProperty)]
709 public Style LoginButtonStyle {
711 if (logonButtonStyle == null) {
712 logonButtonStyle = new Style ();
713 if (IsTrackingViewState) {
714 (logonButtonStyle as IStateManager).TrackViewState ();
717 return logonButtonStyle;
722 public virtual string LoginButtonText {
724 object o = ViewState ["LoginButtonText"];
725 return (o == null) ? Locale.GetText ("Log In") : (string) o;
729 ViewState.Remove ("LoginButtonText");
731 ViewState ["LoginButtonText"] = value;
735 [DefaultValue (ButtonType.Button)]
736 public virtual ButtonType LoginButtonType {
738 object o = ViewState ["LoginButtonType"];
739 return (o == null) ? ButtonType.Button : (ButtonType) o;
742 if ((value < ButtonType.Button) || (value > ButtonType.Link))
743 throw new ArgumentOutOfRangeException ("LoginButtonType");
744 ViewState ["LoginButtonType"] = (int) value;
750 public virtual string MembershipProvider {
752 object o = ViewState ["MembershipProvider"];
753 return (o == null) ? String.Empty : (string) o;
757 ViewState.Remove ("MembershipProvider");
759 ViewState ["MembershipProvider"] = value;
763 [DefaultValue (Orientation.Vertical)]
764 public virtual Orientation Orientation {
766 object o = ViewState ["Orientation"];
767 return (o == null) ? Orientation.Vertical : (Orientation) o;
770 if ((value < Orientation.Horizontal) || (value > Orientation.Vertical))
771 throw new ArgumentOutOfRangeException ("Orientation");
772 ViewState ["Orientation"] = (int) value;
777 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
778 public virtual string Password {
780 return _password != null ? _password : String.Empty;
785 public virtual string PasswordLabelText {
787 object o = ViewState ["PasswordLabelText"];
788 return (o == null) ? "Password:" : (string) o;
792 ViewState.Remove ("PasswordLabelText");
794 ViewState ["PasswordLabelText"] = value;
799 [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
801 public virtual string PasswordRecoveryIconUrl {
803 object o = ViewState ["PasswordRecoveryIconUrl"];
804 return (o == null) ? String.Empty : (string) o;
808 ViewState.Remove ("PasswordRecoveryIconUrl");
810 ViewState ["PasswordRecoveryIconUrl"] = value;
816 public virtual string PasswordRecoveryText {
818 object o = ViewState ["PasswordRecoveryText"];
819 return (o == null) ? String.Empty : (string) o;
823 ViewState.Remove ("PasswordRecoveryText");
825 ViewState ["PasswordRecoveryText"] = value;
830 [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
832 public virtual string PasswordRecoveryUrl {
834 object o = ViewState ["PasswordRecoveryUrl"];
835 return (o == null) ? String.Empty : (string) o;
839 ViewState.Remove ("PasswordRecoveryUrl");
841 ViewState ["PasswordRecoveryUrl"] = value;
846 public virtual string PasswordRequiredErrorMessage {
848 object o = ViewState ["PasswordRequiredErrorMessage"];
849 return (o == null) ? Locale.GetText ("Password is required.") : (string) o;
853 ViewState.Remove ("PasswordRequiredErrorMessage");
855 ViewState ["PasswordRequiredErrorMessage"] = value;
859 [DefaultValue (false)]
861 public virtual bool RememberMeSet {
863 object o = ViewState ["RememberMeSet"];
864 return (o == null) ? false : (bool) o;
867 ViewState ["RememberMeSet"] = value;
872 public virtual string RememberMeText {
874 object o = ViewState ["RememberMeText"];
875 return (o == null) ? Locale.GetText ("Remember me next time.") : (string) o;
879 ViewState.Remove ("RememberMeText");
881 ViewState ["RememberMeText"] = value;
885 protected override HtmlTextWriterTag TagKey {
886 get { return HtmlTextWriterTag.Table; }
889 [DefaultValue (null)]
890 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
891 [NotifyParentProperty (true)]
892 [PersistenceMode (PersistenceMode.InnerProperty)]
893 public Style TextBoxStyle {
895 if (textBoxStyle == null) {
896 textBoxStyle = new Style ();
897 if (IsTrackingViewState) {
898 (textBoxStyle as IStateManager).TrackViewState ();
905 [DefaultValue (LoginTextLayout.TextOnLeft)]
906 public virtual LoginTextLayout TextLayout {
908 object o = ViewState ["TextLayout"];
909 return (o == null) ? LoginTextLayout.TextOnLeft : (LoginTextLayout) o;
912 if ((value < LoginTextLayout.TextOnLeft) || (value > LoginTextLayout.TextOnTop))
913 throw new ArgumentOutOfRangeException ("TextLayout");
914 ViewState ["TextLayout"] = (int) value;
919 public virtual string TitleText {
921 object o = ViewState ["TitleText"];
922 return (o == null) ? Locale.GetText ("Log In") : (string) o;
926 ViewState.Remove ("TitleText");
928 ViewState ["TitleText"] = value;
932 [DefaultValue (null)]
933 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
934 [NotifyParentProperty (true)]
935 [PersistenceMode (PersistenceMode.InnerProperty)]
936 public TableItemStyle TitleTextStyle {
938 if (titleTextStyle == null) {
939 titleTextStyle = new TableItemStyle ();
940 if (IsTrackingViewState) {
941 (titleTextStyle as IStateManager).TrackViewState ();
944 return titleTextStyle;
949 public virtual string UserName {
951 object o = ViewState ["UserName"];
952 return (o == null) ? String.Empty : (string) o;
956 ViewState.Remove ("UserName");
958 ViewState ["UserName"] = value;
963 public virtual string UserNameLabelText {
965 object o = ViewState ["UserNameLabelText"];
966 return (o == null) ? Locale.GetText ("User Name:") : (string) o;
970 ViewState.Remove ("UserNameLabelText");
972 ViewState ["UserNameLabelText"] = value;
977 public virtual string UserNameRequiredErrorMessage {
979 object o = ViewState ["UserNameRequiredErrorMessage"];
980 return (o == null) ? Locale.GetText ("User Name is required.") : (string) o;
984 ViewState.Remove ("UserNameRequiredErrorMessage");
986 ViewState ["UserNameRequiredErrorMessage"] = value;
990 [DefaultValue (null)]
991 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
992 [NotifyParentProperty (true)]
993 [PersistenceMode (PersistenceMode.InnerProperty)]
994 public Style ValidatorTextStyle {
996 if (validatorTextStyle == null) {
997 validatorTextStyle = new Style ();
998 if (IsTrackingViewState) {
999 (validatorTextStyle as IStateManager).TrackViewState ();
1002 return validatorTextStyle;
1006 [DefaultValue (true)]
1008 public virtual bool VisibleWhenLoggedIn {
1010 object o = ViewState ["VisibleWhenLoggedIn"];
1011 return (o == null) ? true : (bool) o;
1014 ViewState ["VisibleWhenLoggedIn"] = value;
1018 LoginContainer LoginTemplateContainer
1021 if (container == null)
1022 container = new LoginContainer (this);
1030 protected internal override void CreateChildControls ()
1034 ITemplate template = LayoutTemplate;
1035 if (template == null)
1036 template = new LoginTemplate (this);
1038 LoginTemplateContainer.InstantiateTemplate (template);
1040 Controls.Add (container);
1042 IEditableTextControl editable;
1043 editable = container.UserNameTextBox as IEditableTextControl;
1045 if (editable != null) {
1046 editable.Text = UserName;
1047 editable.TextChanged += new EventHandler (UserName_TextChanged);
1050 throw new HttpException ("LayoutTemplate does not contain an IEditableTextControl with ID UserName for the username.");
1052 editable = container.PasswordTextBox as IEditableTextControl;
1054 if (editable != null)
1055 editable.TextChanged += new EventHandler (Password_TextChanged);
1057 throw new HttpException ("LayoutTemplate does not contain an IEditableTextControl with ID Password for the password.");
1059 ICheckBoxControl checkBox = container.RememberMeCheckBox as ICheckBoxControl;
1061 if (checkBox != null)
1062 checkBox.CheckedChanged += new EventHandler (RememberMe_CheckedChanged);
1065 protected override void LoadViewState (object savedState)
1067 if (savedState == null) {
1068 base.LoadViewState (null);
1072 object[] state = (object[]) savedState;
1073 base.LoadViewState (state [0]);
1074 if (state [1] != null)
1075 (LoginButtonStyle as IStateManager).LoadViewState (state [1]);
1076 if (state [2] != null)
1077 (LabelStyle as IStateManager).LoadViewState (state [2]);
1078 if (state [3] != null)
1079 (TextBoxStyle as IStateManager).LoadViewState (state [3]);
1080 if (state [4] != null)
1081 (HyperLinkStyle as IStateManager).LoadViewState (state [4]);
1082 if (state [5] != null)
1083 (InstructionTextStyle as IStateManager).LoadViewState (state [5]);
1084 if (state [6] != null)
1085 (TitleTextStyle as IStateManager).LoadViewState (state [6]);
1086 if (state [7] != null)
1087 (CheckBoxStyle as IStateManager).LoadViewState (state [7]);
1088 if (state [8] != null)
1089 (FailureTextStyle as IStateManager).LoadViewState (state [8]);
1090 if (state [9] != null)
1091 (ValidatorTextStyle as IStateManager).LoadViewState (state [9]);
1094 private bool HasOnAuthenticateHandler ()
1096 return Events [authenticateEvent] != null;
1099 protected virtual void OnAuthenticate (AuthenticateEventArgs e)
1101 // this gets called after OnLoggingIn and the authentication so we can change the result
1102 AuthenticateEventHandler authenticate = (AuthenticateEventHandler) Events [authenticateEvent];
1103 if (authenticate != null)
1104 authenticate (this, e);
1107 protected override bool OnBubbleEvent (object source, EventArgs e)
1109 // check for submit button
1110 CommandEventArgs cea = (e as CommandEventArgs);
1111 if ((cea != null) &&
1112 String.Equals (cea.CommandName, LoginButtonCommandName, StringComparison.InvariantCultureIgnoreCase)) {
1113 if (!AuthenticateUser ()) {
1114 ITextControl failureText = LoginTemplateContainer.FailureTextLiteral;
1115 if (failureText != null)
1116 failureText.Text = FailureText;
1123 protected virtual void OnLoggedIn (EventArgs e)
1125 // this gets called only if the authentication was successful
1126 EventHandler loggedIn = (EventHandler) Events [loggedInEvent];
1127 if (loggedIn != null)
1131 protected virtual void OnLoggingIn (LoginCancelEventArgs e)
1133 // this gets called before OnAuthenticate so we can abort the authentication process
1134 LoginCancelEventHandler loggingIn = (LoginCancelEventHandler) Events [loggingInEvent];
1135 if (loggingIn != null)
1136 loggingIn (this, e);
1139 protected virtual void OnLoginError (EventArgs e)
1141 // this gets called only if the authentication wasn't successful
1142 EventHandler loginError = (EventHandler) Events [loginErrorEvent];
1143 if (loginError != null)
1144 loginError (this, e);
1147 [MonoTODO ("overriden for ?")]
1148 protected internal override void OnPreRender (EventArgs e)
1150 base.OnPreRender (e);
1151 // note: doc says that UserName and Password aren't available at
1152 // PageLoad but are during PreRender phase, so... ???
1155 protected internal override void Render (HtmlTextWriter writer)
1157 // VisibleWhenLoggedIn isn't applicable to the default login page
1158 if (!VisibleWhenLoggedIn && !IsDefaultLoginPage () && IsLoggedIn ())
1162 Page.VerifyRenderingInServerForm (this);
1165 EnsureChildControls ();
1167 foreach (object [] styleDef in styles)
1168 ((WebControl) styleDef [0]).ApplyStyle ((Style) styleDef [1]);
1170 RenderContents(writer);
1173 protected override object SaveViewState ()
1175 object[] state = new object [10];
1176 state [0] = base.SaveViewState ();
1177 if (logonButtonStyle != null)
1178 state [1] = (logonButtonStyle as IStateManager).SaveViewState ();
1179 if (labelStyle != null)
1180 state [2] = (labelStyle as IStateManager).SaveViewState ();
1181 if (textBoxStyle != null)
1182 state [3] = (textBoxStyle as IStateManager).SaveViewState ();
1183 if (hyperLinkStyle != null)
1184 state [4] = (hyperLinkStyle as IStateManager).SaveViewState ();
1185 if (instructionTextStyle != null)
1186 state [5] = (instructionTextStyle as IStateManager).SaveViewState ();
1187 if (titleTextStyle != null)
1188 state [6] = (titleTextStyle as IStateManager).SaveViewState ();
1189 if (checkBoxStyle != null)
1190 state [7] = (checkBoxStyle as IStateManager).SaveViewState ();
1191 if (failureTextStyle != null)
1192 state [8] = (failureTextStyle as IStateManager).SaveViewState ();
1193 if (validatorTextStyle != null)
1194 state [9] = (validatorTextStyle as IStateManager).SaveViewState ();
1196 for (int i=0; i < state.Length; i++) {
1197 if (state [0] != null)
1198 return (object) state;
1200 return null; // reduce view state
1203 [MonoTODO ("for design-time usage - no more details available")]
1204 protected override void SetDesignModeState (IDictionary data)
1206 base.SetDesignModeState (data);
1209 protected override void TrackViewState ()
1211 base.TrackViewState ();
1212 if (logonButtonStyle != null)
1213 (logonButtonStyle as IStateManager).TrackViewState ();
1214 if (labelStyle != null)
1215 (labelStyle as IStateManager).TrackViewState ();
1216 if (textBoxStyle != null)
1217 (textBoxStyle as IStateManager).TrackViewState ();
1218 if (hyperLinkStyle != null)
1219 (hyperLinkStyle as IStateManager).TrackViewState ();
1220 if (instructionTextStyle != null)
1221 (instructionTextStyle as IStateManager).TrackViewState ();
1222 if (titleTextStyle != null)
1223 (titleTextStyle as IStateManager).TrackViewState ();
1224 if (checkBoxStyle != null)
1225 (checkBoxStyle as IStateManager).TrackViewState ();
1226 if (failureTextStyle != null)
1227 (failureTextStyle as IStateManager).TrackViewState ();
1228 if (validatorTextStyle != null)
1229 (validatorTextStyle as IStateManager).TrackViewState ();
1235 public event AuthenticateEventHandler Authenticate {
1236 add { Events.AddHandler (authenticateEvent, value); }
1237 remove { Events.RemoveHandler (authenticateEvent, value); }
1240 public event EventHandler LoggedIn {
1241 add { Events.AddHandler (loggedInEvent, value); }
1242 remove { Events.RemoveHandler (loggedInEvent, value); }
1245 public event LoginCancelEventHandler LoggingIn {
1246 add { Events.AddHandler (loggingInEvent, value); }
1247 remove { Events.RemoveHandler (loggingInEvent, value); }
1250 public event EventHandler LoginError {
1251 add { Events.AddHandler (loginErrorEvent, value); }
1252 remove { Events.RemoveHandler (loginErrorEvent, value); }
1258 internal void RegisterApplyStyle (WebControl control, Style style)
1260 styles.Add (new object [] { control, style });
1263 private bool AuthenticateUser ()
1268 LoginCancelEventArgs lcea = new LoginCancelEventArgs ();
1273 AuthenticateEventArgs aea = new AuthenticateEventArgs ();
1275 if (!HasOnAuthenticateHandler ()) {
1276 string mp = MembershipProvider;
1277 MembershipProvider provider = (mp.Length == 0) ?
1278 provider = Membership.Provider : Membership.Providers [mp];
1279 if (provider == null) {
1280 throw new HttpException (Locale.GetText ("No provider named '{0}' could be found.", mp));
1283 aea.Authenticated = provider.ValidateUser (UserName, Password);
1285 OnAuthenticate (aea);
1287 if (aea.Authenticated) {
1288 FormsAuthentication.SetAuthCookie (UserName, RememberMeSet);
1289 OnLoggedIn (EventArgs.Empty);
1291 string url = DestinationPageUrl;
1292 if (Page.Request.Path.StartsWith (FormsAuthentication.LoginUrl, StringComparison.InvariantCultureIgnoreCase)) {
1293 if (!String.IsNullOrEmpty (FormsAuthentication.ReturnUrl))
1294 Redirect (FormsAuthentication.ReturnUrl);
1295 else if (!String.IsNullOrEmpty (DestinationPageUrl))
1297 else if (!String.IsNullOrEmpty (FormsAuthentication.DefaultUrl))
1298 Redirect (FormsAuthentication.DefaultUrl);
1299 else if (url.Length == 0)
1302 else if (!String.IsNullOrEmpty (DestinationPageUrl)) {
1311 OnLoginError (EventArgs.Empty);
1312 if (FailureAction == LoginFailureAction.RedirectToLoginPage) {
1313 // login page is defined in web.config
1314 FormsAuthentication.RedirectToLoginPage ();
1320 // TODO: its called from default template only, not usefully, OnBubbleEvent
1321 // do handle command, need be removed
1323 private void LoginClick (object sender, CommandEventArgs e)
1325 RaiseBubbleEvent (sender, (EventArgs)e);
1328 private bool IsDefaultLoginPage ()
1330 if ((Page == null) || (Page.Request == null))
1332 string defaultLogin = FormsAuthentication.LoginUrl;
1333 if (defaultLogin == null)
1335 string url = Page.Request.Url.AbsolutePath;
1336 return (String.Compare (defaultLogin, 0, url, url.Length - defaultLogin.Length, defaultLogin.Length,
1337 true, CultureInfo.InvariantCulture) == 0);
1340 private bool IsLoggedIn ()
1342 if ((Page == null) || (Page.Request == null))
1344 return Page.Request.IsAuthenticated;
1347 private void Redirect (string url)
1349 if ((Page != null) && (Page.Response != null))
1350 Page.Response.Redirect (url);
1353 private void Refresh () {
1354 if ((Page != null) && (Page.Response != null))
1355 Page.Response.Redirect (Page.Request.RawUrl);
1358 private void UserName_TextChanged (object sender, EventArgs e)
1360 UserName = ((ITextControl)sender).Text;
1363 private void Password_TextChanged (object sender, EventArgs e)
1365 _password = ((ITextControl)sender).Text;
1368 private void RememberMe_CheckedChanged (object sender, EventArgs e)
1370 RememberMeSet = ((ICheckBoxControl)sender).Checked;