| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375 |
- //
- // System.Web.UI.WebControls.Login class
- //
- // Author:
- // Sebastien Pouliot <[email protected]>
- // Konstantin Triger <[email protected]>
- //
- // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- #if NET_2_0
- using System.Collections;
- using System.Globalization;
- using System.ComponentModel;
- using System.Security.Permissions;
- using System.Web.Security;
- namespace System.Web.UI.WebControls {
- // CAS
- [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
- [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
- // attributes
- [Bindable (false)]
- [DefaultEvent ("Authenticate")]
- [Designer ("System.Web.UI.Design.WebControls.LoginDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
- public class Login : CompositeControl {
- #region LoginContainer
- sealed class LoginContainer : Table
- {
- private readonly Login _owner = null;
- TableCell _containerCell = null;
- public LoginContainer (Login owner)
- {
- _owner = owner;
- InitTable ();
- }
-
- public override string ID {
- get {
- return _owner.ID;
- }
- set {
- _owner.ID = value;
- }
- }
-
- public override string ClientID {
- get {
- return _owner.ClientID;
- }
- }
- public void InstantiateTemplate (ITemplate template)
- {
- template.InstantiateIn (_containerCell);
- }
- private void InitTable ()
- {
- _containerCell = new TableCell ();
- TableRow row = new TableRow ();
- row.Cells.Add (_containerCell);
- Rows.Add (row);
- }
- protected internal override void Render (HtmlTextWriter writer) {
- CellSpacing = 0;
- CellPadding = _owner.BorderPadding;
- ApplyStyle (_owner.ControlStyle);
- Attributes.CopyFrom (_owner.Attributes);
- base.Render (writer);
- }
-
- public Control UserNameTextBox {
- get {
- return FindControl("UserName");
- }
- }
- public Control PasswordTextBox {
- get {
- return FindControl("Password");
- }
- }
- public Control RememberMeCheckBox {
- get {
- return FindControl("RememberMe");
- }
- }
- public ITextControl FailureTextLiteral
- {
- get {
- return FindControl ("FailureText") as ITextControl;
- }
- }
- }
- #endregion
- #region LoginTemplate
- sealed class LoginTemplate : WebControl, ITemplate
- {
- readonly Login _login;
- public LoginTemplate (Login login)
- {
- _login = login;
- }
- void ITemplate.InstantiateIn (Control container)
- {
- //
- LiteralControl TitleText = new LiteralControl (_login.TitleText);
- //
- LiteralControl InstructionText = new LiteralControl (_login.InstructionText);
- //
- TextBox UserName = new TextBox ();
- UserName.ID = "UserName";
- UserName.Text = _login.UserName;
- _login.RegisterApplyStyle (UserName, _login.TextBoxStyle);
- Label UserNameLabel = new Label ();
- UserNameLabel.ID = "UserNameLabel";
- UserNameLabel.AssociatedControlID = "UserName";
- UserNameLabel.Text = _login.UserNameLabelText;
- RequiredFieldValidator UserNameRequired = new RequiredFieldValidator ();
- UserNameRequired.ID = "UserNameRequired";
- UserNameRequired.ControlToValidate = "UserName";
- UserNameRequired.ErrorMessage = _login.UserNameRequiredErrorMessage;
- UserNameRequired.ToolTip = _login.UserNameRequiredErrorMessage;
- UserNameRequired.Text = "*";
- UserNameRequired.ValidationGroup = _login.ID;
- _login.RegisterApplyStyle (UserNameRequired, _login.ValidatorTextStyle);
- //
- TextBox Password = new TextBox ();
- Password.ID = "Password";
- Password.TextMode = TextBoxMode.Password;
- _login.RegisterApplyStyle (Password, _login.TextBoxStyle);
- Label PasswordLabel = new Label ();
- PasswordLabel.ID = "PasswordLabel";
- PasswordLabel.AssociatedControlID = "PasswordLabel";
- PasswordLabel.Text = _login.PasswordLabelText;
- RequiredFieldValidator PasswordRequired = new RequiredFieldValidator ();
- PasswordRequired.ID = "PasswordRequired";
- PasswordRequired.ControlToValidate = "Password";
- PasswordRequired.ErrorMessage = _login.PasswordRequiredErrorMessage;
- PasswordRequired.ToolTip = _login.PasswordRequiredErrorMessage;
- PasswordRequired.Text = "*";
- PasswordRequired.ValidationGroup = _login.ID;
- _login.RegisterApplyStyle (PasswordRequired, _login.ValidatorTextStyle);
- //
- CheckBox RememberMe = new CheckBox ();
- RememberMe.ID = "RememberMe";
- RememberMe.Checked = _login.RememberMeSet;
- RememberMe.Text = _login.RememberMeText;
- _login.RegisterApplyStyle (RememberMe, _login.CheckBoxStyle);
- // TODO: Error text
- Literal FailureText = new Literal ();
- FailureText.ID = "FailureText";
- FailureText.EnableViewState = false;
- //
- WebControl LoginButton = null;
- switch (_login.LoginButtonType) {
- case ButtonType.Button:
- LoginButton = new Button ();
- LoginButton.ID = "LoginButton";
- break;
- case ButtonType.Link:
- LoginButton = new LinkButton ();
- LoginButton.ID = "LoginLinkButton";
- break;
- case ButtonType.Image:
- LoginButton = new ImageButton ();
- LoginButton.ID = "LoginImageButton";
- break;
- }
- _login.RegisterApplyStyle (LoginButton, _login.LoginButtonStyle);
- LoginButton.ID = "LoginButton";
- ((IButtonControl) LoginButton).Text = _login.LoginButtonText;
- ((IButtonControl) LoginButton).CommandName = Login.LoginButtonCommandName;
- ((IButtonControl) LoginButton).Command += new CommandEventHandler (_login.LoginClick);
- ((IButtonControl) LoginButton).ValidationGroup = _login.ID;
- // Create layout table
- Table table = new Table ();
- table.CellPadding = 0;
- // Title row
- table.Rows.Add (
- CreateRow (
- CreateCell (TitleText, null, _login.TitleTextStyle, HorizontalAlign.Center)));
- // Instruction row
- if (_login.InstructionText.Length > 0) {
- table.Rows.Add (
- CreateRow (
- CreateCell (InstructionText, null, _login.instructionTextStyle, HorizontalAlign.Center)));
- }
- if (_login.Orientation == Orientation.Horizontal) {
- // Main row
- TableRow row1 = new TableRow ();
- TableRow row2 = new TableRow ();
- if (_login.TextLayout == LoginTextLayout.TextOnTop)
- row1.Cells.Add (CreateCell (UserNameLabel, null, _login.LabelStyle));
- else
- row2.Cells.Add (CreateCell (UserNameLabel, null, _login.LabelStyle));
- row2.Cells.Add (CreateCell (UserName, UserNameRequired, null));
- if (_login.TextLayout == LoginTextLayout.TextOnTop)
- row1.Cells.Add (CreateCell (PasswordLabel, null, _login.LabelStyle));
- else
- row2.Cells.Add (CreateCell (PasswordLabel, null, _login.LabelStyle));
- row2.Cells.Add (CreateCell (Password, PasswordRequired, null));
- row2.Cells.Add (CreateCell (RememberMe, null, null));
- row2.Cells.Add (CreateCell (LoginButton, null, null));
- if (row1.Cells.Count > 0)
- table.Rows.Add (row1);
- table.Rows.Add (row2);
- }
- else { // Orientation.Vertical
- if (_login.TextLayout == LoginTextLayout.TextOnLeft)
- table.Rows.Add (CreateRow (UserNameLabel, UserName, UserNameRequired, _login.LabelStyle));
- else {
- table.Rows.Add (CreateRow (UserNameLabel, null, null, _login.LabelStyle));
- table.Rows.Add (CreateRow (null, UserName, UserNameRequired, null));
- }
- if (_login.TextLayout == LoginTextLayout.TextOnLeft)
- table.Rows.Add (CreateRow (PasswordLabel, Password, PasswordRequired, _login.LabelStyle));
- else {
- table.Rows.Add (CreateRow (PasswordLabel, null, null, _login.LabelStyle));
- table.Rows.Add (CreateRow (null, Password, PasswordRequired, null));
- }
- table.Rows.Add (CreateRow (CreateCell (RememberMe, null, null)));
- table.Rows.Add (CreateRow (CreateCell (LoginButton, null, null, HorizontalAlign.Right)));
- }
- // Error message row
- if (_login.FailureTextStyle.ForeColor.IsEmpty)
- _login.FailureTextStyle.ForeColor = System.Drawing.Color.Red;
- table.Rows.Add (
- CreateRow (
- CreateCell (FailureText, null, _login.FailureTextStyle)));
- // Links row
- TableCell linksCell = new TableCell ();
- _login.RegisterApplyStyle (linksCell, _login.HyperLinkStyle);
- if (AddLink (_login.CreateUserUrl, _login.CreateUserText, _login.CreateUserIconUrl, linksCell, _login.HyperLinkStyle))
- if (_login.Orientation == Orientation.Vertical)
- linksCell.Controls.Add (new LiteralControl ("<br/>"));
- else
- linksCell.Controls.Add (new LiteralControl (" "));
- if (AddLink (_login.PasswordRecoveryUrl, _login.PasswordRecoveryText, _login.PasswordRecoveryIconUrl, linksCell, _login.HyperLinkStyle))
- if (_login.Orientation == Orientation.Vertical)
- linksCell.Controls.Add (new LiteralControl ("<br/>"));
- else
- linksCell.Controls.Add (new LiteralControl (" "));
- AddLink (_login.HelpPageUrl, _login.HelpPageText, _login.HelpPageIconUrl, linksCell, _login.HyperLinkStyle);
- table.Rows.Add (CreateRow (linksCell));
- FixTableColumnSpans (table);
- container.Controls.Add (table);
- }
- private TableRow CreateRow (TableCell cell)
- {
- TableRow row = new TableRow ();
- row.Cells.Add (cell);
- return row;
- }
- TableRow CreateRow (Control c0, Control c1, Control c2, Style s)
- {
- TableRow row = new TableRow ();
- TableCell cell0 = new TableCell ();
- TableCell cell1 = new TableCell ();
- if (c0 != null) {
- cell0.Controls.Add (c0);
- row.Controls.Add (cell0);
- }
- if (s != null)
- cell0.ApplyStyle (s);
- if ((c1 != null) && (c2 != null)) {
- cell1.Controls.Add (c1);
- cell1.Controls.Add (c2);
- cell0.HorizontalAlign = HorizontalAlign.Right;
- row.Controls.Add (cell1);
- }
- return row;
- }
-
- private TableCell CreateCell (Control c0, Control c1, Style s, HorizontalAlign align)
- {
- TableCell cell = CreateCell (c0, c1, s);
- cell.HorizontalAlign = align;
- return cell;
- }
- private TableCell CreateCell (Control c0, Control c1, Style s)
- {
- TableCell cell = new TableCell ();
- if (s != null)
- cell.ApplyStyle (s);
- cell.Controls.Add (c0);
- if (c1 != null)
- cell.Controls.Add (c1);
- return cell;
- }
- bool AddLink (string pageUrl, string linkText, string linkIcon, WebControl container, Style style)
- {
- bool added = false;
- if (linkIcon.Length > 0) {
- Image img = new Image ();
- img.ImageUrl = linkIcon;
- container.Controls.Add (img);
- added = true;
- }
- if (linkText.Length > 0) {
- HyperLink link = new HyperLink ();
- link.NavigateUrl = pageUrl;
- link.Text = linkText;
- _login.RegisterApplyStyle (link, style);
- container.Controls.Add (link);
- added = true;
- }
- return added;
- }
- void FixTableColumnSpans (Table table)
- {
- int maxCols = 0;
- for (int row = 0; row < table.Rows.Count; row++) {
- if (maxCols < table.Rows [row].Cells.Count)
- maxCols = table.Rows [row].Cells.Count;
- }
- for (int row = 0; row < table.Rows.Count; row++) {
- if (table.Rows [row].Cells.Count == 1 && maxCols > 1)
- table.Rows [row].Cells [0].ColumnSpan = maxCols;
- }
- }
- }
- #endregion
- public static readonly string LoginButtonCommandName = "Login";
- private static readonly object authenticateEvent = new object ();
- private static readonly object loggedInEvent = new object ();
- private static readonly object loggingInEvent = new object ();
- private static readonly object loginErrorEvent = new object ();
- private TableItemStyle checkBoxStyle;
- private TableItemStyle failureTextStyle;
- private TableItemStyle hyperLinkStyle;
- private TableItemStyle instructionTextStyle;
- private TableItemStyle labelStyle;
- private Style logonButtonStyle;
- private Style textBoxStyle;
- private TableItemStyle titleTextStyle;
- private Style validatorTextStyle;
- private ArrayList styles = new ArrayList ();
- private ITemplate layoutTemplate;
- private LoginContainer container;
- private string _password;
- public Login ()
- {
- }
- [DefaultValue (1)]
- public virtual int BorderPadding {
- get {
- object o = ViewState ["BorderPadding"];
- return (o == null) ? 1 : (int) o;
- }
- set {
- if (value < -1)
- throw new ArgumentOutOfRangeException ("BorderPadding", "< -1");
- else
- ViewState ["BorderPadding"] = value;
- }
- }
- [DefaultValue (null)]
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
- [NotifyParentProperty (true)]
- [PersistenceMode (PersistenceMode.InnerProperty)]
- public TableItemStyle CheckBoxStyle {
- get {
- if (checkBoxStyle == null) {
- checkBoxStyle = new TableItemStyle ();
- if (IsTrackingViewState) {
- (checkBoxStyle as IStateManager).TrackViewState ();
- }
- }
- return checkBoxStyle;
- }
- }
- [DefaultValue ("")]
- [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
- [UrlProperty]
- public virtual string CreateUserIconUrl {
- get {
- object o = ViewState ["CreateUserIconUrl"];
- return (o == null) ? String.Empty : (string) o;
- }
- set {
- if (value == null)
- ViewState.Remove ("CreateUserIconUrl");
- else
- ViewState ["CreateUserIconUrl"] = value;
- }
- }
- [DefaultValue ("")]
- [Localizable (true)]
- public virtual string CreateUserText {
- get {
- object o = ViewState ["CreateUserText"];
- return (o == null) ? String.Empty : (string) o;
- }
- set {
- if (value == null)
- ViewState.Remove ("CreateUserText");
- else
- ViewState ["CreateUserText"] = value;
- }
- }
- [DefaultValue ("")]
- [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
- [UrlProperty]
- public virtual string CreateUserUrl {
- get {
- object o = ViewState ["CreateUserUrl"];
- return (o == null) ? String.Empty : (string) o;
- }
- set {
- if (value == null)
- ViewState.Remove ("CreateUserUrl");
- else
- ViewState ["CreateUserUrl"] = value;
- }
- }
- [DefaultValue ("")]
- [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
- [Themeable (false)]
- [UrlProperty]
- public virtual string DestinationPageUrl {
- get {
- object o = ViewState ["DestinationPageUrl"];
- return (o == null) ? String.Empty : (string) o;
- }
- set {
- if (value == null)
- ViewState.Remove ("DestinationPageUrl");
- else
- ViewState ["DestinationPageUrl"] = value;
- }
- }
- [DefaultValue (true)]
- [Themeable (false)]
- public virtual bool DisplayRememberMe {
- get {
- object o = ViewState ["DisplayRememberMe"];
- return (o == null) ? true : (bool) o;
- }
- set {
- ViewState ["DisplayRememberMe"] = value;
- }
- }
- [DefaultValue (LoginFailureAction.Refresh)]
- [Themeable (false)]
- [MonoTODO ("RedirectToLoginPage not yet implemented in FormsAuthentication")]
- public virtual LoginFailureAction FailureAction {
- get {
- object o = ViewState ["FailureAction"];
- return (o == null) ? LoginFailureAction.Refresh : (LoginFailureAction) o;
- }
- set {
- if ((value < LoginFailureAction.Refresh) || (value > LoginFailureAction.RedirectToLoginPage))
- throw new ArgumentOutOfRangeException ("FailureAction");
- ViewState ["FailureAction"] = (int) value;
- }
- }
- [Localizable (true)]
- public virtual string FailureText {
- get {
- object o = ViewState ["FailureText"];
- return (o == null) ? Locale.GetText ("Your login attempt was not successful. Please try again.") : (string) o;
- }
- set {
- if (value == null)
- ViewState.Remove ("FailureText");
- else
- ViewState ["FailureText"] = value;
- }
- }
- [DefaultValue (null)]
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
- [NotifyParentProperty (true)]
- [PersistenceMode (PersistenceMode.InnerProperty)]
- public TableItemStyle FailureTextStyle {
- get {
- if (failureTextStyle == null) {
- failureTextStyle = new TableItemStyle ();
- if (IsTrackingViewState) {
- (failureTextStyle as IStateManager).TrackViewState ();
- }
- }
- return failureTextStyle;
- }
- }
- [DefaultValue ("")]
- [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
- [UrlProperty]
- public virtual string HelpPageIconUrl {
- get {
- object o = ViewState ["HelpPageIconUrl"];
- return (o == null) ? String.Empty : (string) o;
- }
- set {
- if (value == null)
- ViewState.Remove ("HelpPageIconUrl");
- else
- ViewState ["HelpPageIconUrl"] = value;
- }
- }
- [DefaultValue ("")]
- [Localizable (true)]
- public virtual string HelpPageText {
- get {
- object o = ViewState ["HelpPageText"];
- return (o == null) ? String.Empty : (string) o;
- }
- set {
- if (value == null)
- ViewState.Remove ("HelpPageText");
- else
- ViewState ["HelpPageText"] = value;
- }
- }
- [DefaultValue ("")]
- [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
- [UrlProperty]
- public virtual string HelpPageUrl {
- get {
- object o = ViewState ["HelpPageUrl"];
- return (o == null) ? String.Empty : (string) o;
- }
- set {
- if (value == null)
- ViewState.Remove ("HelpPageUrl");
- else
- ViewState ["HelpPageUrl"] = value;
- }
- }
- [DefaultValue (null)]
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
- [NotifyParentProperty (true)]
- [PersistenceMode (PersistenceMode.InnerProperty)]
- public TableItemStyle HyperLinkStyle {
- get {
- if (hyperLinkStyle == null) {
- hyperLinkStyle = new TableItemStyle ();
- if (IsTrackingViewState) {
- (hyperLinkStyle as IStateManager).TrackViewState ();
- }
- }
- return hyperLinkStyle;
- }
- }
- [DefaultValue ("")]
- [Localizable (true)]
- public virtual string InstructionText {
- get {
- object o = ViewState ["InstructionText"];
- return (o == null) ? String.Empty : (string) o;
- }
- set {
- if (value == null)
- ViewState.Remove ("InstructionText");
- else
- ViewState ["InstructionText"] = value;
- }
- }
- [DefaultValue (null)]
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
- [NotifyParentProperty (true)]
- [PersistenceMode (PersistenceMode.InnerProperty)]
- public TableItemStyle InstructionTextStyle {
- get {
- if (instructionTextStyle == null) {
- instructionTextStyle = new TableItemStyle ();
- if (IsTrackingViewState) {
- (instructionTextStyle as IStateManager).TrackViewState ();
- }
- }
- return instructionTextStyle;
- }
- }
- [DefaultValue (null)]
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
- [NotifyParentProperty (true)]
- [PersistenceMode (PersistenceMode.InnerProperty)]
- public TableItemStyle LabelStyle {
- get {
- if (labelStyle == null) {
- labelStyle = new TableItemStyle ();
- if (IsTrackingViewState) {
- (labelStyle as IStateManager).TrackViewState ();
- }
- }
- return labelStyle;
- }
- }
- [Browsable (false)]
- [TemplateContainer (typeof (Login))]
- [PersistenceMode (PersistenceMode.InnerProperty)]
- public virtual ITemplate LayoutTemplate {
- get { return layoutTemplate; }
- set { layoutTemplate = value; }
- }
- [DefaultValue ("")]
- [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
- [UrlProperty]
- public virtual string LoginButtonImageUrl {
- get {
- object o = ViewState ["LoginButtonImageUrl"];
- return (o == null) ? String.Empty : (string) o;
- }
- set {
- if (value == null)
- ViewState.Remove ("LoginButtonImageUrl");
- else
- ViewState ["LoginButtonImageUrl"] = value;
- }
- }
- [DefaultValue (null)]
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
- [NotifyParentProperty (true)]
- [PersistenceMode (PersistenceMode.InnerProperty)]
- public Style LoginButtonStyle {
- get {
- if (logonButtonStyle == null) {
- logonButtonStyle = new Style ();
- if (IsTrackingViewState) {
- (logonButtonStyle as IStateManager).TrackViewState ();
- }
- }
- return logonButtonStyle;
- }
- }
- [Localizable (true)]
- public virtual string LoginButtonText {
- get {
- object o = ViewState ["LoginButtonText"];
- return (o == null) ? Locale.GetText ("Log In") : (string) o;
- }
- set {
- if (value == null)
- ViewState.Remove ("LoginButtonText");
- else
- ViewState ["LoginButtonText"] = value;
- }
- }
- [DefaultValue (ButtonType.Button)]
- public virtual ButtonType LoginButtonType {
- get {
- object o = ViewState ["LoginButtonType"];
- return (o == null) ? ButtonType.Button : (ButtonType) o;
- }
- set {
- if ((value < ButtonType.Button) || (value > ButtonType.Link))
- throw new ArgumentOutOfRangeException ("LoginButtonType");
- ViewState ["LoginButtonType"] = (int) value;
- }
- }
- [DefaultValue ("")]
- [Themeable (false)]
- public virtual string MembershipProvider {
- get {
- object o = ViewState ["MembershipProvider"];
- return (o == null) ? String.Empty : (string) o;
- }
- set {
- if (value == null)
- ViewState.Remove ("MembershipProvider");
- else
- ViewState ["MembershipProvider"] = value;
- }
- }
- [DefaultValue (Orientation.Vertical)]
- public virtual Orientation Orientation {
- get {
- object o = ViewState ["Orientation"];
- return (o == null) ? Orientation.Vertical : (Orientation) o;
- }
- set {
- if ((value < Orientation.Horizontal) || (value > Orientation.Vertical))
- throw new ArgumentOutOfRangeException ("Orientation");
- ViewState ["Orientation"] = (int) value;
- }
- }
- [Browsable (false)]
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
- public virtual string Password {
- get {
- return _password != null ? _password : String.Empty;
- }
- }
- [Localizable (true)]
- public virtual string PasswordLabelText {
- get {
- object o = ViewState ["PasswordLabelText"];
- return (o == null) ? "Password:" : (string) o;
- }
- set {
- if (value == null)
- ViewState.Remove ("PasswordLabelText");
- else
- ViewState ["PasswordLabelText"] = value;
- }
- }
- [DefaultValue ("")]
- [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
- [UrlProperty]
- public virtual string PasswordRecoveryIconUrl {
- get {
- object o = ViewState ["PasswordRecoveryIconUrl"];
- return (o == null) ? String.Empty : (string) o;
- }
- set {
- if (value == null)
- ViewState.Remove ("PasswordRecoveryIconUrl");
- else
- ViewState ["PasswordRecoveryIconUrl"] = value;
- }
- }
- [DefaultValue ("")]
- [Localizable (true)]
- public virtual string PasswordRecoveryText {
- get {
- object o = ViewState ["PasswordRecoveryText"];
- return (o == null) ? String.Empty : (string) o;
- }
- set {
- if (value == null)
- ViewState.Remove ("PasswordRecoveryText");
- else
- ViewState ["PasswordRecoveryText"] = value;
- }
- }
- [DefaultValue ("")]
- [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
- [UrlProperty]
- public virtual string PasswordRecoveryUrl {
- get {
- object o = ViewState ["PasswordRecoveryUrl"];
- return (o == null) ? String.Empty : (string) o;
- }
- set {
- if (value == null)
- ViewState.Remove ("PasswordRecoveryUrl");
- else
- ViewState ["PasswordRecoveryUrl"] = value;
- }
- }
- [Localizable (true)]
- public virtual string PasswordRequiredErrorMessage {
- get {
- object o = ViewState ["PasswordRequiredErrorMessage"];
- return (o == null) ? Locale.GetText ("Password is required.") : (string) o;
- }
- set {
- if (value == null)
- ViewState.Remove ("PasswordRequiredErrorMessage");
- else
- ViewState ["PasswordRequiredErrorMessage"] = value;
- }
- }
- [DefaultValue (false)]
- [Themeable (false)]
- public virtual bool RememberMeSet {
- get {
- object o = ViewState ["RememberMeSet"];
- return (o == null) ? false : (bool) o;
- }
- set {
- ViewState ["RememberMeSet"] = value;
- }
- }
- [Localizable (true)]
- public virtual string RememberMeText {
- get {
- object o = ViewState ["RememberMeText"];
- return (o == null) ? Locale.GetText ("Remember me next time.") : (string) o;
- }
- set {
- if (value == null)
- ViewState.Remove ("RememberMeText");
- else
- ViewState ["RememberMeText"] = value;
- }
- }
- protected override HtmlTextWriterTag TagKey {
- get { return HtmlTextWriterTag.Table; }
- }
- [DefaultValue (null)]
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
- [NotifyParentProperty (true)]
- [PersistenceMode (PersistenceMode.InnerProperty)]
- public Style TextBoxStyle {
- get {
- if (textBoxStyle == null) {
- textBoxStyle = new Style ();
- if (IsTrackingViewState) {
- (textBoxStyle as IStateManager).TrackViewState ();
- }
- }
- return textBoxStyle;
- }
- }
- [DefaultValue (LoginTextLayout.TextOnLeft)]
- public virtual LoginTextLayout TextLayout {
- get {
- object o = ViewState ["TextLayout"];
- return (o == null) ? LoginTextLayout.TextOnLeft : (LoginTextLayout) o;
- }
- set {
- if ((value < LoginTextLayout.TextOnLeft) || (value > LoginTextLayout.TextOnTop))
- throw new ArgumentOutOfRangeException ("TextLayout");
- ViewState ["TextLayout"] = (int) value;
- }
- }
- [Localizable (true)]
- public virtual string TitleText {
- get {
- object o = ViewState ["TitleText"];
- return (o == null) ? Locale.GetText ("Log In") : (string) o;
- }
- set {
- if (value == null)
- ViewState.Remove ("TitleText");
- else
- ViewState ["TitleText"] = value;
- }
- }
- [DefaultValue (null)]
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
- [NotifyParentProperty (true)]
- [PersistenceMode (PersistenceMode.InnerProperty)]
- public TableItemStyle TitleTextStyle {
- get {
- if (titleTextStyle == null) {
- titleTextStyle = new TableItemStyle ();
- if (IsTrackingViewState) {
- (titleTextStyle as IStateManager).TrackViewState ();
- }
- }
- return titleTextStyle;
- }
- }
- [DefaultValue ("")]
- public virtual string UserName {
- get {
- object o = ViewState ["UserName"];
- return (o == null) ? String.Empty : (string) o;
- }
- set {
- if (value == null)
- ViewState.Remove ("UserName");
- else
- ViewState ["UserName"] = value;
- }
- }
- [Localizable (true)]
- public virtual string UserNameLabelText {
- get {
- object o = ViewState ["UserNameLabelText"];
- return (o == null) ? Locale.GetText ("User Name:") : (string) o;
- }
- set {
- if (value == null)
- ViewState.Remove ("UserNameLabelText");
- else
- ViewState ["UserNameLabelText"] = value;
- }
- }
- [Localizable (true)]
- public virtual string UserNameRequiredErrorMessage {
- get {
- object o = ViewState ["UserNameRequiredErrorMessage"];
- return (o == null) ? Locale.GetText ("User Name is required.") : (string) o;
- }
- set {
- if (value == null)
- ViewState.Remove ("UserNameRequiredErrorMessage");
- else
- ViewState ["UserNameRequiredErrorMessage"] = value;
- }
- }
- [DefaultValue (null)]
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
- [NotifyParentProperty (true)]
- [PersistenceMode (PersistenceMode.InnerProperty)]
- public Style ValidatorTextStyle {
- get {
- if (validatorTextStyle == null) {
- validatorTextStyle = new Style ();
- if (IsTrackingViewState) {
- (validatorTextStyle as IStateManager).TrackViewState ();
- }
- }
- return validatorTextStyle;
- }
- }
- [DefaultValue (true)]
- [Themeable (false)]
- public virtual bool VisibleWhenLoggedIn {
- get {
- object o = ViewState ["VisibleWhenLoggedIn"];
- return (o == null) ? true : (bool) o;
- }
- set {
- ViewState ["VisibleWhenLoggedIn"] = value;
- }
- }
- LoginContainer LoginTemplateContainer
- {
- get {
- if (container == null)
- container = new LoginContainer (this);
- return container;
- }
- }
- // methods
- protected internal override void CreateChildControls ()
- {
- Controls.Clear ();
- ITemplate template = LayoutTemplate;
- if (template == null)
- template = new LoginTemplate (this);
- LoginTemplateContainer.InstantiateTemplate (template);
- Controls.Add (container);
- IEditableTextControl editable;
- editable = container.UserNameTextBox as IEditableTextControl;
- if (editable != null) {
- editable.Text = UserName;
- editable.TextChanged += new EventHandler (UserName_TextChanged);
- }
- else
- throw new HttpException ("LayoutTemplate does not contain an IEditableTextControl with ID UserName for the username.");
- editable = container.PasswordTextBox as IEditableTextControl;
- if (editable != null)
- editable.TextChanged += new EventHandler (Password_TextChanged);
- else
- throw new HttpException ("LayoutTemplate does not contain an IEditableTextControl with ID Password for the password.");
- ICheckBoxControl checkBox = container.RememberMeCheckBox as ICheckBoxControl;
- if (checkBox != null)
- checkBox.CheckedChanged += new EventHandler (RememberMe_CheckedChanged);
- }
- protected override void LoadViewState (object savedState)
- {
- if (savedState == null) {
- base.LoadViewState (null);
- return;
- }
- object[] state = (object[]) savedState;
- base.LoadViewState (state [0]);
- if (state [1] != null)
- (LoginButtonStyle as IStateManager).LoadViewState (state [1]);
- if (state [2] != null)
- (LabelStyle as IStateManager).LoadViewState (state [2]);
- if (state [3] != null)
- (TextBoxStyle as IStateManager).LoadViewState (state [3]);
- if (state [4] != null)
- (HyperLinkStyle as IStateManager).LoadViewState (state [4]);
- if (state [5] != null)
- (InstructionTextStyle as IStateManager).LoadViewState (state [5]);
- if (state [6] != null)
- (TitleTextStyle as IStateManager).LoadViewState (state [6]);
- if (state [7] != null)
- (CheckBoxStyle as IStateManager).LoadViewState (state [7]);
- if (state [8] != null)
- (FailureTextStyle as IStateManager).LoadViewState (state [8]);
- if (state [9] != null)
- (ValidatorTextStyle as IStateManager).LoadViewState (state [9]);
- }
- private bool HasOnAuthenticateHandler ()
- {
- return Events [authenticateEvent] != null;
- }
- protected virtual void OnAuthenticate (AuthenticateEventArgs e)
- {
- // this gets called after OnLoggingIn and the authentication so we can change the result
- AuthenticateEventHandler authenticate = (AuthenticateEventHandler) Events [authenticateEvent];
- if (authenticate != null)
- authenticate (this, e);
- }
- protected override bool OnBubbleEvent (object source, EventArgs e)
- {
- // check for submit button
- CommandEventArgs cea = (e as CommandEventArgs);
- if ((cea != null) &&
- String.Equals (cea.CommandName, LoginButtonCommandName, StringComparison.InvariantCultureIgnoreCase)) {
- if (!AuthenticateUser ()) {
- ITextControl failureText = LoginTemplateContainer.FailureTextLiteral;
- if (failureText != null)
- failureText.Text = FailureText;
- }
- return true;
- }
- return false;
- }
- protected virtual void OnLoggedIn (EventArgs e)
- {
- // this gets called only if the authentication was successful
- EventHandler loggedIn = (EventHandler) Events [loggedInEvent];
- if (loggedIn != null)
- loggedIn (this, e);
- }
- protected virtual void OnLoggingIn (LoginCancelEventArgs e)
- {
- // this gets called before OnAuthenticate so we can abort the authentication process
- LoginCancelEventHandler loggingIn = (LoginCancelEventHandler) Events [loggingInEvent];
- if (loggingIn != null)
- loggingIn (this, e);
- }
- protected virtual void OnLoginError (EventArgs e)
- {
- // this gets called only if the authentication wasn't successful
- EventHandler loginError = (EventHandler) Events [loginErrorEvent];
- if (loginError != null)
- loginError (this, e);
- }
- [MonoTODO ("overriden for ?")]
- protected internal override void OnPreRender (EventArgs e)
- {
- base.OnPreRender (e);
- // note: doc says that UserName and Password aren't available at
- // PageLoad but are during PreRender phase, so... ???
- }
- protected internal override void Render (HtmlTextWriter writer)
- {
- // VisibleWhenLoggedIn isn't applicable to the default login page
- if (!VisibleWhenLoggedIn && !IsDefaultLoginPage () && IsLoggedIn ())
- return;
- if (Page != null) {
- Page.VerifyRenderingInServerForm (this);
- }
- EnsureChildControls ();
- foreach (object [] styleDef in styles)
- ((WebControl) styleDef [0]).ApplyStyle ((Style) styleDef [1]);
- RenderContents(writer);
- }
- protected override object SaveViewState ()
- {
- object[] state = new object [10];
- state [0] = base.SaveViewState ();
- if (logonButtonStyle != null)
- state [1] = (logonButtonStyle as IStateManager).SaveViewState ();
- if (labelStyle != null)
- state [2] = (labelStyle as IStateManager).SaveViewState ();
- if (textBoxStyle != null)
- state [3] = (textBoxStyle as IStateManager).SaveViewState ();
- if (hyperLinkStyle != null)
- state [4] = (hyperLinkStyle as IStateManager).SaveViewState ();
- if (instructionTextStyle != null)
- state [5] = (instructionTextStyle as IStateManager).SaveViewState ();
- if (titleTextStyle != null)
- state [6] = (titleTextStyle as IStateManager).SaveViewState ();
- if (checkBoxStyle != null)
- state [7] = (checkBoxStyle as IStateManager).SaveViewState ();
- if (failureTextStyle != null)
- state [8] = (failureTextStyle as IStateManager).SaveViewState ();
- if (validatorTextStyle != null)
- state [9] = (validatorTextStyle as IStateManager).SaveViewState ();
- for (int i=0; i < state.Length; i++) {
- if (state [0] != null)
- return (object) state;
- }
- return null; // reduce view state
- }
- [MonoTODO ("for design-time usage - no more details available")]
- protected override void SetDesignModeState (IDictionary data)
- {
- base.SetDesignModeState (data);
- }
- protected override void TrackViewState ()
- {
- base.TrackViewState ();
- if (logonButtonStyle != null)
- (logonButtonStyle as IStateManager).TrackViewState ();
- if (labelStyle != null)
- (labelStyle as IStateManager).TrackViewState ();
- if (textBoxStyle != null)
- (textBoxStyle as IStateManager).TrackViewState ();
- if (hyperLinkStyle != null)
- (hyperLinkStyle as IStateManager).TrackViewState ();
- if (instructionTextStyle != null)
- (instructionTextStyle as IStateManager).TrackViewState ();
- if (titleTextStyle != null)
- (titleTextStyle as IStateManager).TrackViewState ();
- if (checkBoxStyle != null)
- (checkBoxStyle as IStateManager).TrackViewState ();
- if (failureTextStyle != null)
- (failureTextStyle as IStateManager).TrackViewState ();
- if (validatorTextStyle != null)
- (validatorTextStyle as IStateManager).TrackViewState ();
- }
- // events
- public event AuthenticateEventHandler Authenticate {
- add { Events.AddHandler (authenticateEvent, value); }
- remove { Events.RemoveHandler (authenticateEvent, value); }
- }
- public event EventHandler LoggedIn {
- add { Events.AddHandler (loggedInEvent, value); }
- remove { Events.RemoveHandler (loggedInEvent, value); }
- }
- public event LoginCancelEventHandler LoggingIn {
- add { Events.AddHandler (loggingInEvent, value); }
- remove { Events.RemoveHandler (loggingInEvent, value); }
- }
- public event EventHandler LoginError {
- add { Events.AddHandler (loginErrorEvent, value); }
- remove { Events.RemoveHandler (loginErrorEvent, value); }
- }
- // private stuff
- internal void RegisterApplyStyle (WebControl control, Style style)
- {
- styles.Add (new object [] { control, style });
- }
-
- private bool AuthenticateUser ()
- {
- if (!Page.IsValid)
- return true;
- LoginCancelEventArgs lcea = new LoginCancelEventArgs ();
- OnLoggingIn (lcea);
- if (lcea.Cancel)
- return true;
- AuthenticateEventArgs aea = new AuthenticateEventArgs ();
-
- if (!HasOnAuthenticateHandler ()) {
- string mp = MembershipProvider;
- MembershipProvider provider = (mp.Length == 0) ?
- provider = Membership.Provider : Membership.Providers [mp];
- if (provider == null) {
- throw new HttpException (Locale.GetText ("No provider named '{0}' could be found.", mp));
- }
- aea.Authenticated = provider.ValidateUser (UserName, Password);
- }
- OnAuthenticate (aea);
- if (aea.Authenticated) {
- FormsAuthentication.SetAuthCookie (UserName, RememberMeSet);
- OnLoggedIn (EventArgs.Empty);
- string url = DestinationPageUrl;
- if (Page.Request.Path.StartsWith (FormsAuthentication.LoginUrl, StringComparison.InvariantCultureIgnoreCase)) {
- if (!String.IsNullOrEmpty (FormsAuthentication.ReturnUrl))
- Redirect (FormsAuthentication.ReturnUrl);
- else if (!String.IsNullOrEmpty (DestinationPageUrl))
- Redirect (url);
- else if (!String.IsNullOrEmpty (FormsAuthentication.DefaultUrl))
- Redirect (FormsAuthentication.DefaultUrl);
- else if (url.Length == 0)
- Refresh ();
- }
- else if (!String.IsNullOrEmpty (DestinationPageUrl)) {
- Redirect (url);
- }
- else {
- Refresh ();
- }
- return true;
- }
- else {
- OnLoginError (EventArgs.Empty);
- if (FailureAction == LoginFailureAction.RedirectToLoginPage) {
- // login page is defined in web.config
- FormsAuthentication.RedirectToLoginPage ();
- }
- return false;
- }
- }
- // TODO: its called from default template only, not usefully, OnBubbleEvent
- // do handle command, need be removed
- [MonoTODO()]
- private void LoginClick (object sender, CommandEventArgs e)
- {
- RaiseBubbleEvent (sender, (EventArgs)e);
- }
- private bool IsDefaultLoginPage ()
- {
- if ((Page == null) || (Page.Request == null))
- return false;
- string defaultLogin = FormsAuthentication.LoginUrl;
- if (defaultLogin == null)
- return false;
- string url = Page.Request.Url.AbsolutePath;
- return (String.Compare (defaultLogin, 0, url, url.Length - defaultLogin.Length, defaultLogin.Length,
- true, CultureInfo.InvariantCulture) == 0);
- }
- private bool IsLoggedIn ()
- {
- if ((Page == null) || (Page.Request == null))
- return false;
- return Page.Request.IsAuthenticated;
- }
- private void Redirect (string url)
- {
- if ((Page != null) && (Page.Response != null))
- Page.Response.Redirect (url);
- }
-
- private void Refresh () {
- if ((Page != null) && (Page.Response != null))
- Page.Response.Redirect (Page.Request.RawUrl);
- }
- private void UserName_TextChanged (object sender, EventArgs e)
- {
- UserName = ((ITextControl)sender).Text;
- }
- private void Password_TextChanged (object sender, EventArgs e)
- {
- _password = ((ITextControl)sender).Text;
- }
- private void RememberMe_CheckedChanged (object sender, EventArgs e)
- {
- RememberMeSet = ((ICheckBoxControl)sender).Checked;
- }
- }
- }
- #endif
|