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