Login.cs 39 KB

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