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)
63 public void InstantiateTemplate (ITemplate template)
65 template.InstantiateIn (_containerCell);
68 private void InitTable ()
71 CellPadding = _owner.BorderPadding;
73 _containerCell = new TableCell ();
75 TableRow row = new TableRow ();
76 row.Cells.Add (_containerCell);
80 protected internal override void OnPreRender (EventArgs e)
82 ApplyStyle (_owner.ControlStyle);
86 public Control UserNameTextBox {
88 return FindControl("UserName");
92 public Control PasswordTextBox {
94 return FindControl("Password");
98 public Control RememberMeCheckBox {
100 return FindControl("RememberMe");
104 public ITextControl FailureTextLiteral
107 return FindControl ("FailureText") as ITextControl;
114 #region LoginTemplate
116 sealed class LoginTemplate : WebControl, ITemplate
118 readonly Login _login;
120 public LoginTemplate (Login login)
125 void ITemplate.InstantiateIn (Control container)
128 LiteralControl TitleText = new LiteralControl (_login.TitleText);
131 LiteralControl InstructionText = new LiteralControl (_login.InstructionText);
134 TextBox UserName = new TextBox ();
135 UserName.ID = "UserName";
136 UserName.Text = _login.UserName;
137 _login.RegisterApplyStyle (UserName, _login.TextBoxStyle);
139 Label UserNameLabel = new Label ();
140 UserNameLabel.ID = "UserNameLabel";
141 UserNameLabel.AssociatedControlID = "UserName";
142 UserNameLabel.Text = _login.UserNameLabelText;
144 RequiredFieldValidator UserNameRequired = new RequiredFieldValidator ();
145 UserNameRequired.ID = "UserNameRequired";
146 UserNameRequired.ControlToValidate = "UserName";
147 UserNameRequired.ErrorMessage = _login.UserNameRequiredErrorMessage;
148 UserNameRequired.ToolTip = _login.UserNameRequiredErrorMessage;
149 UserNameRequired.Text = "*";
150 UserNameRequired.ValidationGroup = _login.ID;
151 _login.RegisterApplyStyle (UserNameRequired, _login.ValidatorTextStyle);
154 TextBox Password = new TextBox ();
155 Password.ID = "Password";
156 Password.TextMode = TextBoxMode.Password;
157 _login.RegisterApplyStyle (Password, _login.TextBoxStyle);
159 Label PasswordLabel = new Label ();
160 PasswordLabel.ID = "PasswordLabel";
161 PasswordLabel.AssociatedControlID = "PasswordLabel";
162 PasswordLabel.Text = _login.PasswordLabelText;
164 RequiredFieldValidator PasswordRequired = new RequiredFieldValidator ();
165 PasswordRequired.ID = "PasswordRequired";
166 PasswordRequired.ControlToValidate = "Password";
167 PasswordRequired.ErrorMessage = _login.PasswordRequiredErrorMessage;
168 PasswordRequired.ToolTip = _login.PasswordRequiredErrorMessage;
169 PasswordRequired.Text = "*";
170 PasswordRequired.ValidationGroup = _login.ID;
171 _login.RegisterApplyStyle (PasswordRequired, _login.ValidatorTextStyle);
174 CheckBox RememberMe = new CheckBox ();
175 RememberMe.ID = "RememberMe";
176 RememberMe.Checked = _login.RememberMeSet;
177 RememberMe.Text = _login.RememberMeText;
178 _login.RegisterApplyStyle (RememberMe, _login.CheckBoxStyle);
181 Literal FailureText = new Literal ();
182 FailureText.ID = "FailureText";
183 FailureText.EnableViewState = false;
186 WebControl LoginButton = null;
187 switch (_login.LoginButtonType) {
188 case ButtonType.Button:
189 LoginButton = new Button ();
190 LoginButton.ID = "LoginButton";
192 case ButtonType.Link:
193 LoginButton = new LinkButton ();
194 LoginButton.ID = "LoginLinkButton";
196 case ButtonType.Image:
197 LoginButton = new ImageButton ();
198 LoginButton.ID = "LoginImageButton";
201 _login.RegisterApplyStyle (LoginButton, _login.LoginButtonStyle);
202 LoginButton.ID = "LoginButton";
203 ((IButtonControl) LoginButton).Text = _login.LoginButtonText;
204 ((IButtonControl) LoginButton).CommandName = Login.LoginButtonCommandName;
205 ((IButtonControl) LoginButton).Command += new CommandEventHandler (_login.LoginClick);
206 ((IButtonControl) LoginButton).ValidationGroup = _login.ID;
208 // Create layout table
209 Table table = new Table ();
210 table.CellPadding = 0;
215 CreateCell (TitleText, null, _login.TitleTextStyle, HorizontalAlign.Center)));
218 if (_login.InstructionText.Length > 0) {
221 CreateCell (InstructionText, null, _login.instructionTextStyle, HorizontalAlign.Center)));
224 if (_login.Orientation == Orientation.Horizontal) {
226 TableRow row1 = new TableRow ();
227 TableRow row2 = new TableRow ();
228 if (_login.TextLayout == LoginTextLayout.TextOnTop)
229 row1.Cells.Add (CreateCell (UserNameLabel, null, _login.LabelStyle));
231 row2.Cells.Add (CreateCell (UserNameLabel, null, _login.LabelStyle));
232 row2.Cells.Add (CreateCell (UserName, UserNameRequired, null));
233 if (_login.TextLayout == LoginTextLayout.TextOnTop)
234 row1.Cells.Add (CreateCell (PasswordLabel, null, _login.LabelStyle));
236 row2.Cells.Add (CreateCell (PasswordLabel, null, _login.LabelStyle));
237 row2.Cells.Add (CreateCell (Password, PasswordRequired, null));
238 row2.Cells.Add (CreateCell (RememberMe, null, null));
239 row2.Cells.Add (CreateCell (LoginButton, null, null));
240 if (row1.Cells.Count > 0)
241 table.Rows.Add (row1);
242 table.Rows.Add (row2);
244 else { // Orientation.Vertical
245 if (_login.TextLayout == LoginTextLayout.TextOnLeft)
246 table.Rows.Add (CreateRow (UserNameLabel, UserName, UserNameRequired, _login.LabelStyle));
248 table.Rows.Add (CreateRow (UserNameLabel, null, null, _login.LabelStyle));
249 table.Rows.Add (CreateRow (null, UserName, UserNameRequired, null));
251 if (_login.TextLayout == LoginTextLayout.TextOnLeft)
252 table.Rows.Add (CreateRow (PasswordLabel, Password, PasswordRequired, _login.LabelStyle));
254 table.Rows.Add (CreateRow (PasswordLabel, null, null, _login.LabelStyle));
255 table.Rows.Add (CreateRow (null, Password, PasswordRequired, null));
257 table.Rows.Add (CreateRow (CreateCell (RememberMe, null, null)));
258 table.Rows.Add (CreateRow (CreateCell (LoginButton, null, null, HorizontalAlign.Right)));
262 if (_login.FailureTextStyle.ForeColor.IsEmpty)
263 _login.FailureTextStyle.ForeColor = System.Drawing.Color.Red;
267 CreateCell (FailureText, null, _login.FailureTextStyle)));
270 TableCell linksCell = new TableCell ();
271 _login.RegisterApplyStyle (linksCell, _login.HyperLinkStyle);
273 if (AddLink (_login.CreateUserUrl, _login.CreateUserText, _login.CreateUserIconUrl, linksCell, _login.HyperLinkStyle))
274 if (_login.Orientation == Orientation.Vertical)
275 linksCell.Controls.Add (new LiteralControl ("<br/>"));
277 linksCell.Controls.Add (new LiteralControl (" "));
279 if (AddLink (_login.PasswordRecoveryUrl, _login.PasswordRecoveryText, _login.PasswordRecoveryIconUrl, linksCell, _login.HyperLinkStyle))
280 if (_login.Orientation == Orientation.Vertical)
281 linksCell.Controls.Add (new LiteralControl ("<br/>"));
283 linksCell.Controls.Add (new LiteralControl (" "));
285 AddLink (_login.HelpPageUrl, _login.HelpPageText, _login.HelpPageIconUrl, linksCell, _login.HyperLinkStyle);
286 table.Rows.Add (CreateRow (linksCell));
288 FixTableColumnSpans (table);
289 container.Controls.Add (table);
292 private TableRow CreateRow (TableCell cell)
294 TableRow row = new TableRow ();
295 row.Cells.Add (cell);
299 TableRow CreateRow (Control c0, Control c1, Control c2, Style s)
301 TableRow row = new TableRow ();
302 TableCell cell0 = new TableCell ();
303 TableCell cell1 = new TableCell ();
306 cell0.Controls.Add (c0);
307 row.Controls.Add (cell0);
311 cell0.ApplyStyle (s);
313 if ((c1 != null) && (c2 != null)) {
314 cell1.Controls.Add (c1);
315 cell1.Controls.Add (c2);
316 cell0.HorizontalAlign = HorizontalAlign.Right;
317 row.Controls.Add (cell1);
322 private TableCell CreateCell (Control c0, Control c1, Style s, HorizontalAlign align)
324 TableCell cell = CreateCell (c0, c1, s);
325 cell.HorizontalAlign = align;
329 private TableCell CreateCell (Control c0, Control c1, Style s)
331 TableCell cell = new TableCell ();
335 cell.Controls.Add (c0);
337 cell.Controls.Add (c1);
342 bool AddLink (string pageUrl, string linkText, string linkIcon, WebControl container, Style style)
345 if (linkIcon.Length > 0) {
346 Image img = new Image ();
347 img.ImageUrl = linkIcon;
348 container.Controls.Add (img);
351 if (linkText.Length > 0) {
352 HyperLink link = new HyperLink ();
353 link.NavigateUrl = pageUrl;
354 link.Text = linkText;
355 _login.RegisterApplyStyle (link, style);
356 container.Controls.Add (link);
362 void FixTableColumnSpans (Table table)
365 for (int row = 0; row < table.Rows.Count; row++) {
366 if (maxCols < table.Rows [row].Cells.Count)
367 maxCols = table.Rows [row].Cells.Count;
369 for (int row = 0; row < table.Rows.Count; row++) {
370 if (table.Rows [row].Cells.Count == 1 && maxCols > 1)
371 table.Rows [row].Cells [0].ColumnSpan = maxCols;
378 public static readonly string LoginButtonCommandName = "Login";
380 private static readonly object authenticateEvent = new object ();
381 private static readonly object loggedInEvent = new object ();
382 private static readonly object loggingInEvent = new object ();
383 private static readonly object loginErrorEvent = new object ();
385 private TableItemStyle checkBoxStyle;
386 private TableItemStyle failureTextStyle;
387 private TableItemStyle hyperLinkStyle;
388 private TableItemStyle instructionTextStyle;
389 private TableItemStyle labelStyle;
390 private Style logonButtonStyle;
391 private Style textBoxStyle;
392 private TableItemStyle titleTextStyle;
393 private Style validatorTextStyle;
394 private ArrayList styles = new ArrayList ();
396 private ITemplate layoutTemplate;
397 private LoginContainer container;
399 private string _password;
408 public virtual int BorderPadding {
410 object o = ViewState ["BorderPadding"];
411 return (o == null) ? 1 : (int) o;
415 throw new ArgumentOutOfRangeException ("BorderPadding", "< -1");
417 ViewState ["BorderPadding"] = value;
421 [DefaultValue (null)]
422 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
423 [NotifyParentProperty (true)]
424 [PersistenceMode (PersistenceMode.InnerProperty)]
425 public TableItemStyle CheckBoxStyle {
427 if (checkBoxStyle == null) {
428 checkBoxStyle = new TableItemStyle ();
429 if (IsTrackingViewState) {
430 (checkBoxStyle as IStateManager).TrackViewState ();
433 return checkBoxStyle;
438 [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
440 public virtual string CreateUserIconUrl {
442 object o = ViewState ["CreateUserIconUrl"];
443 return (o == null) ? String.Empty : (string) o;
447 ViewState.Remove ("CreateUserIconUrl");
449 ViewState ["CreateUserIconUrl"] = value;
455 public virtual string CreateUserText {
457 object o = ViewState ["CreateUserText"];
458 return (o == null) ? String.Empty : (string) o;
462 ViewState.Remove ("CreateUserText");
464 ViewState ["CreateUserText"] = value;
469 [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
471 public virtual string CreateUserUrl {
473 object o = ViewState ["CreateUserUrl"];
474 return (o == null) ? String.Empty : (string) o;
478 ViewState.Remove ("CreateUserUrl");
480 ViewState ["CreateUserUrl"] = value;
485 [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
488 public virtual string DestinationPageUrl {
490 object o = ViewState ["DestinationPageUrl"];
491 return (o == null) ? String.Empty : (string) o;
495 ViewState.Remove ("DestinationPageUrl");
497 ViewState ["DestinationPageUrl"] = value;
501 [DefaultValue (true)]
503 public virtual bool DisplayRememberMe {
505 object o = ViewState ["DisplayRememberMe"];
506 return (o == null) ? true : (bool) o;
509 ViewState ["DisplayRememberMe"] = value;
513 [DefaultValue (LoginFailureAction.Refresh)]
515 [MonoTODO ("RedirectToLoginPage not yet implemented in FormsAuthentication")]
516 public virtual LoginFailureAction FailureAction {
518 object o = ViewState ["FailureAction"];
519 return (o == null) ? LoginFailureAction.Refresh : (LoginFailureAction) o;
522 if ((value < LoginFailureAction.Refresh) || (value > LoginFailureAction.RedirectToLoginPage))
523 throw new ArgumentOutOfRangeException ("FailureAction");
524 ViewState ["FailureAction"] = (int) value;
529 public virtual string FailureText {
531 object o = ViewState ["FailureText"];
532 return (o == null) ? Locale.GetText ("Your login attempt was not successful. Please try again.") : (string) o;
536 ViewState.Remove ("FailureText");
538 ViewState ["FailureText"] = value;
542 [DefaultValue (null)]
543 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
544 [NotifyParentProperty (true)]
545 [PersistenceMode (PersistenceMode.InnerProperty)]
546 public TableItemStyle FailureTextStyle {
548 if (failureTextStyle == null) {
549 failureTextStyle = new TableItemStyle ();
550 if (IsTrackingViewState) {
551 (failureTextStyle as IStateManager).TrackViewState ();
554 return failureTextStyle;
559 [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
561 public virtual string HelpPageIconUrl {
563 object o = ViewState ["HelpPageIconUrl"];
564 return (o == null) ? String.Empty : (string) o;
568 ViewState.Remove ("HelpPageIconUrl");
570 ViewState ["HelpPageIconUrl"] = value;
576 public virtual string HelpPageText {
578 object o = ViewState ["HelpPageText"];
579 return (o == null) ? String.Empty : (string) o;
583 ViewState.Remove ("HelpPageText");
585 ViewState ["HelpPageText"] = value;
590 [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
592 public virtual string HelpPageUrl {
594 object o = ViewState ["HelpPageUrl"];
595 return (o == null) ? String.Empty : (string) o;
599 ViewState.Remove ("HelpPageUrl");
601 ViewState ["HelpPageUrl"] = value;
605 [DefaultValue (null)]
606 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
607 [NotifyParentProperty (true)]
608 [PersistenceMode (PersistenceMode.InnerProperty)]
609 public TableItemStyle HyperLinkStyle {
611 if (hyperLinkStyle == null) {
612 hyperLinkStyle = new TableItemStyle ();
613 if (IsTrackingViewState) {
614 (hyperLinkStyle as IStateManager).TrackViewState ();
617 return hyperLinkStyle;
623 public virtual string InstructionText {
625 object o = ViewState ["InstructionText"];
626 return (o == null) ? String.Empty : (string) o;
630 ViewState.Remove ("InstructionText");
632 ViewState ["InstructionText"] = value;
636 [DefaultValue (null)]
637 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
638 [NotifyParentProperty (true)]
639 [PersistenceMode (PersistenceMode.InnerProperty)]
640 public TableItemStyle InstructionTextStyle {
642 if (instructionTextStyle == null) {
643 instructionTextStyle = new TableItemStyle ();
644 if (IsTrackingViewState) {
645 (instructionTextStyle as IStateManager).TrackViewState ();
648 return instructionTextStyle;
652 [DefaultValue (null)]
653 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
654 [NotifyParentProperty (true)]
655 [PersistenceMode (PersistenceMode.InnerProperty)]
656 public TableItemStyle LabelStyle {
658 if (labelStyle == null) {
659 labelStyle = new TableItemStyle ();
660 if (IsTrackingViewState) {
661 (labelStyle as IStateManager).TrackViewState ();
669 [TemplateContainer (typeof (Login))]
670 [PersistenceMode (PersistenceMode.InnerProperty)]
671 public virtual ITemplate LayoutTemplate {
672 get { return layoutTemplate; }
673 set { layoutTemplate = value; }
677 [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
679 public virtual string LoginButtonImageUrl {
681 object o = ViewState ["LoginButtonImageUrl"];
682 return (o == null) ? String.Empty : (string) o;
686 ViewState.Remove ("LoginButtonImageUrl");
688 ViewState ["LoginButtonImageUrl"] = value;
692 [DefaultValue (null)]
693 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
694 [NotifyParentProperty (true)]
695 [PersistenceMode (PersistenceMode.InnerProperty)]
696 public Style LoginButtonStyle {
698 if (logonButtonStyle == null) {
699 logonButtonStyle = new Style ();
700 if (IsTrackingViewState) {
701 (logonButtonStyle as IStateManager).TrackViewState ();
704 return logonButtonStyle;
709 public virtual string LoginButtonText {
711 object o = ViewState ["LoginButtonText"];
712 return (o == null) ? Locale.GetText ("Log In") : (string) o;
716 ViewState.Remove ("LoginButtonText");
718 ViewState ["LoginButtonText"] = value;
722 [DefaultValue (ButtonType.Button)]
723 public virtual ButtonType LoginButtonType {
725 object o = ViewState ["LoginButtonType"];
726 return (o == null) ? ButtonType.Button : (ButtonType) o;
729 if ((value < ButtonType.Button) || (value > ButtonType.Link))
730 throw new ArgumentOutOfRangeException ("LoginButtonType");
731 ViewState ["LoginButtonType"] = (int) value;
737 public virtual string MembershipProvider {
739 object o = ViewState ["MembershipProvider"];
740 return (o == null) ? String.Empty : (string) o;
744 ViewState.Remove ("MembershipProvider");
746 ViewState ["MembershipProvider"] = value;
750 [DefaultValue (Orientation.Vertical)]
751 public virtual Orientation Orientation {
753 object o = ViewState ["Orientation"];
754 return (o == null) ? Orientation.Vertical : (Orientation) o;
757 if ((value < Orientation.Horizontal) || (value > Orientation.Vertical))
758 throw new ArgumentOutOfRangeException ("Orientation");
759 ViewState ["Orientation"] = (int) value;
764 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
765 public virtual string Password {
767 return _password != null ? _password : String.Empty;
772 public virtual string PasswordLabelText {
774 object o = ViewState ["PasswordLabelText"];
775 return (o == null) ? "Password:" : (string) o;
779 ViewState.Remove ("PasswordLabelText");
781 ViewState ["PasswordLabelText"] = value;
786 [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
788 public virtual string PasswordRecoveryIconUrl {
790 object o = ViewState ["PasswordRecoveryIconUrl"];
791 return (o == null) ? String.Empty : (string) o;
795 ViewState.Remove ("PasswordRecoveryIconUrl");
797 ViewState ["PasswordRecoveryIconUrl"] = value;
803 public virtual string PasswordRecoveryText {
805 object o = ViewState ["PasswordRecoveryText"];
806 return (o == null) ? String.Empty : (string) o;
810 ViewState.Remove ("PasswordRecoveryText");
812 ViewState ["PasswordRecoveryText"] = value;
817 [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
819 public virtual string PasswordRecoveryUrl {
821 object o = ViewState ["PasswordRecoveryUrl"];
822 return (o == null) ? String.Empty : (string) o;
826 ViewState.Remove ("PasswordRecoveryUrl");
828 ViewState ["PasswordRecoveryUrl"] = value;
833 public virtual string PasswordRequiredErrorMessage {
835 object o = ViewState ["PasswordRequiredErrorMessage"];
836 return (o == null) ? Locale.GetText ("Password is required.") : (string) o;
840 ViewState.Remove ("PasswordRequiredErrorMessage");
842 ViewState ["PasswordRequiredErrorMessage"] = value;
846 [DefaultValue (false)]
848 public virtual bool RememberMeSet {
850 object o = ViewState ["RememberMeSet"];
851 return (o == null) ? false : (bool) o;
854 ViewState ["RememberMeSet"] = value;
859 public virtual string RememberMeText {
861 object o = ViewState ["RememberMeText"];
862 return (o == null) ? Locale.GetText ("Remember me next time.") : (string) o;
866 ViewState.Remove ("RememberMeText");
868 ViewState ["RememberMeText"] = value;
872 protected override HtmlTextWriterTag TagKey {
873 get { return HtmlTextWriterTag.Table; }
876 [DefaultValue (null)]
877 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
878 [NotifyParentProperty (true)]
879 [PersistenceMode (PersistenceMode.InnerProperty)]
880 public Style TextBoxStyle {
882 if (textBoxStyle == null) {
883 textBoxStyle = new Style ();
884 if (IsTrackingViewState) {
885 (textBoxStyle as IStateManager).TrackViewState ();
892 [DefaultValue (LoginTextLayout.TextOnLeft)]
893 public virtual LoginTextLayout TextLayout {
895 object o = ViewState ["TextLayout"];
896 return (o == null) ? LoginTextLayout.TextOnLeft : (LoginTextLayout) o;
899 if ((value < LoginTextLayout.TextOnLeft) || (value > LoginTextLayout.TextOnTop))
900 throw new ArgumentOutOfRangeException ("TextLayout");
901 ViewState ["TextLayout"] = (int) value;
906 public virtual string TitleText {
908 object o = ViewState ["TitleText"];
909 return (o == null) ? Locale.GetText ("Log In") : (string) o;
913 ViewState.Remove ("TitleText");
915 ViewState ["TitleText"] = value;
919 [DefaultValue (null)]
920 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
921 [NotifyParentProperty (true)]
922 [PersistenceMode (PersistenceMode.InnerProperty)]
923 public TableItemStyle TitleTextStyle {
925 if (titleTextStyle == null) {
926 titleTextStyle = new TableItemStyle ();
927 if (IsTrackingViewState) {
928 (titleTextStyle as IStateManager).TrackViewState ();
931 return titleTextStyle;
936 public virtual string UserName {
938 object o = ViewState ["UserName"];
939 return (o == null) ? String.Empty : (string) o;
943 ViewState.Remove ("UserName");
945 ViewState ["UserName"] = value;
950 public virtual string UserNameLabelText {
952 object o = ViewState ["UserNameLabelText"];
953 return (o == null) ? Locale.GetText ("User Name:") : (string) o;
957 ViewState.Remove ("UserNameLabelText");
959 ViewState ["UserNameLabelText"] = value;
964 public virtual string UserNameRequiredErrorMessage {
966 object o = ViewState ["UserNameRequiredErrorMessage"];
967 return (o == null) ? Locale.GetText ("User Name is required.") : (string) o;
971 ViewState.Remove ("UserNameRequiredErrorMessage");
973 ViewState ["UserNameRequiredErrorMessage"] = value;
977 [DefaultValue (null)]
978 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
979 [NotifyParentProperty (true)]
980 [PersistenceMode (PersistenceMode.InnerProperty)]
981 public Style ValidatorTextStyle {
983 if (validatorTextStyle == null) {
984 validatorTextStyle = new Style ();
985 if (IsTrackingViewState) {
986 (validatorTextStyle as IStateManager).TrackViewState ();
989 return validatorTextStyle;
993 [DefaultValue (true)]
995 public virtual bool VisibleWhenLoggedIn {
997 object o = ViewState ["VisibleWhenLoggedIn"];
998 return (o == null) ? true : (bool) o;
1001 ViewState ["VisibleWhenLoggedIn"] = value;
1005 LoginContainer LoginTemplateContainer
1008 if (container == null)
1009 container = new LoginContainer (this);
1017 protected internal override void CreateChildControls ()
1021 ITemplate template = LayoutTemplate;
1022 if (template == null)
1023 template = new LoginTemplate (this);
1025 LoginTemplateContainer.InstantiateTemplate (template);
1027 Controls.Add (container);
1029 IEditableTextControl editable;
1030 editable = container.UserNameTextBox as IEditableTextControl;
1032 if (editable != null) {
1033 editable.Text = UserName;
1034 editable.TextChanged += new EventHandler (UserName_TextChanged);
1037 throw new HttpException ("LayoutTemplate does not contain an IEditableTextControl with ID UserName for the username.");
1039 editable = container.PasswordTextBox as IEditableTextControl;
1041 if (editable != null)
1042 editable.TextChanged += new EventHandler (Password_TextChanged);
1044 throw new HttpException ("LayoutTemplate does not contain an IEditableTextControl with ID Password for the password.");
1046 ICheckBoxControl checkBox = container.RememberMeCheckBox as ICheckBoxControl;
1048 if (checkBox != null)
1049 checkBox.CheckedChanged += new EventHandler (RememberMe_CheckedChanged);
1052 protected override void LoadViewState (object savedState)
1054 if (savedState == null) {
1055 base.LoadViewState (null);
1059 object[] state = (object[]) savedState;
1060 base.LoadViewState (state [0]);
1061 if (state [1] != null)
1062 (LoginButtonStyle as IStateManager).LoadViewState (state [1]);
1063 if (state [2] != null)
1064 (LabelStyle as IStateManager).LoadViewState (state [2]);
1065 if (state [3] != null)
1066 (TextBoxStyle as IStateManager).LoadViewState (state [3]);
1067 if (state [4] != null)
1068 (HyperLinkStyle as IStateManager).LoadViewState (state [4]);
1069 if (state [5] != null)
1070 (InstructionTextStyle as IStateManager).LoadViewState (state [5]);
1071 if (state [6] != null)
1072 (TitleTextStyle as IStateManager).LoadViewState (state [6]);
1073 if (state [7] != null)
1074 (CheckBoxStyle as IStateManager).LoadViewState (state [7]);
1075 if (state [8] != null)
1076 (FailureTextStyle as IStateManager).LoadViewState (state [8]);
1077 if (state [9] != null)
1078 (ValidatorTextStyle as IStateManager).LoadViewState (state [9]);
1081 private bool HasOnAuthenticateHandler ()
1083 return Events [authenticateEvent] != null;
1086 protected virtual void OnAuthenticate (AuthenticateEventArgs e)
1088 // this gets called after OnLoggingIn and the authentication so we can change the result
1089 AuthenticateEventHandler authenticate = (AuthenticateEventHandler) Events [authenticateEvent];
1090 if (authenticate != null)
1091 authenticate (this, e);
1094 protected override bool OnBubbleEvent (object source, EventArgs e)
1096 // check for submit button
1097 CommandEventArgs cea = (e as CommandEventArgs);
1098 if ((cea != null) &&
1099 String.Equals (cea.CommandName, LoginButtonCommandName, StringComparison.InvariantCultureIgnoreCase)) {
1100 if (!AuthenticateUser ()) {
1101 ITextControl failureText = LoginTemplateContainer.FailureTextLiteral;
1102 if (failureText != null)
1103 failureText.Text = FailureText;
1110 protected virtual void OnLoggedIn (EventArgs e)
1112 // this gets called only if the authentication was successful
1113 EventHandler loggedIn = (EventHandler) Events [loggedInEvent];
1114 if (loggedIn != null)
1118 protected virtual void OnLoggingIn (LoginCancelEventArgs e)
1120 // this gets called before OnAuthenticate so we can abort the authentication process
1121 LoginCancelEventHandler loggingIn = (LoginCancelEventHandler) Events [loggingInEvent];
1122 if (loggingIn != null)
1123 loggingIn (this, e);
1126 protected virtual void OnLoginError (EventArgs e)
1128 // this gets called only if the authentication wasn't successful
1129 EventHandler loginError = (EventHandler) Events [loginErrorEvent];
1130 if (loginError != null)
1131 loginError (this, e);
1134 [MonoTODO ("overriden for ?")]
1135 protected internal override void OnPreRender (EventArgs e)
1137 base.OnPreRender (e);
1138 // note: doc says that UserName and Password aren't available at
1139 // PageLoad but are during PreRender phase, so... ???
1142 protected internal override void Render (HtmlTextWriter writer)
1144 // VisibleWhenLoggedIn isn't applicable to the default login page
1145 if (!VisibleWhenLoggedIn && !IsDefaultLoginPage () && IsLoggedIn ())
1149 Page.VerifyRenderingInServerForm (this);
1152 EnsureChildControls ();
1154 foreach (object [] styleDef in styles)
1155 ((WebControl) styleDef [0]).ApplyStyle ((Style) styleDef [1]);
1157 RenderContents(writer);
1160 protected override object SaveViewState ()
1162 object[] state = new object [10];
1163 state [0] = base.SaveViewState ();
1164 if (logonButtonStyle != null)
1165 state [1] = (logonButtonStyle as IStateManager).SaveViewState ();
1166 if (labelStyle != null)
1167 state [2] = (labelStyle as IStateManager).SaveViewState ();
1168 if (textBoxStyle != null)
1169 state [3] = (textBoxStyle as IStateManager).SaveViewState ();
1170 if (hyperLinkStyle != null)
1171 state [4] = (hyperLinkStyle as IStateManager).SaveViewState ();
1172 if (instructionTextStyle != null)
1173 state [5] = (instructionTextStyle as IStateManager).SaveViewState ();
1174 if (titleTextStyle != null)
1175 state [6] = (titleTextStyle as IStateManager).SaveViewState ();
1176 if (checkBoxStyle != null)
1177 state [7] = (checkBoxStyle as IStateManager).SaveViewState ();
1178 if (failureTextStyle != null)
1179 state [8] = (failureTextStyle as IStateManager).SaveViewState ();
1180 if (validatorTextStyle != null)
1181 state [9] = (validatorTextStyle as IStateManager).SaveViewState ();
1183 for (int i=0; i < state.Length; i++) {
1184 if (state [0] != null)
1185 return (object) state;
1187 return null; // reduce view state
1190 [MonoTODO ("for design-time usage - no more details available")]
1191 protected override void SetDesignModeState (IDictionary data)
1193 base.SetDesignModeState (data);
1196 protected override void TrackViewState ()
1198 base.TrackViewState ();
1199 if (logonButtonStyle != null)
1200 (logonButtonStyle as IStateManager).TrackViewState ();
1201 if (labelStyle != null)
1202 (labelStyle as IStateManager).TrackViewState ();
1203 if (textBoxStyle != null)
1204 (textBoxStyle as IStateManager).TrackViewState ();
1205 if (hyperLinkStyle != null)
1206 (hyperLinkStyle as IStateManager).TrackViewState ();
1207 if (instructionTextStyle != null)
1208 (instructionTextStyle as IStateManager).TrackViewState ();
1209 if (titleTextStyle != null)
1210 (titleTextStyle as IStateManager).TrackViewState ();
1211 if (checkBoxStyle != null)
1212 (checkBoxStyle as IStateManager).TrackViewState ();
1213 if (failureTextStyle != null)
1214 (failureTextStyle as IStateManager).TrackViewState ();
1215 if (validatorTextStyle != null)
1216 (validatorTextStyle as IStateManager).TrackViewState ();
1222 public event AuthenticateEventHandler Authenticate {
1223 add { Events.AddHandler (authenticateEvent, value); }
1224 remove { Events.RemoveHandler (authenticateEvent, value); }
1227 public event EventHandler LoggedIn {
1228 add { Events.AddHandler (loggedInEvent, value); }
1229 remove { Events.RemoveHandler (loggedInEvent, value); }
1232 public event LoginCancelEventHandler LoggingIn {
1233 add { Events.AddHandler (loggingInEvent, value); }
1234 remove { Events.RemoveHandler (loggingInEvent, value); }
1237 public event EventHandler LoginError {
1238 add { Events.AddHandler (loginErrorEvent, value); }
1239 remove { Events.RemoveHandler (loginErrorEvent, value); }
1245 internal void RegisterApplyStyle (WebControl control, Style style)
1247 styles.Add (new object [] { control, style });
1250 private bool AuthenticateUser ()
1252 LoginCancelEventArgs lcea = new LoginCancelEventArgs ();
1257 AuthenticateEventArgs aea = new AuthenticateEventArgs ();
1259 if (!HasOnAuthenticateHandler ()) {
1260 string mp = MembershipProvider;
1261 MembershipProvider provider = (mp.Length == 0) ?
1262 provider = Membership.Provider : Membership.Providers [mp];
1263 if (provider == null) {
1264 throw new HttpException (Locale.GetText ("No provider named '{0}' could be found.", mp));
1267 aea.Authenticated = provider.ValidateUser (UserName, Password);
1269 OnAuthenticate (aea);
1271 if (aea.Authenticated) {
1272 FormsAuthentication.SetAuthCookie (UserName, RememberMeSet);
1273 OnLoggedIn (EventArgs.Empty);
1275 string url = DestinationPageUrl;
1276 if (Page.Request.Path.ToLower ().StartsWith (FormsAuthentication.LoginUrl.ToLower ())) {
1277 if (!String.IsNullOrEmpty (FormsAuthentication.ReturnUrl))
1278 Redirect (FormsAuthentication.ReturnUrl);
1279 else if (!String.IsNullOrEmpty (DestinationPageUrl))
1281 else if (!String.IsNullOrEmpty (FormsAuthentication.DefaultUrl))
1282 Redirect (FormsAuthentication.DefaultUrl);
1283 else if (url.Length == 0)
1286 else if (!String.IsNullOrEmpty (DestinationPageUrl)) {
1295 OnLoginError (EventArgs.Empty);
1296 if (FailureAction == LoginFailureAction.RedirectToLoginPage) {
1297 // login page is defined in web.config
1298 FormsAuthentication.RedirectToLoginPage ();
1304 // TODO: its called from default template only, not usefully, OnBubbleEvent
1305 // do handle command, need be removed
1307 private void LoginClick (object sender, CommandEventArgs e)
1309 RaiseBubbleEvent (sender, (EventArgs)e);
1312 private bool IsDefaultLoginPage ()
1314 if ((Page == null) || (Page.Request == null))
1316 string defaultLogin = FormsAuthentication.LoginUrl;
1317 if (defaultLogin == null)
1319 string url = Page.Request.Url.AbsolutePath;
1320 return (String.Compare (defaultLogin, 0, url, url.Length - defaultLogin.Length, defaultLogin.Length,
1321 true, CultureInfo.InvariantCulture) == 0);
1324 private bool IsLoggedIn ()
1326 if ((Page == null) || (Page.Request == null))
1328 return Page.Request.IsAuthenticated;
1331 private void Redirect (string url)
1333 if ((Page != null) && (Page.Response != null))
1334 Page.Response.Redirect (url);
1337 private void Refresh () {
1338 if ((Page != null) && (Page.Response != null))
1339 Page.Response.Redirect (Page.Request.RawUrl);
1342 private void UserName_TextChanged (object sender, EventArgs e)
1344 UserName = ((ITextControl)sender).Text;
1347 private void Password_TextChanged (object sender, EventArgs e)
1349 _password = ((ITextControl)sender).Text;
1352 private void RememberMe_CheckedChanged (object sender, EventArgs e)
1354 RememberMeSet = ((ICheckBoxControl)sender).Checked;