2 // System.Web.UI.WebControls.Login class
5 // Sebastien Pouliot <sebastien@ximian.com>
6 // Konstantin Triger <kostat@mainsoft.com>
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System
.Collections
;
32 using System
.Globalization
;
33 using System
.ComponentModel
;
34 using System
.Security
.Permissions
;
35 using System
.Web
.Security
;
36 using System
.Web
.Util
;
38 namespace System
.Web
.UI
.WebControls
{
41 [AspNetHostingPermission (SecurityAction
.LinkDemand
, Level
= AspNetHostingPermissionLevel
.Minimal
)]
42 [AspNetHostingPermission (SecurityAction
.InheritanceDemand
, Level
= AspNetHostingPermissionLevel
.Minimal
)]
45 [DefaultEvent ("Authenticate")]
46 [Designer ("System.Web.UI.Design.WebControls.LoginDesigner, " + Consts
.AssemblySystem_Design
, "System.ComponentModel.Design.IDesigner")]
47 public class Login
: CompositeControl
50 #region LoginContainer
51 // TODO: This class should probably be folded into a generic one with BaseChangePasswordContainer
52 sealed class LoginContainer
: Control
54 readonly Login _owner
;
55 bool renderOuterTable
;
57 TableCell _containerCell
;
59 public LoginContainer (Login owner
)
62 renderOuterTable
= _owner
.RenderOuterTable
;
68 public override string ID
{
77 public override string ClientID
{
79 return _owner
.ClientID
;
83 public void InstantiateTemplate (ITemplate template
)
85 if (!renderOuterTable
)
86 template
.InstantiateIn (this);
88 template
.InstantiateIn (_containerCell
);
93 _table
= new Table ();
94 _containerCell
= new TableCell ();
96 TableRow row
= new TableRow ();
97 row
.Cells
.Add (_containerCell
);
98 _table
.Rows
.Add (row
);
100 Controls
.AddAt (0, _table
);
103 protected internal override void Render (HtmlTextWriter writer
)
105 if (_table
!= null) {
106 _table
.CellSpacing
= 0;
107 _table
.CellPadding
= _owner
.BorderPadding
;
108 _table
.ApplyStyle (_owner
.ControlStyle
);
109 _table
.Attributes
.CopyFrom (_owner
.Attributes
);
112 base.Render (writer
);
115 public Control UserNameTextBox
{
117 return FindControl("UserName");
121 public Control PasswordTextBox
{
123 return FindControl("Password");
127 public Control RememberMeCheckBox
{
129 return FindControl("RememberMe");
133 public ITextControl FailureTextLiteral
136 return FindControl ("FailureText") as ITextControl
;
143 #region LoginTemplate
145 sealed class LoginTemplate
: WebControl
, ITemplate
147 readonly Login _login
;
149 public LoginTemplate (Login login
)
154 void ITemplate
.InstantiateIn (Control container
)
157 LiteralControl TitleText
= new LiteralControl (_login
.TitleText
);
160 LiteralControl InstructionText
= new LiteralControl (_login
.InstructionText
);
163 TextBox UserName
= new TextBox ();
164 UserName
.ID
= "UserName";
165 UserName
.Text
= _login
.UserName
;
166 _login
.RegisterApplyStyle (UserName
, _login
.TextBoxStyle
);
168 Label UserNameLabel
= new Label ();
169 UserNameLabel
.ID
= "UserNameLabel";
170 UserNameLabel
.AssociatedControlID
= "UserName";
171 UserNameLabel
.Text
= _login
.UserNameLabelText
;
173 RequiredFieldValidator UserNameRequired
= new RequiredFieldValidator ();
174 UserNameRequired
.ID
= "UserNameRequired";
175 UserNameRequired
.ControlToValidate
= "UserName";
176 UserNameRequired
.ErrorMessage
= _login
.UserNameRequiredErrorMessage
;
177 UserNameRequired
.ToolTip
= _login
.UserNameRequiredErrorMessage
;
178 UserNameRequired
.Text
= "*";
179 UserNameRequired
.ValidationGroup
= _login
.ID
;
180 _login
.RegisterApplyStyle (UserNameRequired
, _login
.ValidatorTextStyle
);
183 TextBox Password
= new TextBox ();
184 Password
.ID
= "Password";
185 Password
.TextMode
= TextBoxMode
.Password
;
186 _login
.RegisterApplyStyle (Password
, _login
.TextBoxStyle
);
188 Label PasswordLabel
= new Label ();
189 PasswordLabel
.ID
= "PasswordLabel";
190 PasswordLabel
.AssociatedControlID
= "PasswordLabel";
191 PasswordLabel
.Text
= _login
.PasswordLabelText
;
193 RequiredFieldValidator PasswordRequired
= new RequiredFieldValidator ();
194 PasswordRequired
.ID
= "PasswordRequired";
195 PasswordRequired
.ControlToValidate
= "Password";
196 PasswordRequired
.ErrorMessage
= _login
.PasswordRequiredErrorMessage
;
197 PasswordRequired
.ToolTip
= _login
.PasswordRequiredErrorMessage
;
198 PasswordRequired
.Text
= "*";
199 PasswordRequired
.ValidationGroup
= _login
.ID
;
200 _login
.RegisterApplyStyle (PasswordRequired
, _login
.ValidatorTextStyle
);
202 bool useRememberMe
= _login
!= null ? _login
.DisplayRememberMe
: true;
206 RememberMe
= new CheckBox ();
207 RememberMe
.ID
= "RememberMe";
208 RememberMe
.Checked
= _login
.RememberMeSet
;
209 RememberMe
.Text
= _login
.RememberMeText
;
210 _login
.RegisterApplyStyle (RememberMe
, _login
.CheckBoxStyle
);
215 Literal FailureText
= new Literal ();
216 FailureText
.ID
= "FailureText";
217 FailureText
.EnableViewState
= false;
220 WebControl LoginButton
= null;
221 switch (_login
.LoginButtonType
) {
222 case ButtonType
.Button
:
223 LoginButton
= new Button ();
224 LoginButton
.ID
= "LoginButton";
226 case ButtonType
.Link
:
227 LoginButton
= new LinkButton ();
228 LoginButton
.ID
= "LoginLinkButton";
230 case ButtonType
.Image
:
231 LoginButton
= new ImageButton ();
232 LoginButton
.ID
= "LoginImageButton";
235 _login
.RegisterApplyStyle (LoginButton
, _login
.LoginButtonStyle
);
236 LoginButton
.ID
= "LoginButton";
237 ((IButtonControl
) LoginButton
).Text
= _login
.LoginButtonText
;
238 ((IButtonControl
) LoginButton
).CommandName
= Login
.LoginButtonCommandName
;
239 ((IButtonControl
) LoginButton
).Command
+= new CommandEventHandler (_login
.LoginClick
);
240 ((IButtonControl
) LoginButton
).ValidationGroup
= _login
.ID
;
242 // Create layout table
243 Table table
= new Table ();
244 table
.CellPadding
= 0;
249 CreateCell (TitleText
, null, _login
.TitleTextStyle
, HorizontalAlign
.Center
)));
252 if (_login
.InstructionText
.Length
> 0) {
255 CreateCell (InstructionText
, null, _login
.instructionTextStyle
, HorizontalAlign
.Center
)));
258 if (_login
.Orientation
== Orientation
.Horizontal
) {
260 TableRow row1
= new TableRow ();
261 TableRow row2
= new TableRow ();
262 if (_login
.TextLayout
== LoginTextLayout
.TextOnTop
)
263 row1
.Cells
.Add (CreateCell (UserNameLabel
, null, _login
.LabelStyle
));
265 row2
.Cells
.Add (CreateCell (UserNameLabel
, null, _login
.LabelStyle
));
266 row2
.Cells
.Add (CreateCell (UserName
, UserNameRequired
, null));
267 if (_login
.TextLayout
== LoginTextLayout
.TextOnTop
)
268 row1
.Cells
.Add (CreateCell (PasswordLabel
, null, _login
.LabelStyle
));
270 row2
.Cells
.Add (CreateCell (PasswordLabel
, null, _login
.LabelStyle
));
271 row2
.Cells
.Add (CreateCell (Password
, PasswordRequired
, null));
273 row2
.Cells
.Add (CreateCell (RememberMe
, null, null));
274 row2
.Cells
.Add (CreateCell (LoginButton
, null, null));
275 if (row1
.Cells
.Count
> 0)
276 table
.Rows
.Add (row1
);
277 table
.Rows
.Add (row2
);
279 else { // Orientation.Vertical
280 if (_login
.TextLayout
== LoginTextLayout
.TextOnLeft
)
281 table
.Rows
.Add (CreateRow (UserNameLabel
, UserName
, UserNameRequired
, _login
.LabelStyle
));
283 table
.Rows
.Add (CreateRow (UserNameLabel
, null, null, _login
.LabelStyle
));
284 table
.Rows
.Add (CreateRow (null, UserName
, UserNameRequired
, null));
286 if (_login
.TextLayout
== LoginTextLayout
.TextOnLeft
)
287 table
.Rows
.Add (CreateRow (PasswordLabel
, Password
, PasswordRequired
, _login
.LabelStyle
));
289 table
.Rows
.Add (CreateRow (PasswordLabel
, null, null, _login
.LabelStyle
));
290 table
.Rows
.Add (CreateRow (null, Password
, PasswordRequired
, null));
293 table
.Rows
.Add (CreateRow (CreateCell (RememberMe
, null, null)));
294 table
.Rows
.Add (CreateRow (CreateCell (LoginButton
, null, null, HorizontalAlign
.Right
)));
298 if (_login
.FailureTextStyle
.ForeColor
.IsEmpty
)
299 _login
.FailureTextStyle
.ForeColor
= System
.Drawing
.Color
.Red
;
303 CreateCell (FailureText
, null, _login
.FailureTextStyle
)));
306 TableCell linksCell
= new TableCell ();
307 _login
.RegisterApplyStyle (linksCell
, _login
.HyperLinkStyle
);
309 if (AddLink (_login
.CreateUserUrl
, _login
.CreateUserText
, _login
.CreateUserIconUrl
, linksCell
, _login
.HyperLinkStyle
))
310 if (_login
.Orientation
== Orientation
.Vertical
)
311 linksCell
.Controls
.Add (new LiteralControl ("<br/>"));
313 linksCell
.Controls
.Add (new LiteralControl (" "));
315 if (AddLink (_login
.PasswordRecoveryUrl
, _login
.PasswordRecoveryText
, _login
.PasswordRecoveryIconUrl
, linksCell
, _login
.HyperLinkStyle
))
316 if (_login
.Orientation
== Orientation
.Vertical
)
317 linksCell
.Controls
.Add (new LiteralControl ("<br/>"));
319 linksCell
.Controls
.Add (new LiteralControl (" "));
321 AddLink (_login
.HelpPageUrl
, _login
.HelpPageText
, _login
.HelpPageIconUrl
, linksCell
, _login
.HyperLinkStyle
);
322 table
.Rows
.Add (CreateRow (linksCell
));
324 FixTableColumnSpans (table
);
325 container
.Controls
.Add (table
);
328 TableRow
CreateRow (TableCell cell
)
330 TableRow row
= new TableRow ();
331 row
.Cells
.Add (cell
);
335 TableRow
CreateRow (Control c0
, Control c1
, Control c2
, Style s
)
337 TableRow row
= new TableRow ();
338 TableCell cell0
= new TableCell ();
339 TableCell cell1
= new TableCell ();
342 cell0
.Controls
.Add (c0
);
343 row
.Controls
.Add (cell0
);
347 cell0
.ApplyStyle (s
);
349 if ((c1
!= null) && (c2
!= null)) {
350 cell1
.Controls
.Add (c1
);
351 cell1
.Controls
.Add (c2
);
352 cell0
.HorizontalAlign
= HorizontalAlign
.Right
;
353 row
.Controls
.Add (cell1
);
358 TableCell
CreateCell (Control c0
, Control c1
, Style s
, HorizontalAlign align
)
360 TableCell cell
= CreateCell (c0
, c1
, s
);
361 cell
.HorizontalAlign
= align
;
365 TableCell
CreateCell (Control c0
, Control c1
, Style s
)
367 TableCell cell
= new TableCell ();
371 cell
.Controls
.Add (c0
);
373 cell
.Controls
.Add (c1
);
378 bool AddLink (string pageUrl
, string linkText
, string linkIcon
, WebControl container
, Style style
)
381 if (linkIcon
.Length
> 0) {
382 Image img
= new Image ();
383 img
.ImageUrl
= linkIcon
;
384 container
.Controls
.Add (img
);
387 if (linkText
.Length
> 0) {
388 HyperLink link
= new HyperLink ();
389 link
.NavigateUrl
= pageUrl
;
390 link
.Text
= linkText
;
391 _login
.RegisterApplyStyle (link
, style
);
392 container
.Controls
.Add (link
);
398 void FixTableColumnSpans (Table table
)
401 for (int row
= 0; row
< table
.Rows
.Count
; row
++) {
402 if (maxCols
< table
.Rows
[row
].Cells
.Count
)
403 maxCols
= table
.Rows
[row
].Cells
.Count
;
405 for (int row
= 0; row
< table
.Rows
.Count
; row
++) {
406 if (table
.Rows
[row
].Cells
.Count
== 1 && maxCols
> 1)
407 table
.Rows
[row
].Cells
[0].ColumnSpan
= maxCols
;
414 public static readonly string LoginButtonCommandName
= "Login";
416 static readonly object authenticateEvent
= new object ();
417 static readonly object loggedInEvent
= new object ();
418 static readonly object loggingInEvent
= new object ();
419 static readonly object loginErrorEvent
= new object ();
421 TableItemStyle checkBoxStyle
;
422 TableItemStyle failureTextStyle
;
423 TableItemStyle hyperLinkStyle
;
424 TableItemStyle instructionTextStyle
;
425 TableItemStyle labelStyle
;
426 Style logonButtonStyle
;
428 TableItemStyle titleTextStyle
;
429 Style validatorTextStyle
;
430 ArrayList styles
= new ArrayList ();
432 ITemplate layoutTemplate
;
433 LoginContainer container
;
436 bool renderOuterTable
= true;
442 public virtual int BorderPadding
{
444 object o
= ViewState
["BorderPadding"];
445 return (o
== null) ? 1 : (int) o
;
449 throw new ArgumentOutOfRangeException ("BorderPadding", "< -1");
451 ViewState
["BorderPadding"] = value;
455 [DefaultValue (null)]
456 [DesignerSerializationVisibility (DesignerSerializationVisibility
.Content
)]
457 [NotifyParentProperty (true)]
458 [PersistenceMode (PersistenceMode
.InnerProperty
)]
459 public TableItemStyle CheckBoxStyle
{
461 if (checkBoxStyle
== null) {
462 checkBoxStyle
= new TableItemStyle ();
463 if (IsTrackingViewState
) {
464 (checkBoxStyle
as IStateManager
).TrackViewState ();
467 return checkBoxStyle
;
472 [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts
.AssemblySystem_Design
, "System.Drawing.Design.UITypeEditor, " + Consts
.AssemblySystem_Drawing
)]
474 public virtual string CreateUserIconUrl
{
476 object o
= ViewState
["CreateUserIconUrl"];
477 return (o
== null) ? String
.Empty
: (string) o
;
481 ViewState
.Remove ("CreateUserIconUrl");
483 ViewState
["CreateUserIconUrl"] = value;
489 public virtual string CreateUserText
{
491 object o
= ViewState
["CreateUserText"];
492 return (o
== null) ? String
.Empty
: (string) o
;
496 ViewState
.Remove ("CreateUserText");
498 ViewState
["CreateUserText"] = value;
503 [Editor ("System.Web.UI.Design.UrlEditor, " + Consts
.AssemblySystem_Design
, "System.Drawing.Design.UITypeEditor, " + Consts
.AssemblySystem_Drawing
)]
505 public virtual string CreateUserUrl
{
507 object o
= ViewState
["CreateUserUrl"];
508 return (o
== null) ? String
.Empty
: (string) o
;
512 ViewState
.Remove ("CreateUserUrl");
514 ViewState
["CreateUserUrl"] = value;
519 [Editor ("System.Web.UI.Design.UrlEditor, " + Consts
.AssemblySystem_Design
, "System.Drawing.Design.UITypeEditor, " + Consts
.AssemblySystem_Drawing
)]
522 public virtual string DestinationPageUrl
{
524 object o
= ViewState
["DestinationPageUrl"];
525 return (o
== null) ? String
.Empty
: (string) o
;
529 ViewState
.Remove ("DestinationPageUrl");
531 ViewState
["DestinationPageUrl"] = value;
535 [DefaultValue (true)]
537 public virtual bool DisplayRememberMe
{
539 object o
= ViewState
["DisplayRememberMe"];
540 return (o
== null) ? true : (bool) o
;
543 ViewState
["DisplayRememberMe"] = value;
547 [DefaultValue (LoginFailureAction
.Refresh
)]
549 [MonoTODO ("RedirectToLoginPage not yet implemented in FormsAuthentication")]
550 public virtual LoginFailureAction FailureAction
{
552 object o
= ViewState
["FailureAction"];
553 return (o
== null) ? LoginFailureAction
.Refresh
: (LoginFailureAction
) o
;
556 if ((value < LoginFailureAction
.Refresh
) || (value > LoginFailureAction
.RedirectToLoginPage
))
557 throw new ArgumentOutOfRangeException ("FailureAction");
558 ViewState
["FailureAction"] = (int) value;
563 public virtual string FailureText
{
565 object o
= ViewState
["FailureText"];
566 return (o
== null) ? Locale
.GetText ("Your login attempt was not successful. Please try again.") : (string) o
;
570 ViewState
.Remove ("FailureText");
572 ViewState
["FailureText"] = value;
576 [DefaultValue (null)]
577 [DesignerSerializationVisibility (DesignerSerializationVisibility
.Content
)]
578 [NotifyParentProperty (true)]
579 [PersistenceMode (PersistenceMode
.InnerProperty
)]
580 public TableItemStyle FailureTextStyle
{
582 if (failureTextStyle
== null) {
583 failureTextStyle
= new TableItemStyle ();
584 if (IsTrackingViewState
) {
585 (failureTextStyle
as IStateManager
).TrackViewState ();
588 return failureTextStyle
;
593 [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts
.AssemblySystem_Design
, "System.Drawing.Design.UITypeEditor, " + Consts
.AssemblySystem_Drawing
)]
595 public virtual string HelpPageIconUrl
{
597 object o
= ViewState
["HelpPageIconUrl"];
598 return (o
== null) ? String
.Empty
: (string) o
;
602 ViewState
.Remove ("HelpPageIconUrl");
604 ViewState
["HelpPageIconUrl"] = value;
610 public virtual string HelpPageText
{
612 object o
= ViewState
["HelpPageText"];
613 return (o
== null) ? String
.Empty
: (string) o
;
617 ViewState
.Remove ("HelpPageText");
619 ViewState
["HelpPageText"] = value;
624 [Editor ("System.Web.UI.Design.UrlEditor, " + Consts
.AssemblySystem_Design
, "System.Drawing.Design.UITypeEditor, " + Consts
.AssemblySystem_Drawing
)]
626 public virtual string HelpPageUrl
{
628 object o
= ViewState
["HelpPageUrl"];
629 return (o
== null) ? String
.Empty
: (string) o
;
633 ViewState
.Remove ("HelpPageUrl");
635 ViewState
["HelpPageUrl"] = value;
639 [DefaultValue (null)]
640 [DesignerSerializationVisibility (DesignerSerializationVisibility
.Content
)]
641 [NotifyParentProperty (true)]
642 [PersistenceMode (PersistenceMode
.InnerProperty
)]
643 public TableItemStyle HyperLinkStyle
{
645 if (hyperLinkStyle
== null) {
646 hyperLinkStyle
= new TableItemStyle ();
647 if (IsTrackingViewState
) {
648 (hyperLinkStyle
as IStateManager
).TrackViewState ();
651 return hyperLinkStyle
;
657 public virtual string InstructionText
{
659 object o
= ViewState
["InstructionText"];
660 return (o
== null) ? String
.Empty
: (string) o
;
664 ViewState
.Remove ("InstructionText");
666 ViewState
["InstructionText"] = value;
670 [DefaultValue (null)]
671 [DesignerSerializationVisibility (DesignerSerializationVisibility
.Content
)]
672 [NotifyParentProperty (true)]
673 [PersistenceMode (PersistenceMode
.InnerProperty
)]
674 public TableItemStyle InstructionTextStyle
{
676 if (instructionTextStyle
== null) {
677 instructionTextStyle
= new TableItemStyle ();
678 if (IsTrackingViewState
) {
679 (instructionTextStyle
as IStateManager
).TrackViewState ();
682 return instructionTextStyle
;
686 [DefaultValue (null)]
687 [DesignerSerializationVisibility (DesignerSerializationVisibility
.Content
)]
688 [NotifyParentProperty (true)]
689 [PersistenceMode (PersistenceMode
.InnerProperty
)]
690 public TableItemStyle LabelStyle
{
692 if (labelStyle
== null) {
693 labelStyle
= new TableItemStyle ();
694 if (IsTrackingViewState
) {
695 (labelStyle
as IStateManager
).TrackViewState ();
703 [TemplateContainer (typeof (Login
))]
704 [PersistenceMode (PersistenceMode
.InnerProperty
)]
705 public virtual ITemplate LayoutTemplate
{
706 get { return layoutTemplate; }
707 set { layoutTemplate = value; }
711 [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts
.AssemblySystem_Design
, "System.Drawing.Design.UITypeEditor, " + Consts
.AssemblySystem_Drawing
)]
713 public virtual string LoginButtonImageUrl
{
715 object o
= ViewState
["LoginButtonImageUrl"];
716 return (o
== null) ? String
.Empty
: (string) o
;
720 ViewState
.Remove ("LoginButtonImageUrl");
722 ViewState
["LoginButtonImageUrl"] = value;
726 [DefaultValue (null)]
727 [DesignerSerializationVisibility (DesignerSerializationVisibility
.Content
)]
728 [NotifyParentProperty (true)]
729 [PersistenceMode (PersistenceMode
.InnerProperty
)]
730 public Style LoginButtonStyle
{
732 if (logonButtonStyle
== null) {
733 logonButtonStyle
= new Style ();
734 if (IsTrackingViewState
) {
735 (logonButtonStyle
as IStateManager
).TrackViewState ();
738 return logonButtonStyle
;
743 public virtual string LoginButtonText
{
745 object o
= ViewState
["LoginButtonText"];
746 return (o
== null) ? Locale
.GetText ("Log In") : (string) o
;
750 ViewState
.Remove ("LoginButtonText");
752 ViewState
["LoginButtonText"] = value;
756 [DefaultValue (ButtonType
.Button
)]
757 public virtual ButtonType LoginButtonType
{
759 object o
= ViewState
["LoginButtonType"];
760 return (o
== null) ? ButtonType
.Button
: (ButtonType
) o
;
763 if ((value < ButtonType
.Button
) || (value > ButtonType
.Link
))
764 throw new ArgumentOutOfRangeException ("LoginButtonType");
765 ViewState
["LoginButtonType"] = (int) value;
771 public virtual string MembershipProvider
{
773 object o
= ViewState
["MembershipProvider"];
774 return (o
== null) ? String
.Empty
: (string) o
;
778 ViewState
.Remove ("MembershipProvider");
780 ViewState
["MembershipProvider"] = value;
784 [DefaultValue (Orientation
.Vertical
)]
785 public virtual Orientation Orientation
{
787 object o
= ViewState
["Orientation"];
788 return (o
== null) ? Orientation
.Vertical
: (Orientation
) o
;
791 if ((value < Orientation
.Horizontal
) || (value > Orientation
.Vertical
))
792 throw new ArgumentOutOfRangeException ("Orientation");
793 ViewState
["Orientation"] = (int) value;
798 [DesignerSerializationVisibility (DesignerSerializationVisibility
.Hidden
)]
799 public virtual string Password
{
801 return _password
!= null ? _password
: String
.Empty
;
806 public virtual string PasswordLabelText
{
808 object o
= ViewState
["PasswordLabelText"];
809 return (o
== null) ? "Password:" : (string) o
;
813 ViewState
.Remove ("PasswordLabelText");
815 ViewState
["PasswordLabelText"] = value;
820 [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts
.AssemblySystem_Design
, "System.Drawing.Design.UITypeEditor, " + Consts
.AssemblySystem_Drawing
)]
822 public virtual string PasswordRecoveryIconUrl
{
824 object o
= ViewState
["PasswordRecoveryIconUrl"];
825 return (o
== null) ? String
.Empty
: (string) o
;
829 ViewState
.Remove ("PasswordRecoveryIconUrl");
831 ViewState
["PasswordRecoveryIconUrl"] = value;
837 public virtual string PasswordRecoveryText
{
839 object o
= ViewState
["PasswordRecoveryText"];
840 return (o
== null) ? String
.Empty
: (string) o
;
844 ViewState
.Remove ("PasswordRecoveryText");
846 ViewState
["PasswordRecoveryText"] = value;
851 [Editor ("System.Web.UI.Design.UrlEditor, " + Consts
.AssemblySystem_Design
, "System.Drawing.Design.UITypeEditor, " + Consts
.AssemblySystem_Drawing
)]
853 public virtual string PasswordRecoveryUrl
{
855 object o
= ViewState
["PasswordRecoveryUrl"];
856 return (o
== null) ? String
.Empty
: (string) o
;
860 ViewState
.Remove ("PasswordRecoveryUrl");
862 ViewState
["PasswordRecoveryUrl"] = value;
867 public virtual string PasswordRequiredErrorMessage
{
869 object o
= ViewState
["PasswordRequiredErrorMessage"];
870 return (o
== null) ? Locale
.GetText ("Password is required.") : (string) o
;
874 ViewState
.Remove ("PasswordRequiredErrorMessage");
876 ViewState
["PasswordRequiredErrorMessage"] = value;
879 [DefaultValue (true)]
880 public virtual bool RenderOuterTable
{
881 get { return renderOuterTable; }
882 set { renderOuterTable = value; }
884 [DefaultValue (false)]
886 public virtual bool RememberMeSet
{
888 object o
= ViewState
["RememberMeSet"];
889 return (o
== null) ? false : (bool) o
;
892 ViewState
["RememberMeSet"] = value;
897 public virtual string RememberMeText
{
899 object o
= ViewState
["RememberMeText"];
900 return (o
== null) ? Locale
.GetText ("Remember me next time.") : (string) o
;
904 ViewState
.Remove ("RememberMeText");
906 ViewState
["RememberMeText"] = value;
910 protected override HtmlTextWriterTag TagKey
{
911 get { return HtmlTextWriterTag.Table; }
914 [DefaultValue (null)]
915 [DesignerSerializationVisibility (DesignerSerializationVisibility
.Content
)]
916 [NotifyParentProperty (true)]
917 [PersistenceMode (PersistenceMode
.InnerProperty
)]
918 public Style TextBoxStyle
{
920 if (textBoxStyle
== null) {
921 textBoxStyle
= new Style ();
922 if (IsTrackingViewState
) {
923 (textBoxStyle
as IStateManager
).TrackViewState ();
930 [DefaultValue (LoginTextLayout
.TextOnLeft
)]
931 public virtual LoginTextLayout TextLayout
{
933 object o
= ViewState
["TextLayout"];
934 return (o
== null) ? LoginTextLayout
.TextOnLeft
: (LoginTextLayout
) o
;
937 if ((value < LoginTextLayout
.TextOnLeft
) || (value > LoginTextLayout
.TextOnTop
))
938 throw new ArgumentOutOfRangeException ("TextLayout");
939 ViewState
["TextLayout"] = (int) value;
944 public virtual string TitleText
{
946 object o
= ViewState
["TitleText"];
947 return (o
== null) ? Locale
.GetText ("Log In") : (string) o
;
951 ViewState
.Remove ("TitleText");
953 ViewState
["TitleText"] = value;
957 [DefaultValue (null)]
958 [DesignerSerializationVisibility (DesignerSerializationVisibility
.Content
)]
959 [NotifyParentProperty (true)]
960 [PersistenceMode (PersistenceMode
.InnerProperty
)]
961 public TableItemStyle TitleTextStyle
{
963 if (titleTextStyle
== null) {
964 titleTextStyle
= new TableItemStyle ();
965 if (IsTrackingViewState
) {
966 (titleTextStyle
as IStateManager
).TrackViewState ();
969 return titleTextStyle
;
974 public virtual string UserName
{
976 object o
= ViewState
["UserName"];
977 return (o
== null) ? String
.Empty
: (string) o
;
981 ViewState
.Remove ("UserName");
983 ViewState
["UserName"] = value;
988 public virtual string UserNameLabelText
{
990 object o
= ViewState
["UserNameLabelText"];
991 return (o
== null) ? Locale
.GetText ("User Name:") : (string) o
;
995 ViewState
.Remove ("UserNameLabelText");
997 ViewState
["UserNameLabelText"] = value;
1001 [Localizable (true)]
1002 public virtual string UserNameRequiredErrorMessage
{
1004 object o
= ViewState
["UserNameRequiredErrorMessage"];
1005 return (o
== null) ? Locale
.GetText ("User Name is required.") : (string) o
;
1009 ViewState
.Remove ("UserNameRequiredErrorMessage");
1011 ViewState
["UserNameRequiredErrorMessage"] = value;
1015 [DefaultValue (null)]
1016 [DesignerSerializationVisibility (DesignerSerializationVisibility
.Content
)]
1017 [NotifyParentProperty (true)]
1018 [PersistenceMode (PersistenceMode
.InnerProperty
)]
1019 public Style ValidatorTextStyle
{
1021 if (validatorTextStyle
== null) {
1022 validatorTextStyle
= new Style ();
1023 if (IsTrackingViewState
) {
1024 (validatorTextStyle
as IStateManager
).TrackViewState ();
1027 return validatorTextStyle
;
1031 [DefaultValue (true)]
1033 public virtual bool VisibleWhenLoggedIn
{
1035 object o
= ViewState
["VisibleWhenLoggedIn"];
1036 return (o
== null) ? true : (bool) o
;
1039 ViewState
["VisibleWhenLoggedIn"] = value;
1043 LoginContainer LoginTemplateContainer
1046 if (container
== null)
1047 container
= new LoginContainer (this);
1055 protected internal override void CreateChildControls ()
1059 ITemplate template
= LayoutTemplate
;
1060 if (template
== null)
1061 template
= new LoginTemplate (this);
1063 LoginTemplateContainer
.InstantiateTemplate (template
);
1065 Controls
.Add (container
);
1067 IEditableTextControl editable
;
1068 editable
= container
.UserNameTextBox
as IEditableTextControl
;
1070 if (editable
!= null) {
1071 editable
.Text
= UserName
;
1072 editable
.TextChanged
+= new EventHandler (UserName_TextChanged
);
1075 throw new HttpException ("LayoutTemplate does not contain an IEditableTextControl with ID UserName for the username.");
1077 editable
= container
.PasswordTextBox
as IEditableTextControl
;
1079 if (editable
!= null)
1080 editable
.TextChanged
+= new EventHandler (Password_TextChanged
);
1082 throw new HttpException ("LayoutTemplate does not contain an IEditableTextControl with ID Password for the password.");
1084 ICheckBoxControl checkBox
= container
.RememberMeCheckBox
as ICheckBoxControl
;
1086 if (checkBox
!= null)
1087 checkBox
.CheckedChanged
+= new EventHandler (RememberMe_CheckedChanged
);
1090 protected override void LoadViewState (object savedState
)
1092 if (savedState
== null) {
1093 base.LoadViewState (null);
1097 object[] state
= (object[]) savedState
;
1098 base.LoadViewState (state
[0]);
1099 if (state
[1] != null)
1100 (LoginButtonStyle
as IStateManager
).LoadViewState (state
[1]);
1101 if (state
[2] != null)
1102 (LabelStyle
as IStateManager
).LoadViewState (state
[2]);
1103 if (state
[3] != null)
1104 (TextBoxStyle
as IStateManager
).LoadViewState (state
[3]);
1105 if (state
[4] != null)
1106 (HyperLinkStyle
as IStateManager
).LoadViewState (state
[4]);
1107 if (state
[5] != null)
1108 (InstructionTextStyle
as IStateManager
).LoadViewState (state
[5]);
1109 if (state
[6] != null)
1110 (TitleTextStyle
as IStateManager
).LoadViewState (state
[6]);
1111 if (state
[7] != null)
1112 (CheckBoxStyle
as IStateManager
).LoadViewState (state
[7]);
1113 if (state
[8] != null)
1114 (FailureTextStyle
as IStateManager
).LoadViewState (state
[8]);
1115 if (state
[9] != null)
1116 (ValidatorTextStyle
as IStateManager
).LoadViewState (state
[9]);
1119 bool HasOnAuthenticateHandler ()
1121 return Events
[authenticateEvent
] != null;
1124 protected virtual void OnAuthenticate (AuthenticateEventArgs e
)
1126 // this gets called after OnLoggingIn and the authentication so we can change the result
1127 AuthenticateEventHandler authenticate
= (AuthenticateEventHandler
) Events
[authenticateEvent
];
1128 if (authenticate
!= null)
1129 authenticate (this, e
);
1132 protected override bool OnBubbleEvent (object source
, EventArgs e
)
1134 // check for submit button
1135 CommandEventArgs cea
= (e
as CommandEventArgs
);
1136 if ((cea
!= null) &&
1137 String
.Equals (cea
.CommandName
, LoginButtonCommandName
, StringComparison
.InvariantCultureIgnoreCase
)) {
1138 if (!AuthenticateUser ()) {
1139 ITextControl failureText
= LoginTemplateContainer
.FailureTextLiteral
;
1140 if (failureText
!= null)
1141 failureText
.Text
= FailureText
;
1148 protected virtual void OnLoggedIn (EventArgs e
)
1150 // this gets called only if the authentication was successful
1151 EventHandler loggedIn
= (EventHandler
) Events
[loggedInEvent
];
1152 if (loggedIn
!= null)
1156 protected virtual void OnLoggingIn (LoginCancelEventArgs e
)
1158 // this gets called before OnAuthenticate so we can abort the authentication process
1159 LoginCancelEventHandler loggingIn
= (LoginCancelEventHandler
) Events
[loggingInEvent
];
1160 if (loggingIn
!= null)
1161 loggingIn (this, e
);
1164 protected virtual void OnLoginError (EventArgs e
)
1166 // this gets called only if the authentication wasn't successful
1167 EventHandler loginError
= (EventHandler
) Events
[loginErrorEvent
];
1168 if (loginError
!= null)
1169 loginError (this, e
);
1172 [MonoTODO ("overriden for ?")]
1173 protected internal override void OnPreRender (EventArgs e
)
1175 base.OnPreRender (e
);
1176 // note: doc says that UserName and Password aren't available at
1177 // PageLoad but are during PreRender phase, so... ???
1180 protected internal override void Render (HtmlTextWriter writer
)
1182 VerifyInlinePropertiesNotSet ();
1183 // VisibleWhenLoggedIn isn't applicable to the default login page
1184 if (!VisibleWhenLoggedIn
&& !IsDefaultLoginPage () && IsLoggedIn ())
1189 page
.VerifyRenderingInServerForm (this);
1191 EnsureChildControls ();
1193 foreach (object [] styleDef
in styles
)
1194 ((WebControl
) styleDef
[0]).ApplyStyle ((Style
) styleDef
[1]);
1196 RenderContents(writer
);
1199 protected override object SaveViewState ()
1201 object[] state
= new object [10];
1202 state
[0] = base.SaveViewState ();
1203 if (logonButtonStyle
!= null)
1204 state
[1] = (logonButtonStyle
as IStateManager
).SaveViewState ();
1205 if (labelStyle
!= null)
1206 state
[2] = (labelStyle
as IStateManager
).SaveViewState ();
1207 if (textBoxStyle
!= null)
1208 state
[3] = (textBoxStyle
as IStateManager
).SaveViewState ();
1209 if (hyperLinkStyle
!= null)
1210 state
[4] = (hyperLinkStyle
as IStateManager
).SaveViewState ();
1211 if (instructionTextStyle
!= null)
1212 state
[5] = (instructionTextStyle
as IStateManager
).SaveViewState ();
1213 if (titleTextStyle
!= null)
1214 state
[6] = (titleTextStyle
as IStateManager
).SaveViewState ();
1215 if (checkBoxStyle
!= null)
1216 state
[7] = (checkBoxStyle
as IStateManager
).SaveViewState ();
1217 if (failureTextStyle
!= null)
1218 state
[8] = (failureTextStyle
as IStateManager
).SaveViewState ();
1219 if (validatorTextStyle
!= null)
1220 state
[9] = (validatorTextStyle
as IStateManager
).SaveViewState ();
1222 for (int i
=0; i
< state
.Length
; i
++) {
1223 if (state
[0] != null)
1224 return (object) state
;
1226 return null; // reduce view state
1229 [MonoTODO ("for design-time usage - no more details available")]
1230 protected override void SetDesignModeState (IDictionary data
)
1232 base.SetDesignModeState (data
);
1235 protected override void TrackViewState ()
1237 base.TrackViewState ();
1238 if (logonButtonStyle
!= null)
1239 (logonButtonStyle
as IStateManager
).TrackViewState ();
1240 if (labelStyle
!= null)
1241 (labelStyle
as IStateManager
).TrackViewState ();
1242 if (textBoxStyle
!= null)
1243 (textBoxStyle
as IStateManager
).TrackViewState ();
1244 if (hyperLinkStyle
!= null)
1245 (hyperLinkStyle
as IStateManager
).TrackViewState ();
1246 if (instructionTextStyle
!= null)
1247 (instructionTextStyle
as IStateManager
).TrackViewState ();
1248 if (titleTextStyle
!= null)
1249 (titleTextStyle
as IStateManager
).TrackViewState ();
1250 if (checkBoxStyle
!= null)
1251 (checkBoxStyle
as IStateManager
).TrackViewState ();
1252 if (failureTextStyle
!= null)
1253 (failureTextStyle
as IStateManager
).TrackViewState ();
1254 if (validatorTextStyle
!= null)
1255 (validatorTextStyle
as IStateManager
).TrackViewState ();
1261 public event AuthenticateEventHandler Authenticate
{
1262 add { Events.AddHandler (authenticateEvent, value); }
1263 remove { Events.RemoveHandler (authenticateEvent, value); }
1266 public event EventHandler LoggedIn
{
1267 add { Events.AddHandler (loggedInEvent, value); }
1268 remove { Events.RemoveHandler (loggedInEvent, value); }
1271 public event LoginCancelEventHandler LoggingIn
{
1272 add { Events.AddHandler (loggingInEvent, value); }
1273 remove { Events.RemoveHandler (loggingInEvent, value); }
1276 public event EventHandler LoginError
{
1277 add { Events.AddHandler (loginErrorEvent, value); }
1278 remove { Events.RemoveHandler (loginErrorEvent, value); }
1284 internal void RegisterApplyStyle (WebControl control
, Style style
)
1286 styles
.Add (new object [] { control, style }
);
1289 bool AuthenticateUser ()
1294 LoginCancelEventArgs lcea
= new LoginCancelEventArgs ();
1299 AuthenticateEventArgs aea
= new AuthenticateEventArgs ();
1301 if (!HasOnAuthenticateHandler ()) {
1302 string mp
= MembershipProvider
;
1303 MembershipProvider provider
= (mp
.Length
== 0) ?
1304 provider
= Membership
.Provider
: Membership
.Providers
[mp
];
1305 if (provider
== null) {
1306 throw new HttpException (Locale
.GetText ("No provider named '{0}' could be found.", mp
));
1309 aea
.Authenticated
= provider
.ValidateUser (UserName
, Password
);
1311 OnAuthenticate (aea
);
1313 if (aea
.Authenticated
) {
1314 FormsAuthentication
.SetAuthCookie (UserName
, RememberMeSet
);
1315 OnLoggedIn (EventArgs
.Empty
);
1317 string url
= DestinationPageUrl
;
1318 if (Page
.Request
.Path
.StartsWith (FormsAuthentication
.LoginUrl
, StringComparison
.InvariantCultureIgnoreCase
)) {
1319 if (!String
.IsNullOrEmpty (FormsAuthentication
.ReturnUrl
))
1320 Redirect (FormsAuthentication
.ReturnUrl
);
1321 else if (!String
.IsNullOrEmpty (DestinationPageUrl
))
1323 else if (!String
.IsNullOrEmpty (FormsAuthentication
.DefaultUrl
))
1324 Redirect (FormsAuthentication
.DefaultUrl
);
1325 else if (url
.Length
== 0)
1328 else if (!String
.IsNullOrEmpty (DestinationPageUrl
)) {
1337 OnLoginError (EventArgs
.Empty
);
1338 if (FailureAction
== LoginFailureAction
.RedirectToLoginPage
) {
1339 // login page is defined in web.config
1340 FormsAuthentication
.RedirectToLoginPage ();
1346 // TODO: its called from default template only, not usefully, OnBubbleEvent
1347 // do handle command, need be removed
1349 void LoginClick (object sender
, CommandEventArgs e
)
1351 RaiseBubbleEvent (sender
, (EventArgs
)e
);
1354 bool IsDefaultLoginPage ()
1356 if ((Page
== null) || (Page
.Request
== null))
1358 string defaultLogin
= FormsAuthentication
.LoginUrl
;
1359 if (defaultLogin
== null)
1361 string url
= Page
.Request
.Url
.AbsolutePath
;
1362 return (String
.Compare (defaultLogin
, 0, url
, url
.Length
- defaultLogin
.Length
, defaultLogin
.Length
,
1363 true, Helpers
.InvariantCulture
) == 0);
1368 if ((Page
== null) || (Page
.Request
== null))
1370 return Page
.Request
.IsAuthenticated
;
1373 void Redirect (string url
)
1375 if ((Page
!= null) && (Page
.Response
!= null))
1376 Page
.Response
.Redirect (url
);
1380 if ((Page
!= null) && (Page
.Response
!= null))
1381 Page
.Response
.Redirect (Page
.Request
.RawUrl
);
1384 void UserName_TextChanged (object sender
, EventArgs e
)
1386 UserName
= ((ITextControl
)sender
).Text
;
1389 void Password_TextChanged (object sender
, EventArgs e
)
1391 _password
= ((ITextControl
)sender
).Text
;
1394 void RememberMe_CheckedChanged (object sender
, EventArgs e
)
1396 RememberMeSet
= ((ICheckBoxControl
)sender
).Checked
;