51958555846157661b1ac8200b9e67449bed3dcf
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / Login.cs
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.
28 //
29
30
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;
37
38 namespace System.Web.UI.WebControls {
39
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 #if NET_4_0
49         , IRenderOuterTable
50 #endif
51         {
52                 #region LoginContainer
53                 // TODO: This class should probably be folded into a generic one with BaseChangePasswordContainer
54                 sealed class LoginContainer : Control
55                 {
56                         readonly Login _owner;
57 #if NET_4_0
58                         bool renderOuterTable;
59 #endif
60                         Table _table;
61                         TableCell _containerCell;
62
63                         public LoginContainer (Login owner)
64                         {
65                                 _owner = owner;
66 #if NET_4_0
67                                 renderOuterTable = _owner.RenderOuterTable;
68
69                                 if (renderOuterTable)
70 #endif
71                                         InitTable ();
72                         }
73                         
74                         public override string ID {
75                                 get {
76                                         return _owner.ID;
77                                 }
78                                 set {
79                                         _owner.ID = value;
80                                 }
81                         }
82                         
83                         public override string ClientID {
84                                 get {
85                                         return _owner.ClientID;
86                                 }
87                         }
88
89                         public void InstantiateTemplate (ITemplate template)
90                         {
91 #if NET_4_0
92                                 if (!renderOuterTable)
93                                         template.InstantiateIn (this);
94                                 else
95 #endif
96                                         template.InstantiateIn (_containerCell);
97                         }
98
99                         void InitTable ()
100                         {
101                                 _table = new Table ();
102                                 _containerCell = new TableCell ();
103
104                                 TableRow row = new TableRow ();
105                                 row.Cells.Add (_containerCell);
106                                 _table.Rows.Add (row);
107
108                                 Controls.AddAt (0, _table);
109                         }
110                         
111                         protected internal override void Render (HtmlTextWriter writer)
112                         {
113                                 if (_table != null) {
114                                         _table.CellSpacing = 0;
115                                         _table.CellPadding = _owner.BorderPadding;
116                                         _table.ApplyStyle (_owner.ControlStyle);
117                                         _table.Attributes.CopyFrom (_owner.Attributes);
118                                 }
119                                 
120                                 base.Render (writer);
121                         }
122                         
123                         public Control UserNameTextBox {
124                                 get {
125                                         return FindControl("UserName");
126                                 }
127                         }
128
129                         public Control PasswordTextBox {
130                                 get {
131                                         return FindControl("Password");
132                                 }
133                         }
134
135                         public Control RememberMeCheckBox {
136                                 get {
137                                         return FindControl("RememberMe");
138                                 }
139                         }
140
141                         public ITextControl FailureTextLiteral
142                         {
143                                 get { 
144                                         return FindControl ("FailureText") as ITextControl; 
145                                 }
146                         }
147                 }
148
149                 #endregion
150
151                 #region LoginTemplate
152
153                 sealed class LoginTemplate : WebControl, ITemplate
154                 {
155                         readonly Login _login;
156
157                         public LoginTemplate (Login login)
158                         {
159                                 _login = login;
160                         }
161
162                         void ITemplate.InstantiateIn (Control container)
163                         {
164                                 // 
165                                 LiteralControl TitleText = new LiteralControl (_login.TitleText);
166
167                                 // 
168                                 LiteralControl InstructionText = new LiteralControl (_login.InstructionText);
169
170                                 //
171                                 TextBox UserName = new TextBox ();
172                                 UserName.ID = "UserName";
173                                 UserName.Text = _login.UserName;
174                                 _login.RegisterApplyStyle (UserName, _login.TextBoxStyle);
175
176                                 Label UserNameLabel = new Label ();
177                                 UserNameLabel.ID = "UserNameLabel";
178                                 UserNameLabel.AssociatedControlID = "UserName";
179                                 UserNameLabel.Text = _login.UserNameLabelText;
180
181                                 RequiredFieldValidator UserNameRequired = new RequiredFieldValidator ();
182                                 UserNameRequired.ID = "UserNameRequired";
183                                 UserNameRequired.ControlToValidate = "UserName";
184                                 UserNameRequired.ErrorMessage = _login.UserNameRequiredErrorMessage;
185                                 UserNameRequired.ToolTip = _login.UserNameRequiredErrorMessage;
186                                 UserNameRequired.Text = "*";
187                                 UserNameRequired.ValidationGroup = _login.ID;
188                                 _login.RegisterApplyStyle (UserNameRequired, _login.ValidatorTextStyle);
189
190                                 //
191                                 TextBox Password = new TextBox ();
192                                 Password.ID = "Password";
193                                 Password.TextMode = TextBoxMode.Password;
194                                 _login.RegisterApplyStyle (Password, _login.TextBoxStyle);
195
196                                 Label PasswordLabel = new Label ();
197                                 PasswordLabel.ID = "PasswordLabel";
198                                 PasswordLabel.AssociatedControlID = "PasswordLabel";
199                                 PasswordLabel.Text = _login.PasswordLabelText;
200
201                                 RequiredFieldValidator PasswordRequired = new RequiredFieldValidator ();
202                                 PasswordRequired.ID = "PasswordRequired";
203                                 PasswordRequired.ControlToValidate = "Password";
204                                 PasswordRequired.ErrorMessage = _login.PasswordRequiredErrorMessage;
205                                 PasswordRequired.ToolTip = _login.PasswordRequiredErrorMessage;
206                                 PasswordRequired.Text = "*";
207                                 PasswordRequired.ValidationGroup = _login.ID;
208                                 _login.RegisterApplyStyle (PasswordRequired, _login.ValidatorTextStyle);
209
210                                 bool useRememberMe = _login != null ? _login.DisplayRememberMe : true;
211                                 CheckBox RememberMe;
212                                 //
213                                 if (useRememberMe) {
214                                         RememberMe = new CheckBox ();
215                                         RememberMe.ID = "RememberMe";
216                                         RememberMe.Checked = _login.RememberMeSet;
217                                         RememberMe.Text = _login.RememberMeText;
218                                         _login.RegisterApplyStyle (RememberMe, _login.CheckBoxStyle);
219                                 } else
220                                         RememberMe = null;
221                                 
222                                 // TODO: Error text
223                                 Literal FailureText = new Literal ();
224                                 FailureText.ID = "FailureText";
225                                 FailureText.EnableViewState = false;
226
227                                 //
228                                 WebControl LoginButton = null;
229                                 switch (_login.LoginButtonType) {
230                                         case ButtonType.Button:
231                                                 LoginButton = new Button ();
232                                                 LoginButton.ID = "LoginButton";
233                                                 break;
234                                         case ButtonType.Link:
235                                                 LoginButton = new LinkButton ();
236                                                 LoginButton.ID = "LoginLinkButton";
237                                                 break;
238                                         case ButtonType.Image:
239                                                 LoginButton = new ImageButton ();
240                                                 LoginButton.ID = "LoginImageButton";
241                                                 break;
242                                 }
243                                 _login.RegisterApplyStyle (LoginButton, _login.LoginButtonStyle);
244                                 LoginButton.ID = "LoginButton";
245                                 ((IButtonControl) LoginButton).Text = _login.LoginButtonText;
246                                 ((IButtonControl) LoginButton).CommandName = Login.LoginButtonCommandName;
247                                 ((IButtonControl) LoginButton).Command += new CommandEventHandler (_login.LoginClick);
248                                 ((IButtonControl) LoginButton).ValidationGroup = _login.ID;
249
250                                 // Create layout table
251                                 Table table = new Table ();
252                                 table.CellPadding = 0;
253
254                                 // Title row
255                                 table.Rows.Add (
256                                         CreateRow (
257                                         CreateCell (TitleText, null, _login.TitleTextStyle, HorizontalAlign.Center)));
258
259                                 // Instruction row
260                                 if (_login.InstructionText.Length > 0) {
261                                         table.Rows.Add (
262                                                 CreateRow (
263                                                 CreateCell (InstructionText, null, _login.instructionTextStyle, HorizontalAlign.Center)));
264                                 }
265
266                                 if (_login.Orientation == Orientation.Horizontal) {
267                                         // Main row
268                                         TableRow row1 = new TableRow ();
269                                         TableRow row2 = new TableRow ();
270                                         if (_login.TextLayout == LoginTextLayout.TextOnTop)
271                                                 row1.Cells.Add (CreateCell (UserNameLabel, null, _login.LabelStyle));
272                                         else
273                                                 row2.Cells.Add (CreateCell (UserNameLabel, null, _login.LabelStyle));
274                                         row2.Cells.Add (CreateCell (UserName, UserNameRequired, null));
275                                         if (_login.TextLayout == LoginTextLayout.TextOnTop)
276                                                 row1.Cells.Add (CreateCell (PasswordLabel, null, _login.LabelStyle));
277                                         else
278                                                 row2.Cells.Add (CreateCell (PasswordLabel, null, _login.LabelStyle));
279                                         row2.Cells.Add (CreateCell (Password, PasswordRequired, null));
280                                         if (useRememberMe)
281                                                 row2.Cells.Add (CreateCell (RememberMe, null, null));
282                                         row2.Cells.Add (CreateCell (LoginButton, null, null));
283                                         if (row1.Cells.Count > 0)
284                                                 table.Rows.Add (row1);
285                                         table.Rows.Add (row2);
286                                 }
287                                 else { // Orientation.Vertical
288                                         if (_login.TextLayout == LoginTextLayout.TextOnLeft)
289                                                 table.Rows.Add (CreateRow (UserNameLabel, UserName, UserNameRequired, _login.LabelStyle));
290                                         else {
291                                                 table.Rows.Add (CreateRow (UserNameLabel, null, null, _login.LabelStyle));
292                                                 table.Rows.Add (CreateRow (null, UserName, UserNameRequired, null));
293                                         }
294                                         if (_login.TextLayout == LoginTextLayout.TextOnLeft)
295                                                 table.Rows.Add (CreateRow (PasswordLabel, Password, PasswordRequired, _login.LabelStyle));
296                                         else {
297                                                 table.Rows.Add (CreateRow (PasswordLabel, null, null, _login.LabelStyle));
298                                                 table.Rows.Add (CreateRow (null, Password, PasswordRequired, null));
299                                         }
300                                         if (useRememberMe)
301                                                 table.Rows.Add (CreateRow (CreateCell (RememberMe, null, null)));
302                                         table.Rows.Add (CreateRow (CreateCell (LoginButton, null, null, HorizontalAlign.Right)));
303                                 }
304
305                                 // Error message row
306                                 if (_login.FailureTextStyle.ForeColor.IsEmpty)
307                                         _login.FailureTextStyle.ForeColor = System.Drawing.Color.Red;
308
309                                 table.Rows.Add (
310                                         CreateRow (
311                                         CreateCell (FailureText, null, _login.FailureTextStyle)));
312
313                                 // Links row
314                                 TableCell linksCell = new TableCell ();
315                                 _login.RegisterApplyStyle (linksCell, _login.HyperLinkStyle);
316
317                                 if (AddLink (_login.CreateUserUrl, _login.CreateUserText, _login.CreateUserIconUrl, linksCell, _login.HyperLinkStyle))
318                                         if (_login.Orientation == Orientation.Vertical)
319                                                 linksCell.Controls.Add (new LiteralControl ("<br/>"));
320                                         else
321                                                 linksCell.Controls.Add (new LiteralControl (" "));
322
323                                 if (AddLink (_login.PasswordRecoveryUrl, _login.PasswordRecoveryText, _login.PasswordRecoveryIconUrl, linksCell, _login.HyperLinkStyle))
324                                         if (_login.Orientation == Orientation.Vertical)
325                                                 linksCell.Controls.Add (new LiteralControl ("<br/>"));
326                                         else
327                                                 linksCell.Controls.Add (new LiteralControl (" "));
328
329                                 AddLink (_login.HelpPageUrl, _login.HelpPageText, _login.HelpPageIconUrl, linksCell, _login.HyperLinkStyle);
330                                 table.Rows.Add (CreateRow (linksCell));
331
332                                 FixTableColumnSpans (table);
333                                 container.Controls.Add (table);
334                         }
335
336                         TableRow CreateRow (TableCell cell)
337                         {
338                                 TableRow row = new TableRow ();
339                                 row.Cells.Add (cell);
340                                 return row;
341                         }
342
343                         TableRow CreateRow (Control c0, Control c1, Control c2, Style s)
344                         {
345                                 TableRow row = new TableRow ();
346                                 TableCell cell0 = new TableCell ();
347                                 TableCell cell1 = new TableCell ();
348
349                                 if (c0 != null) {
350                                         cell0.Controls.Add (c0);
351                                         row.Controls.Add (cell0);
352                                 }
353
354                                 if (s != null)
355                                         cell0.ApplyStyle (s);
356
357                                 if ((c1 != null) && (c2 != null)) {
358                                         cell1.Controls.Add (c1);
359                                         cell1.Controls.Add (c2);
360                                         cell0.HorizontalAlign = HorizontalAlign.Right;
361                                         row.Controls.Add (cell1);
362                                 }
363                                 return row;
364                         }
365                         
366                         TableCell CreateCell (Control c0, Control c1, Style s, HorizontalAlign align)
367                         {
368                                 TableCell cell = CreateCell (c0, c1, s);
369                                 cell.HorizontalAlign = align;
370                                 return cell;
371                         }
372
373                         TableCell CreateCell (Control c0, Control c1, Style s)
374                         {
375                                 TableCell cell = new TableCell ();
376                                 if (s != null)
377                                         cell.ApplyStyle (s);
378
379                                 cell.Controls.Add (c0);
380                                 if (c1 != null)
381                                         cell.Controls.Add (c1);
382
383                                 return cell;
384                         }
385
386                         bool AddLink (string pageUrl, string linkText, string linkIcon, WebControl container, Style style)
387                         {
388                                 bool added = false;
389                                 if (linkIcon.Length > 0) {
390                                         Image img = new Image ();
391                                         img.ImageUrl = linkIcon;
392                                         container.Controls.Add (img);
393                                         added = true;
394                                 }
395                                 if (linkText.Length > 0) {
396                                         HyperLink link = new HyperLink ();
397                                         link.NavigateUrl = pageUrl;
398                                         link.Text = linkText;
399                                         _login.RegisterApplyStyle (link, style);
400                                         container.Controls.Add (link);
401                                         added = true;
402                                 }
403                                 return added;
404                         }
405
406                         void FixTableColumnSpans (Table table)
407                         {
408                                 int maxCols = 0;
409                                 for (int row = 0; row < table.Rows.Count; row++) {
410                                         if (maxCols < table.Rows [row].Cells.Count)
411                                                 maxCols = table.Rows [row].Cells.Count;
412                                 }
413                                 for (int row = 0; row < table.Rows.Count; row++) {
414                                         if (table.Rows [row].Cells.Count == 1 && maxCols > 1)
415                                                         table.Rows [row].Cells [0].ColumnSpan = maxCols;
416                                 }
417                         }
418                 }
419
420                 #endregion
421
422                 public static readonly string LoginButtonCommandName = "Login";
423
424                 static readonly object authenticateEvent = new object ();
425                 static readonly object loggedInEvent = new object ();
426                 static readonly object loggingInEvent = new object ();
427                 static readonly object loginErrorEvent = new object ();
428
429                 TableItemStyle checkBoxStyle;
430                 TableItemStyle failureTextStyle;
431                 TableItemStyle hyperLinkStyle;
432                 TableItemStyle instructionTextStyle;
433                 TableItemStyle labelStyle;
434                 Style logonButtonStyle;
435                 Style textBoxStyle;
436                 TableItemStyle titleTextStyle;
437                 Style validatorTextStyle;
438                 ArrayList styles = new ArrayList ();
439
440                 ITemplate layoutTemplate;
441                 LoginContainer container;
442
443                 string _password;
444 #if NET_4_0
445                 bool renderOuterTable = true;
446 #endif
447                 public Login ()
448                 {
449                 }
450
451                 [DefaultValue (1)]
452                 public virtual int BorderPadding {
453                         get {
454                                 object o = ViewState ["BorderPadding"];
455                                 return (o == null) ? 1 : (int) o;
456                         }
457                         set {
458                                 if (value < -1)
459                                         throw new ArgumentOutOfRangeException ("BorderPadding", "< -1");
460                                 else
461                                         ViewState ["BorderPadding"] = value;
462                         }
463                 }
464
465                 [DefaultValue (null)]
466                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
467                 [NotifyParentProperty (true)]
468                 [PersistenceMode (PersistenceMode.InnerProperty)]
469                 public TableItemStyle CheckBoxStyle {
470                         get {
471                                 if (checkBoxStyle == null) {
472                                         checkBoxStyle = new TableItemStyle ();
473                                         if (IsTrackingViewState) {
474                                                 (checkBoxStyle as IStateManager).TrackViewState ();
475                                         }
476                                 }
477                                 return checkBoxStyle;
478                         }
479                 }
480
481                 [DefaultValue ("")]
482                 [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
483                 [UrlProperty]
484                 public virtual string CreateUserIconUrl {
485                         get {
486                                 object o = ViewState ["CreateUserIconUrl"];
487                                 return (o == null) ? String.Empty : (string) o;
488                         }
489                         set {
490                                 if (value == null)
491                                         ViewState.Remove ("CreateUserIconUrl");
492                                 else
493                                         ViewState ["CreateUserIconUrl"] = value;
494                         }
495                 }
496
497                 [DefaultValue ("")]
498                 [Localizable (true)]
499                 public virtual string CreateUserText {
500                         get {
501                                 object o = ViewState ["CreateUserText"];
502                                 return (o == null) ? String.Empty : (string) o;
503                         }
504                         set {
505                                 if (value == null)
506                                         ViewState.Remove ("CreateUserText");
507                                 else
508                                         ViewState ["CreateUserText"] = value;
509                         }
510                 }
511
512                 [DefaultValue ("")]
513                 [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
514                 [UrlProperty]
515                 public virtual string CreateUserUrl {
516                         get {
517                                 object o = ViewState ["CreateUserUrl"];
518                                 return (o == null) ? String.Empty : (string) o;
519                         }
520                         set {
521                                 if (value == null)
522                                         ViewState.Remove ("CreateUserUrl");
523                                 else
524                                         ViewState ["CreateUserUrl"] = value;
525                         }
526                 }
527
528                 [DefaultValue ("")]
529                 [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
530                 [Themeable (false)]
531                 [UrlProperty]
532                 public virtual string DestinationPageUrl {
533                         get {
534                                 object o = ViewState ["DestinationPageUrl"];
535                                 return (o == null) ? String.Empty : (string) o;
536                         }
537                         set {
538                                 if (value == null)
539                                         ViewState.Remove ("DestinationPageUrl");
540                                 else
541                                         ViewState ["DestinationPageUrl"] = value;
542                         }
543                 }
544
545                 [DefaultValue (true)]
546                 [Themeable (false)]
547                 public virtual bool DisplayRememberMe {
548                         get {
549                                 object o = ViewState ["DisplayRememberMe"];
550                                 return (o == null) ? true : (bool) o;
551                         }
552                         set {
553                                 ViewState ["DisplayRememberMe"] = value;
554                         }
555                 }
556
557                 [DefaultValue (LoginFailureAction.Refresh)]
558                 [Themeable (false)]
559                 [MonoTODO ("RedirectToLoginPage not yet implemented in FormsAuthentication")]
560                 public virtual LoginFailureAction FailureAction {
561                         get {
562                                 object o = ViewState ["FailureAction"];
563                                 return (o == null) ? LoginFailureAction.Refresh : (LoginFailureAction) o;
564                         }
565                         set {
566                                 if ((value < LoginFailureAction.Refresh) || (value > LoginFailureAction.RedirectToLoginPage))
567                                         throw new ArgumentOutOfRangeException ("FailureAction");
568                                 ViewState ["FailureAction"] = (int) value;
569                         }
570                 }
571
572                 [Localizable (true)]
573                 public virtual string FailureText {
574                         get {
575                                 object o = ViewState ["FailureText"];
576                                 return (o == null) ? Locale.GetText ("Your login attempt was not successful. Please try again.") : (string) o;
577                         }
578                         set {
579                                 if (value == null)
580                                         ViewState.Remove ("FailureText");
581                                 else
582                                         ViewState ["FailureText"] = value;
583                         }
584                 }
585
586                 [DefaultValue (null)]
587                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
588                 [NotifyParentProperty (true)]
589                 [PersistenceMode (PersistenceMode.InnerProperty)]
590                 public TableItemStyle FailureTextStyle {
591                         get {
592                                 if (failureTextStyle == null) {
593                                         failureTextStyle = new TableItemStyle ();
594                                         if (IsTrackingViewState) {
595                                                 (failureTextStyle as IStateManager).TrackViewState ();
596                                         }
597                                 }
598                                 return failureTextStyle;
599                         }
600                 }
601
602                 [DefaultValue ("")]
603                 [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
604                 [UrlProperty]
605                 public virtual string HelpPageIconUrl {
606                         get {
607                                 object o = ViewState ["HelpPageIconUrl"];
608                                 return (o == null) ? String.Empty : (string) o;
609                         }
610                         set {
611                                 if (value == null)
612                                         ViewState.Remove ("HelpPageIconUrl");
613                                 else
614                                         ViewState ["HelpPageIconUrl"] = value;
615                         }
616                 }
617
618                 [DefaultValue ("")]
619                 [Localizable (true)]
620                 public virtual string HelpPageText {
621                         get {
622                                 object o = ViewState ["HelpPageText"];
623                                 return (o == null) ? String.Empty : (string) o;
624                         }
625                         set {
626                                 if (value == null)
627                                         ViewState.Remove ("HelpPageText");
628                                 else
629                                         ViewState ["HelpPageText"] = value;
630                         }
631                 }
632
633                 [DefaultValue ("")]
634                 [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
635                 [UrlProperty]
636                 public virtual string HelpPageUrl {
637                         get {
638                                 object o = ViewState ["HelpPageUrl"];
639                                 return (o == null) ? String.Empty : (string) o;
640                         }
641                         set {
642                                 if (value == null)
643                                         ViewState.Remove ("HelpPageUrl");
644                                 else
645                                         ViewState ["HelpPageUrl"] = value;
646                         }
647                 }
648
649                 [DefaultValue (null)]
650                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
651                 [NotifyParentProperty (true)]
652                 [PersistenceMode (PersistenceMode.InnerProperty)]
653                 public TableItemStyle HyperLinkStyle {
654                         get {
655                                 if (hyperLinkStyle == null) {
656                                         hyperLinkStyle = new TableItemStyle ();
657                                         if (IsTrackingViewState) {
658                                                 (hyperLinkStyle  as IStateManager).TrackViewState ();
659                                         }
660                                 }
661                                 return hyperLinkStyle;
662                         }
663                 }
664
665                 [DefaultValue ("")]
666                 [Localizable (true)]
667                 public virtual string InstructionText {
668                         get {
669                                 object o = ViewState ["InstructionText"];
670                                 return (o == null) ? String.Empty : (string) o;
671                         }
672                         set {
673                                 if (value == null)
674                                         ViewState.Remove ("InstructionText");
675                                 else
676                                         ViewState ["InstructionText"] = value;
677                         }
678                 }
679
680                 [DefaultValue (null)]
681                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
682                 [NotifyParentProperty (true)]
683                 [PersistenceMode (PersistenceMode.InnerProperty)]
684                 public TableItemStyle InstructionTextStyle {
685                         get {
686                                 if (instructionTextStyle == null) {
687                                         instructionTextStyle = new TableItemStyle ();
688                                         if (IsTrackingViewState) {
689                                                 (instructionTextStyle as IStateManager).TrackViewState ();
690                                         }
691                                 }
692                                 return instructionTextStyle;
693                         }
694                 }
695
696                 [DefaultValue (null)]
697                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
698                 [NotifyParentProperty (true)]
699                 [PersistenceMode (PersistenceMode.InnerProperty)]
700                 public TableItemStyle LabelStyle {
701                         get {
702                                 if (labelStyle == null) {
703                                         labelStyle = new TableItemStyle ();
704                                         if (IsTrackingViewState) {
705                                                 (labelStyle as IStateManager).TrackViewState ();
706                                         }
707                                 }
708                                 return labelStyle;
709                         }
710                 }
711
712                 [Browsable (false)]
713                 [TemplateContainer (typeof (Login))]
714                 [PersistenceMode (PersistenceMode.InnerProperty)]
715                 public virtual ITemplate LayoutTemplate {
716                         get { return layoutTemplate; }
717                         set { layoutTemplate = value; }
718                 }
719
720                 [DefaultValue ("")]
721                 [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
722                 [UrlProperty]
723                 public virtual string LoginButtonImageUrl {
724                         get {
725                                 object o = ViewState ["LoginButtonImageUrl"];
726                                 return (o == null) ? String.Empty : (string) o;
727                         }
728                         set {
729                                 if (value == null)
730                                         ViewState.Remove ("LoginButtonImageUrl");
731                                 else
732                                         ViewState ["LoginButtonImageUrl"] = value;
733                         }
734                 }
735
736                 [DefaultValue (null)]
737                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
738                 [NotifyParentProperty (true)]
739                 [PersistenceMode (PersistenceMode.InnerProperty)]
740                 public Style LoginButtonStyle {
741                         get {
742                                 if (logonButtonStyle == null) {
743                                         logonButtonStyle = new Style ();
744                                         if (IsTrackingViewState) {
745                                                 (logonButtonStyle as IStateManager).TrackViewState ();
746                                         }
747                                 }
748                                 return logonButtonStyle;
749                         }
750                 }
751
752                 [Localizable (true)]
753                 public virtual string LoginButtonText {
754                         get {
755                                 object o = ViewState ["LoginButtonText"];
756                                 return (o == null) ? Locale.GetText ("Log In") : (string) o;
757                         }
758                         set {
759                                 if (value == null)
760                                         ViewState.Remove ("LoginButtonText");
761                                 else
762                                         ViewState ["LoginButtonText"] = value;
763                         }
764                 }
765
766                 [DefaultValue (ButtonType.Button)]
767                 public virtual ButtonType LoginButtonType {
768                         get {
769                                 object o = ViewState ["LoginButtonType"];
770                                 return (o == null) ? ButtonType.Button : (ButtonType) o;
771                         }
772                         set {
773                                 if ((value < ButtonType.Button) || (value > ButtonType.Link))
774                                         throw new ArgumentOutOfRangeException ("LoginButtonType");
775                                 ViewState ["LoginButtonType"] = (int) value;
776                         }
777                 }
778
779                 [DefaultValue ("")]
780                 [Themeable (false)]
781                 public virtual string MembershipProvider {
782                         get {
783                                 object o = ViewState ["MembershipProvider"];
784                                 return (o == null) ? String.Empty : (string) o;
785                         }
786                         set {
787                                 if (value == null)
788                                         ViewState.Remove ("MembershipProvider");
789                                 else
790                                         ViewState ["MembershipProvider"] = value;
791                         }
792                 }
793
794                 [DefaultValue (Orientation.Vertical)]
795                 public virtual Orientation Orientation {
796                         get {
797                                 object o = ViewState ["Orientation"];
798                                 return (o == null) ? Orientation.Vertical : (Orientation) o;
799                         }
800                         set {
801                                 if ((value < Orientation.Horizontal) || (value > Orientation.Vertical))
802                                         throw new ArgumentOutOfRangeException ("Orientation");
803                                 ViewState ["Orientation"] = (int) value;
804                         }
805                 }
806
807                 [Browsable (false)]
808                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
809                 public virtual string Password {
810                         get {
811                                 return _password != null ? _password : String.Empty;
812                         }
813                 }
814
815                 [Localizable (true)]
816                 public virtual string PasswordLabelText {
817                         get {
818                                 object o = ViewState ["PasswordLabelText"];
819                                 return (o == null) ? "Password:" : (string) o;
820                         }
821                         set {
822                                 if (value == null)
823                                         ViewState.Remove ("PasswordLabelText");
824                                 else
825                                         ViewState ["PasswordLabelText"] = value;
826                         }
827                 }
828
829                 [DefaultValue ("")]
830                 [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
831                 [UrlProperty]
832                 public virtual string PasswordRecoveryIconUrl {
833                         get {
834                                 object o = ViewState ["PasswordRecoveryIconUrl"];
835                                 return (o == null) ? String.Empty : (string) o;
836                         }
837                         set {
838                                 if (value == null)
839                                         ViewState.Remove ("PasswordRecoveryIconUrl");
840                                 else
841                                         ViewState ["PasswordRecoveryIconUrl"] = value;
842                         }
843                 }
844
845                 [DefaultValue ("")]
846                 [Localizable (true)]
847                 public virtual string PasswordRecoveryText {
848                         get {
849                                 object o = ViewState ["PasswordRecoveryText"];
850                                 return (o == null) ? String.Empty : (string) o;
851                         }
852                         set {
853                                 if (value == null)
854                                         ViewState.Remove ("PasswordRecoveryText");
855                                 else
856                                         ViewState ["PasswordRecoveryText"] = value;
857                         }
858                 }
859
860                 [DefaultValue ("")]
861                 [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
862                 [UrlProperty]
863                 public virtual string PasswordRecoveryUrl {
864                         get {
865                                 object o = ViewState ["PasswordRecoveryUrl"];
866                                 return (o == null) ? String.Empty : (string) o;
867                         }
868                         set {
869                                 if (value == null)
870                                         ViewState.Remove ("PasswordRecoveryUrl");
871                                 else
872                                         ViewState ["PasswordRecoveryUrl"] = value;
873                         }
874                 }
875
876                 [Localizable (true)]
877                 public virtual string PasswordRequiredErrorMessage {
878                         get {
879                                 object o = ViewState ["PasswordRequiredErrorMessage"];
880                                 return (o == null) ? Locale.GetText ("Password is required.") : (string) o;
881                         }
882                         set {
883                                 if (value == null)
884                                         ViewState.Remove ("PasswordRequiredErrorMessage");
885                                 else
886                                         ViewState ["PasswordRequiredErrorMessage"] = value;
887                         }
888                 }
889 #if NET_4_0
890                 [DefaultValue (true)]
891                 public virtual bool RenderOuterTable {
892                         get { return renderOuterTable; }
893                         set { renderOuterTable = value; }
894                 }
895 #endif
896                 [DefaultValue (false)]
897                 [Themeable (false)]
898                 public virtual bool RememberMeSet {
899                         get {
900                                 object o = ViewState ["RememberMeSet"];
901                                 return (o == null) ? false : (bool) o;
902                         }
903                         set {
904                                 ViewState ["RememberMeSet"] = value;
905                         }
906                 }
907
908                 [Localizable (true)]
909                 public virtual string RememberMeText {
910                         get {
911                                 object o = ViewState ["RememberMeText"];
912                                 return (o == null) ? Locale.GetText ("Remember me next time.") : (string) o;
913                         }
914                         set {
915                                 if (value == null)
916                                         ViewState.Remove ("RememberMeText");
917                                 else
918                                         ViewState ["RememberMeText"] = value;
919                         }
920                 }
921
922                 protected override HtmlTextWriterTag TagKey {
923                         get { return HtmlTextWriterTag.Table; }
924                 }
925
926                 [DefaultValue (null)]
927                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
928                 [NotifyParentProperty (true)]
929                 [PersistenceMode (PersistenceMode.InnerProperty)]
930                 public Style TextBoxStyle {
931                         get {
932                                 if (textBoxStyle == null) {
933                                         textBoxStyle = new Style ();
934                                         if (IsTrackingViewState) {
935                                                 (textBoxStyle as IStateManager).TrackViewState ();
936                                         }
937                                 }
938                                 return textBoxStyle;
939                         }
940                 }
941
942                 [DefaultValue (LoginTextLayout.TextOnLeft)]
943                 public virtual LoginTextLayout TextLayout {
944                         get {
945                                 object o = ViewState ["TextLayout"];
946                                 return (o == null) ? LoginTextLayout.TextOnLeft : (LoginTextLayout) o;
947                         }
948                         set {
949                                 if ((value < LoginTextLayout.TextOnLeft) || (value > LoginTextLayout.TextOnTop))
950                                         throw new ArgumentOutOfRangeException ("TextLayout");
951                                 ViewState ["TextLayout"] = (int) value;
952                         }
953                 }
954
955                 [Localizable (true)]
956                 public virtual string TitleText {
957                         get {
958                                 object o = ViewState ["TitleText"];
959                                 return (o == null) ? Locale.GetText ("Log In") : (string) o;
960                         }
961                         set {
962                                 if (value == null)
963                                         ViewState.Remove ("TitleText");
964                                 else
965                                         ViewState ["TitleText"] = value;
966                         }
967                 }
968
969                 [DefaultValue (null)]
970                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
971                 [NotifyParentProperty (true)]
972                 [PersistenceMode (PersistenceMode.InnerProperty)]
973                 public TableItemStyle TitleTextStyle {
974                         get {
975                                 if (titleTextStyle == null) {
976                                         titleTextStyle = new TableItemStyle ();
977                                         if (IsTrackingViewState) {
978                                                 (titleTextStyle as IStateManager).TrackViewState ();
979                                         }
980                                 }
981                                 return titleTextStyle;
982                         }
983                 }
984
985                 [DefaultValue ("")]
986                 public virtual string UserName {
987                         get {
988                                 object o = ViewState ["UserName"];
989                                 return (o == null) ? String.Empty : (string) o;
990                         }
991                         set {
992                                 if (value == null)
993                                         ViewState.Remove ("UserName");
994                                 else
995                                         ViewState ["UserName"] = value;
996                         }
997                 }
998
999                 [Localizable (true)]
1000                 public virtual string UserNameLabelText {
1001                         get {
1002                                 object o = ViewState ["UserNameLabelText"];
1003                                 return (o == null) ? Locale.GetText ("User Name:") : (string) o;
1004                         }
1005                         set {
1006                                 if (value == null)
1007                                         ViewState.Remove ("UserNameLabelText");
1008                                 else
1009                                         ViewState ["UserNameLabelText"] = value;
1010                         }
1011                 }
1012
1013                 [Localizable (true)]
1014                 public virtual string UserNameRequiredErrorMessage {
1015                         get {
1016                                 object o = ViewState ["UserNameRequiredErrorMessage"];
1017                                 return (o == null) ? Locale.GetText ("User Name is required.") : (string) o;
1018                         }
1019                         set {
1020                                 if (value == null)
1021                                         ViewState.Remove ("UserNameRequiredErrorMessage");
1022                                 else
1023                                         ViewState ["UserNameRequiredErrorMessage"] = value;
1024                         }
1025                 }
1026
1027                 [DefaultValue (null)]
1028                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
1029                 [NotifyParentProperty (true)]
1030                 [PersistenceMode (PersistenceMode.InnerProperty)]
1031                 public Style ValidatorTextStyle {
1032                         get {
1033                                 if (validatorTextStyle == null) {
1034                                         validatorTextStyle = new Style ();
1035                                         if (IsTrackingViewState) {
1036                                                 (validatorTextStyle as IStateManager).TrackViewState ();
1037                                         }
1038                                 }
1039                                 return validatorTextStyle;
1040                         }
1041                 }
1042
1043                 [DefaultValue (true)]
1044                 [Themeable (false)]
1045                 public virtual bool VisibleWhenLoggedIn {
1046                         get {
1047                                 object o = ViewState ["VisibleWhenLoggedIn"];
1048                                 return (o == null) ? true : (bool) o;
1049                         }
1050                         set {
1051                                 ViewState ["VisibleWhenLoggedIn"] = value;
1052                         }
1053                 }
1054
1055                 LoginContainer LoginTemplateContainer
1056                 {
1057                         get {
1058                                 if (container == null)
1059                                         container = new LoginContainer (this);
1060                                 return container;
1061                         }
1062                 }
1063
1064
1065                 // methods
1066
1067                 protected internal override void CreateChildControls ()
1068                 {
1069                         Controls.Clear ();
1070
1071                         ITemplate template = LayoutTemplate;
1072                         if (template == null)
1073                                 template = new LoginTemplate (this);
1074
1075                         LoginTemplateContainer.InstantiateTemplate (template);
1076
1077                         Controls.Add (container);
1078
1079                         IEditableTextControl editable;
1080                         editable = container.UserNameTextBox as IEditableTextControl;
1081
1082                         if (editable != null) {
1083                                 editable.Text = UserName;
1084                                 editable.TextChanged += new EventHandler (UserName_TextChanged);
1085                         }
1086                         else
1087                                 throw new HttpException ("LayoutTemplate does not contain an IEditableTextControl with ID UserName for the username.");
1088
1089                         editable = container.PasswordTextBox as IEditableTextControl;
1090
1091                         if (editable != null)
1092                                 editable.TextChanged += new EventHandler (Password_TextChanged);
1093                         else
1094                                 throw new HttpException ("LayoutTemplate does not contain an IEditableTextControl with ID Password for the password.");
1095
1096                         ICheckBoxControl checkBox = container.RememberMeCheckBox as ICheckBoxControl;
1097
1098                         if (checkBox != null)
1099                                 checkBox.CheckedChanged += new EventHandler (RememberMe_CheckedChanged);
1100                 }
1101
1102                 protected override void LoadViewState (object savedState)
1103                 {
1104                         if (savedState == null) {
1105                                 base.LoadViewState (null);
1106                                 return;
1107                         }
1108
1109                         object[] state = (object[]) savedState;
1110                         base.LoadViewState (state [0]);
1111                         if (state [1] != null)
1112                                 (LoginButtonStyle as IStateManager).LoadViewState (state [1]);
1113                         if (state [2] != null)
1114                                 (LabelStyle as IStateManager).LoadViewState (state [2]);
1115                         if (state [3] != null)
1116                                 (TextBoxStyle as IStateManager).LoadViewState (state [3]);
1117                         if (state [4] != null)
1118                                 (HyperLinkStyle as IStateManager).LoadViewState (state [4]);
1119                         if (state [5] != null)
1120                                 (InstructionTextStyle as IStateManager).LoadViewState (state [5]);
1121                         if (state [6] != null)
1122                                 (TitleTextStyle as IStateManager).LoadViewState (state [6]);
1123                         if (state [7] != null)
1124                                 (CheckBoxStyle as IStateManager).LoadViewState (state [7]);
1125                         if (state [8] != null)
1126                                 (FailureTextStyle as IStateManager).LoadViewState (state [8]);
1127                         if (state [9] != null)
1128                                 (ValidatorTextStyle as IStateManager).LoadViewState (state [9]);
1129                 }
1130
1131                 bool HasOnAuthenticateHandler ()
1132                 {
1133                         return Events [authenticateEvent] != null;
1134                 }
1135
1136                 protected virtual void OnAuthenticate (AuthenticateEventArgs e)
1137                 {
1138                         // this gets called after OnLoggingIn and the authentication so we can change the result
1139                         AuthenticateEventHandler authenticate = (AuthenticateEventHandler) Events [authenticateEvent];
1140                         if (authenticate != null)
1141                                 authenticate (this, e);
1142                 }
1143
1144                 protected override bool OnBubbleEvent (object source, EventArgs e)
1145                 {
1146                         // check for submit button
1147                         CommandEventArgs cea = (e as CommandEventArgs);
1148                         if ((cea != null) &&
1149                             String.Equals (cea.CommandName, LoginButtonCommandName, StringComparison.InvariantCultureIgnoreCase)) {
1150                                 if (!AuthenticateUser ()) {
1151                                         ITextControl failureText = LoginTemplateContainer.FailureTextLiteral;
1152                                         if (failureText != null)
1153                                                 failureText.Text = FailureText;
1154                                 }
1155                                 return true;
1156                         }
1157                         return false;
1158                 }
1159
1160                 protected virtual void OnLoggedIn (EventArgs e)
1161                 {
1162                         // this gets called only if the authentication was successful
1163                         EventHandler loggedIn = (EventHandler) Events [loggedInEvent];
1164                         if (loggedIn != null)
1165                                 loggedIn (this, e);
1166                 }
1167
1168                 protected virtual void OnLoggingIn (LoginCancelEventArgs e)
1169                 {
1170                         // this gets called before OnAuthenticate so we can abort the authentication process
1171                         LoginCancelEventHandler loggingIn = (LoginCancelEventHandler) Events [loggingInEvent];
1172                         if (loggingIn != null)
1173                                 loggingIn (this, e);
1174                 }
1175
1176                 protected virtual void OnLoginError (EventArgs e)
1177                 {
1178                         // this gets called only if the authentication wasn't successful
1179                         EventHandler loginError = (EventHandler) Events [loginErrorEvent];
1180                         if (loginError != null)
1181                                 loginError (this, e);
1182                 }
1183
1184                 [MonoTODO ("overriden for ?")]
1185                 protected internal override void OnPreRender (EventArgs e)
1186                 {
1187                         base.OnPreRender (e);
1188                         // note: doc says that UserName and Password aren't available at 
1189                         // PageLoad but are during PreRender phase, so... ???
1190                 }
1191
1192                 protected internal override void Render (HtmlTextWriter writer)
1193                 {
1194 #if NET_4_0
1195                         VerifyInlinePropertiesNotSet ();
1196 #endif
1197                         // VisibleWhenLoggedIn isn't applicable to the default login page
1198                         if (!VisibleWhenLoggedIn && !IsDefaultLoginPage () && IsLoggedIn ())
1199                                 return;
1200
1201                         Page page = Page;
1202                         if (page != null)
1203                                 page.VerifyRenderingInServerForm (this);
1204
1205                         EnsureChildControls ();
1206
1207                         foreach (object [] styleDef in styles)
1208                                 ((WebControl) styleDef [0]).ApplyStyle ((Style) styleDef [1]);
1209
1210                         RenderContents(writer);
1211                 }
1212
1213                 protected override object SaveViewState ()
1214                 {
1215                         object[] state = new object [10];
1216                         state [0] = base.SaveViewState ();
1217                         if (logonButtonStyle != null)
1218                                 state [1] = (logonButtonStyle as IStateManager).SaveViewState ();
1219                         if (labelStyle != null)
1220                                 state [2] = (labelStyle as IStateManager).SaveViewState ();
1221                         if (textBoxStyle != null)
1222                                 state [3] = (textBoxStyle as IStateManager).SaveViewState ();
1223                         if (hyperLinkStyle != null)
1224                                 state [4] = (hyperLinkStyle as IStateManager).SaveViewState ();
1225                         if (instructionTextStyle != null)
1226                                 state [5] = (instructionTextStyle as IStateManager).SaveViewState ();
1227                         if (titleTextStyle != null)
1228                                 state [6] = (titleTextStyle as IStateManager).SaveViewState ();
1229                         if (checkBoxStyle != null)
1230                                 state [7] = (checkBoxStyle as IStateManager).SaveViewState ();
1231                         if (failureTextStyle != null)
1232                                 state [8] = (failureTextStyle as IStateManager).SaveViewState ();
1233                         if (validatorTextStyle != null)
1234                                 state [9] = (validatorTextStyle as IStateManager).SaveViewState ();
1235
1236                         for (int i=0; i < state.Length; i++) {
1237                                 if (state [0] != null)
1238                                         return (object) state;
1239                         }
1240                         return null; // reduce view state
1241                 }
1242
1243                 [MonoTODO ("for design-time usage - no more details available")]
1244                 protected override void SetDesignModeState (IDictionary data)
1245                 {
1246                         base.SetDesignModeState (data);
1247                 }
1248
1249                 protected override void TrackViewState ()
1250                 {
1251                         base.TrackViewState ();
1252                         if (logonButtonStyle != null)
1253                                 (logonButtonStyle as IStateManager).TrackViewState ();
1254                         if (labelStyle != null)
1255                                 (labelStyle as IStateManager).TrackViewState ();
1256                         if (textBoxStyle != null)
1257                                 (textBoxStyle as IStateManager).TrackViewState ();
1258                         if (hyperLinkStyle != null)
1259                                 (hyperLinkStyle as IStateManager).TrackViewState ();
1260                         if (instructionTextStyle != null)
1261                                 (instructionTextStyle as IStateManager).TrackViewState ();
1262                         if (titleTextStyle != null)
1263                                 (titleTextStyle as IStateManager).TrackViewState ();
1264                         if (checkBoxStyle != null)
1265                                 (checkBoxStyle as IStateManager).TrackViewState ();
1266                         if (failureTextStyle != null)
1267                                 (failureTextStyle as IStateManager).TrackViewState ();
1268                         if (validatorTextStyle != null)
1269                                 (validatorTextStyle as IStateManager).TrackViewState ();
1270                 }
1271
1272
1273                 // events
1274
1275                 public event AuthenticateEventHandler Authenticate {
1276                         add { Events.AddHandler (authenticateEvent, value); }
1277                         remove { Events.RemoveHandler (authenticateEvent, value); }
1278                 }
1279
1280                 public event EventHandler LoggedIn {
1281                         add { Events.AddHandler (loggedInEvent, value); }
1282                         remove { Events.RemoveHandler (loggedInEvent, value); }
1283                 }
1284
1285                 public event LoginCancelEventHandler LoggingIn {
1286                         add { Events.AddHandler (loggingInEvent, value); }
1287                         remove { Events.RemoveHandler (loggingInEvent, value); }
1288                 }
1289
1290                 public event EventHandler LoginError {
1291                         add { Events.AddHandler (loginErrorEvent, value); }
1292                         remove { Events.RemoveHandler (loginErrorEvent, value); }
1293                 }
1294
1295
1296                 // private stuff
1297
1298                 internal void RegisterApplyStyle (WebControl control, Style style)
1299                 {
1300                         styles.Add (new object [] { control, style });
1301                 }
1302                 
1303                 bool AuthenticateUser ()
1304                 {
1305                         if (!Page.IsValid)
1306                                 return true;
1307
1308                         LoginCancelEventArgs lcea = new LoginCancelEventArgs ();
1309                         OnLoggingIn (lcea);
1310                         if (lcea.Cancel)
1311                                 return true;
1312
1313                         AuthenticateEventArgs aea = new AuthenticateEventArgs ();
1314                         
1315                         if (!HasOnAuthenticateHandler ()) {
1316                                 string mp = MembershipProvider;
1317                                 MembershipProvider provider = (mp.Length == 0) ?
1318                                         provider = Membership.Provider : Membership.Providers [mp];
1319                                 if (provider == null) {
1320                                         throw new HttpException (Locale.GetText ("No provider named '{0}' could be found.", mp));
1321                                 }
1322
1323                                 aea.Authenticated = provider.ValidateUser (UserName, Password);
1324                         }
1325                         OnAuthenticate (aea);
1326
1327                         if (aea.Authenticated) {
1328                                 FormsAuthentication.SetAuthCookie (UserName, RememberMeSet);
1329                                 OnLoggedIn (EventArgs.Empty);
1330
1331                                 string url = DestinationPageUrl;
1332                                 if (Page.Request.Path.StartsWith (FormsAuthentication.LoginUrl, StringComparison.InvariantCultureIgnoreCase)) {
1333                                         if (!String.IsNullOrEmpty (FormsAuthentication.ReturnUrl))
1334                                                 Redirect (FormsAuthentication.ReturnUrl);
1335                                         else if (!String.IsNullOrEmpty (DestinationPageUrl))
1336                                                 Redirect (url);
1337                                         else if (!String.IsNullOrEmpty (FormsAuthentication.DefaultUrl))
1338                                                 Redirect (FormsAuthentication.DefaultUrl);
1339                                         else if (url.Length == 0)
1340                                                 Refresh ();
1341                                 }
1342                                 else if (!String.IsNullOrEmpty (DestinationPageUrl)) {
1343                                         Redirect (url);
1344                                 }
1345                                 else {
1346                                         Refresh ();
1347                                 }
1348                                 return true;
1349                         }
1350                         else {
1351                                 OnLoginError (EventArgs.Empty);
1352                                 if (FailureAction == LoginFailureAction.RedirectToLoginPage) {
1353                                         // login page is defined in web.config
1354                                         FormsAuthentication.RedirectToLoginPage ();
1355                                 }
1356                                 return false;
1357                         }
1358                 }
1359
1360                 // TODO: its called from default template only, not usefully, OnBubbleEvent 
1361                 // do handle command, need be removed
1362                 [MonoTODO()]
1363                 void LoginClick (object sender, CommandEventArgs e)
1364                 {
1365                         RaiseBubbleEvent (sender, (EventArgs)e);
1366                 }
1367
1368                 bool IsDefaultLoginPage ()
1369                 {
1370                         if ((Page == null) || (Page.Request == null))
1371                                 return false;
1372                         string defaultLogin = FormsAuthentication.LoginUrl;
1373                         if (defaultLogin == null)
1374                                 return false;
1375                         string url = Page.Request.Url.AbsolutePath;
1376                         return (String.Compare (defaultLogin, 0, url, url.Length - defaultLogin.Length, defaultLogin.Length,
1377                                 true, Helpers.InvariantCulture) == 0);
1378                 }
1379
1380                 bool IsLoggedIn ()
1381                 {
1382                         if ((Page == null) || (Page.Request == null))
1383                                 return false;
1384                         return Page.Request.IsAuthenticated;
1385                 }
1386
1387                 void Redirect (string url)
1388                 {
1389                         if ((Page != null) && (Page.Response != null))
1390                                 Page.Response.Redirect (url);
1391                 }
1392                 
1393                 void Refresh () {
1394                         if ((Page != null) && (Page.Response != null))
1395                                 Page.Response.Redirect (Page.Request.RawUrl);
1396                 }
1397
1398                 void UserName_TextChanged (object sender, EventArgs e)
1399                 {
1400                         UserName = ((ITextControl)sender).Text;
1401                 }
1402
1403                 void Password_TextChanged (object sender, EventArgs e)
1404                 {
1405                         _password = ((ITextControl)sender).Text;
1406                 }
1407
1408                 void RememberMe_CheckedChanged (object sender, EventArgs e)
1409                 {
1410                         RememberMeSet = ((ICheckBoxControl)sender).Checked;
1411                 }
1412         }
1413 }
1414