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