public ActionResult Register(RegisterModel model) { string recaptchaprivatekey = BgResources.Recaptcha_PrivateKeyHttp; try { if (!ReCaptcha.Validate(privateKey: recaptchaprivatekey)) { ModelState.AddModelError("recaptcha", Resources.AppMessages.Error_Recaptcha); } } catch (Exception) { ModelState.AddModelError("recaptcha", Resources.AppMessages.Error_Recaptcha_Key); } if (ModelState.IsValid) { string token = null; try { token = CodeFirstSecurity.CreateAccount(model.UserName, model.Password, model.Email, model.FirstName, model.LastName, model.TimeZone, model.Culture, requireConfirmationToken: true); SmtpClient client = new SmtpClient { Host = BgResources.Email_Server, Port = Int32.Parse(BgResources.Email_SmtpPort), EnableSsl = BgResources.Email_SSL, Credentials = new NetworkCredential(BgResources.Email_UserName, BgResources.Email_Password) }; UserMailer.Register(token, model.Email, AccountServices.FindUser(usr => usr.Username == model.UserName)).Send(new SmtpClientWrapper { InnerSmtpClient = client }); ViewBag.Email = model.Email; return(View("CompleteRegister")); } catch (MembershipCreateUserException ex) { if ((ex.StatusCode == MembershipCreateStatus.DuplicateUserName) || (ex.StatusCode == MembershipCreateStatus.InvalidUserName)) { ModelState.AddModelError("UserName", ErrorCodeToString(ex.StatusCode)); } else if ((ex.StatusCode == MembershipCreateStatus.DuplicateEmail) || (ex.StatusCode == MembershipCreateStatus.InvalidEmail)) { ModelState.AddModelError("Email", ErrorCodeToString(ex.StatusCode)); } else if (ex.StatusCode == MembershipCreateStatus.InvalidPassword) { ModelState.AddModelError("Password", ErrorCodeToString(ex.StatusCode)); } else { ModelState.AddModelError("", ErrorCodeToString(ex.StatusCode)); } } catch (Exception ex) { throw new Exception(Resources.AppMessages.Error_SendMail); } } return(View(model)); }
private void CheckForAdminUser() { var roles = CodeFirstRoleServices.GetUsersInRole(BgResources.Security_AdminRole); if (roles.Length == 0) { CodeFirstSecurity.CreateAccount(BgResources.Security_AdminRole, "admin", BgResources.Email_UserName, false); CodeFirstRoleServices.AddUsersToRoles(new string[] { "admin" }, new string[] { BgResources.Security_AdminRole, BgResources.Security_PremiumRole }); } }
public ActionResult Register(RegisterModel model) { if (ModelState.IsValid) { // Attempt to register the user CodeFirstSecurity.CreateAccount(model.UserName, model.Password, model.Email); FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */); return(RedirectToAction("Index", "Home")); } // If we got this far, something failed, redisplay form return(View(model)); }
/// <summary> /// Create new User in Blog /// </summary> /// <param name="username">Name of the user</param> /// <param name="password">Password</param> /// <param name="email">Email</param> /// <param name="firstname">The FirstName</param> /// <param name="lastname">The Lastname</param> /// <param name="timezone">The Time Zone for the user</param> /// <param name="culture">Culture</param> /// <param name="requireconfirmation">Always true because the acccount is confirmed inmediately</param> /// <param name="selectedroles">Bunch of roles for the user being created</param> public void CreateAccount(string username, string password, string email, string firstname, string lastname, string timezone, string culture, bool requireconfirmation, string[] selectedroles) { var token = CodeFirstSecurity.CreateAccount(username, password, email, firstname, lastname, timezone, culture, requireconfirmation); if (selectedroles.Length == 0) { CodeFirstRoleServices.RemoveUsersFromRoles(new string[1] { username }, CodeFirstRoleServices.GetAllRoles()); } else { CodeFirstRoleServices.RemoveUsersFromRoles(new string[1] { username }, CodeFirstRoleServices.GetAllRoles()); CodeFirstRoleServices.AddUsersToRoles(new string[1] { username }, selectedroles); } }
protected override void Seed(PalaverDb context) { CodeFirstSecurity.CreateAccount("Demo", "Demo", "*****@*****.**"); User u = context.Users.FirstOrDefault(); Comment c = new Comment { User = u, Text = "testing", CreatedTime = DateTime.UtcNow, LastUpdatedTime = DateTime.UtcNow }; context.Comments.Add(c); context.SaveChanges(); c.SubjectId = c.CommentId; context.Comments.Add(new Comment { ParentCommentId = c.CommentId, User = u, Text = "testing 1", CreatedTime = DateTime.UtcNow, LastUpdatedTime = DateTime.UtcNow, SubjectId = c.SubjectId }); context.Comments.Add(new Comment { ParentCommentId = c.CommentId, User = u, Text = "testing 2", CreatedTime = DateTime.UtcNow, LastUpdatedTime = DateTime.UtcNow, SubjectId = c.SubjectId }); context.Comments.Add(new Comment { ParentCommentId = c.CommentId, User = u, Text = "testing 3", CreatedTime = DateTime.UtcNow, LastUpdatedTime = DateTime.UtcNow, SubjectId = c.SubjectId }); context.SaveChanges(); foreach (Comment cc in context.Comments) { foreach (User uu in context.Users) { if (uu.UserId != u.UserId) { context.UnreadItems.Add(new UnreadItem { Comment = cc, User = uu }); } } } context.SaveChanges(); }
public ActionResult Register(RegisterModel model) { if (ModelState.IsValid) { // Attempt to register the user MembershipCreateStatus createStatus; try { CodeFirstSecurity.CreateAccount(model.UserName, model.Password, model.Email, false); FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */); return(RedirectToAction("Index", "Home")); } catch (MembershipCreateUserException ex) { ModelState.AddModelError("", ErrorCodeToString(ex.StatusCode)); } } // If we got this far, something failed, redisplay form return(View(model)); }