public ActionResult Register(WorkNC_UserPermission user) { try { if (ModelState.IsValid) { db.Entry(user).State = EntityState.Added; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(user)); } catch (Exception) { return(View()); } }
public async Task <ActionResult> Register(RegisterAccount model) { if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model.Username, Email = model.Email }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { try { //add WorkNC_UserPermission WorkNC_UserPermission userPermission = new WorkNC_UserPermission(); userPermission.Username = model.Username; userPermission.CompanyId = model.CompanyId; userPermission.WebPermission = model.WebPermission; userPermission.AppPermission = model.AppPermission; userPermission.CreateDate = DateTime.Now; userPermission.CreateAccount = User.Identity.Name; userPermission.ModifiedDate = DateTime.Now; userPermission.ModifiedAccount = User.Identity.Name; db.WorkNC_UserPermission.Add(userPermission); UserManager.AddToRole(user.Id, model.WebPermission);//add to AspnetUserRoles table db.SaveChanges(); //await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false); //string callbackUrl = await SendEmailConfirmationTokenAsync(user.Id, "Confirm your account"); // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 // Send an email with this link // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); // Uncomment to debug locally // TempData["ViewBagLink"] = callbackUrl; //ViewBag.Message = "Check your email and confirm your account, you must be confirmed " //+ "before you can log in."; //return View("Info"); return(RedirectToAction("Index", "Home")); } catch (DbEntityValidationException e) { foreach (var item in e.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", item.Entry.Entity.GetType().Name, item.Entry.State); foreach (var ve in item.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }