public string CreateUser(string username, string email, Site currentSite) { User user = new User(); user.UserName = username; user.Email = email; user.IsActive = true; string newPassword = user.GeneratePassword(); // Add the default role from the current site. user.Roles.Add(currentSite.DefaultRole); this._commonDao.SaveOrUpdateObject(user); return newPassword; }
private void btnRegister_Click(object sender, System.EventArgs e) { if (this.Page.IsValid) { // Check if username already exists. if (_userService.FindUsersByUsername(this.txtUsername.Text).Count > 0) { this.lblError.Text = String.Format(GetTextFromFile("USEREXISTS"), this.txtUsername.Text); this.lblError.Visible = true; } else { Site site = this._page.ActiveNode.Site; // OK, create new user. User user = new User(); user.UserName = txtUsername.Text; user.Email = txtEmail.Text; user.IsActive = true; string newPassword = user.GeneratePassword(); // Add the default role from the current site. user.Roles.Add(site.DefaultRole); _userService.CreateUser(user); // Send email string subject = GetTextFromFile("REGISTEREMAILSUBJECT").Replace("{site}", site.Name); string body = GetTextFromFile("REGISTEREMAILBODY"); body = body.Replace("{site}", site.Name + " (" + site.SiteUrl + ")"); body = body.Replace("{username}", user.UserName); body = body.Replace("{password}", newPassword); try { Util.Email.Send(user.Email, site.WebmasterEmail, subject, body); this.pnlConfirmation.Visible = true; this.lblConfirmation.Text = String.Format(GetTextFromFile("REGISTERCONFIRMATION"), user.Email); } catch { // delete user when sending email fails. _userService.DeleteUser(user); this.lblError.Text = GetTextFromFile("REGISTEREMAILERROR"); this.lblError.Visible = true; } this.pnlRegister.Visible = false; } } }