// // POST: /Account/Register [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public async Task Register(RegisterViewModel model) { if (!ModelState.IsValid) return View(model); var user = new ApplicationUser() { UserName = model.UserName }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { try { using (var dal = new DALEF.WebShopEntities()) { dal.Client.Add( new DALEF.Client{ CLI_Nom = model.Nom, CLI_Prenom = model.Prenom, CLI_Civilite = model.Civilite, CLI_Email = model.Email, CLI_Adresse = model.Adresse, CLI_CodePostal = model.CodePostal, CLI_Ville = model.Ville, CLI_Telephone = model.Telephone, CLI_AspUser_Id = user.Id }); dal.SaveChanges(); } } catch (Exception) { // client n'est pas ajouté delete l'asp user créé ModelState.AddModelError("", "Echec création client, veuillez réessayer"); return View(model); } //await UserManager.AddToRoleAsync(user.Id, "client"); await SignInAsync(user, isPersistent:false); return RedirectToAction("Index", "Home"); } else { AddErrors(result); } // If we got this far, something failed, redisplay form return View(model); } |