PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/BlogEngine/BlogEngine.NET/Account/register.aspx.cs

#
C# | 112 lines | 77 code | 15 blank | 20 comment | 17 complexity | d477edfa278bfbee047932b9d56038f8 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. namespace Account
  2. {
  3. using System;
  4. using System.Web.Security;
  5. using System.Web.UI.WebControls;
  6. using System.Linq;
  7. using BlogEngine.Core;
  8. using Resources;
  9. using Page = System.Web.UI.Page;
  10. using System.Web.UI.HtmlControls;
  11. /// <summary>
  12. /// The account register.
  13. /// </summary>
  14. public partial class Register : Page
  15. {
  16. #region Methods
  17. /// <summary>
  18. /// Handles the Load event of the Page control.
  19. /// </summary>
  20. /// <param name="sender">The source of the event.</param>
  21. /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
  22. protected void Page_Load(object sender, EventArgs e)
  23. {
  24. if (BlogSettings.Instance.CreateBlogOnSelfRegistration && Blog.CurrentInstance.IsPrimary)
  25. {
  26. Response.Redirect("create-blog.aspx");
  27. }
  28. HtmlAnchor HeadLoginStatus = RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("HeadLoginStatus") as HtmlAnchor;
  29. if (HeadLoginStatus != null)
  30. {
  31. HeadLoginStatus.HRef = Utils.RelativeWebRoot + "Account/login.aspx";
  32. }
  33. this.RegisterUser.ContinueDestinationPageUrl = this.Request.QueryString["ReturnUrl"];
  34. this.hdnPassLength.Value = Membership.MinRequiredPasswordLength.ToString();
  35. // if self registration not allowed and user is trying to directly
  36. // navigate to register page, redirect to login
  37. if (!BlogSettings.Instance.EnableSelfRegistration)
  38. {
  39. Response.Redirect(Utils.RelativeWebRoot + "Account/login.aspx");
  40. }
  41. }
  42. /// <summary>
  43. /// Handles the CreatedUser event of the RegisterUser control.
  44. /// </summary>
  45. /// <param name="sender">The source of the event.</param>
  46. /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
  47. protected void RegisterUser_CreatedUser(object sender, EventArgs e)
  48. {
  49. if (!string.IsNullOrEmpty(BlogSettings.Instance.SelfRegistrationInitialRole))
  50. {
  51. string role = Roles.GetAllRoles().FirstOrDefault(r => r.Equals(BlogSettings.Instance.SelfRegistrationInitialRole, StringComparison.OrdinalIgnoreCase));
  52. if (!string.IsNullOrEmpty(role))
  53. {
  54. Roles.AddUsersToRoles(new string[] { this.RegisterUser.UserName }, new string[] { role });
  55. }
  56. }
  57. Security.AuthenticateUser(this.RegisterUser.UserName, this.RegisterUser.Password, false);
  58. FormsAuthentication.SetAuthCookie(this.RegisterUser.UserName, false /* createPersistentCookie */);
  59. var continueUrl = this.RegisterUser.ContinueDestinationPageUrl;
  60. if (String.IsNullOrEmpty(continueUrl))
  61. {
  62. continueUrl = Utils.RelativeWebRoot;
  63. }
  64. this.Response.Redirect(continueUrl);
  65. }
  66. /// <summary>
  67. /// Handles the CreatingUser event of the RegisterUser control.
  68. /// </summary>
  69. /// <param name="sender">The source of the event.</param>
  70. /// <param name="e">The <see cref="System.Web.UI.WebControls.LoginCancelEventArgs"/> instance containing the event data.</param>
  71. protected void RegisterUser_CreatingUser(object sender, LoginCancelEventArgs e)
  72. {
  73. App_Code.Controls.RecaptchaControl captcha =
  74. (App_Code.Controls.RecaptchaControl)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("recaptcha") as App_Code.Controls.RecaptchaControl;
  75. if (Membership.GetUser(this.RegisterUser.UserName) != null)
  76. {
  77. e.Cancel = true;
  78. this.Master.SetStatus("warning", Resources.labels.anotherUserName);
  79. }
  80. else if (Membership.GetUserNameByEmail(this.RegisterUser.Email) != null)
  81. {
  82. e.Cancel = true;
  83. this.Master.SetStatus("warning", Resources.labels.anotherEmail);
  84. }
  85. else if (captcha != null)
  86. {
  87. captcha.Validate();
  88. if (!captcha.IsValid)
  89. {
  90. e.Cancel = true;
  91. this.Master.SetStatus("warning", "Captcha invalid.");
  92. }
  93. }
  94. }
  95. #endregion
  96. }
  97. }