public async Task<ActionResult> ChangeProfile(ApplicationUser applicationUser) { ManageMessageId manageMessageId; ApplicationUser retrievedApplicationUser = await UserManager.FindByIdAsync(User.Identity.GetUserId()); retrievedApplicationUser.FirstName = applicationUser.FirstName; retrievedApplicationUser.LastName = applicationUser.LastName; retrievedApplicationUser.Address = applicationUser.Address; retrievedApplicationUser.City = applicationUser.City; retrievedApplicationUser.ZipCode = applicationUser.ZipCode; // Update profile var result = await UserManager.UpdateAsync(retrievedApplicationUser); if (result.Succeeded) { manageMessageId = ManageMessageId.ChangeProfileSuccess; // If email address changed, sync both email and username if (retrievedApplicationUser.Email != applicationUser.Email) { // update user name string previousUserName = retrievedApplicationUser.UserName; retrievedApplicationUser.UserName = applicationUser.UserName; result = await UserManager.UpdateAsync(retrievedApplicationUser); if (result.Succeeded) { // update the Email address result = await UserManager.SetEmailAsync(retrievedApplicationUser.Id, applicationUser.Email); if (result.Succeeded) { manageMessageId = ManageMessageId.ChangeEmailSuccess; string code = await UserManager.GenerateEmailConfirmationTokenAsync(retrievedApplicationUser.Id); var callbackUrl = Url.Action( "ConfirmEmail", "Account", new { userId = retrievedApplicationUser.Id, code = code }, protocol: Request.Url.Scheme); await UserManager.SendEmailAsync( retrievedApplicationUser.Id, "Confirmez votre compte", "Merci de confirmer la création de votre compte en cliquant <a href=\"" + callbackUrl + "\">sur ce lien</a>"); } else { retrievedApplicationUser.UserName = previousUserName; result = await UserManager.UpdateAsync(retrievedApplicationUser); manageMessageId = ManageMessageId.Error; } } else { retrievedApplicationUser.UserName = previousUserName; result = await UserManager.UpdateAsync(retrievedApplicationUser); manageMessageId = ManageMessageId.Error; } } } else manageMessageId = ManageMessageId.Error; return RedirectToAction("Index", new { Message = manageMessageId }); }
public async Task<ActionResult> GenerateEmailConfirmation(ApplicationUser user) { 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, "Confirmez votre compte", "Merci de confirmer la création de votre compte en cliquant <a href=\"" + callbackUrl + "\">sur ce lien</a>"); return View("CheckYourEmail"); }
public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl) { if (User.Identity.IsAuthenticated) { return RedirectToAction("Index", "Manage"); } if (ModelState.IsValid) { // Obtenez des informations sur l’utilisateur auprès du fournisseur de connexions externe var info = await AuthenticationManager.GetExternalLoginInfoAsync(); if (info == null) { return View("ExternalLoginFailure"); } string firstName = info.ExternalIdentity.Claims.First(c => c.Type.Contains("givenname")).Value ?? string.Empty; string lastName = info.ExternalIdentity.Claims.First(c => c.Type.Contains("surname")).Value ?? string.Empty; var user = new ApplicationUser { UserName = model.Email, Email = model.Email, FirstName = firstName.Substring(0, Math.Min(firstName.Length, 15)), LastName = lastName.Substring(0, Math.Min(lastName.Length, 15)) }; var result = await UserManager.CreateAsync(user); if (result.Succeeded) { await UserManager.SetTwoFactorEnabledAsync(user.Id, true); result = await UserManager.AddLoginAsync(user.Id, info.Login); if (result.Succeeded) { await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false); return RedirectToLocal(returnUrl); } } AddErrors(result); } ViewBag.ReturnUrl = returnUrl; return View(model); }
public async Task<ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model.Email, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, Address = model.Address, City = model.City, ZipCode = model.ZipCode }; var result = await UserManager.CreateAsync(user, model.Password); await UserManager.SetTwoFactorEnabledAsync(user.Id, true); if (result.Succeeded) { return await GenerateEmailConfirmation(user); } AddErrors(result); } // Si nous sommes arrivés là, un échec s’est produit. Réafficher le formulaire return View(model); }