Login.cs 39 KB

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