Login.cs 40 KB

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