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