public static ProfileViewModel CreateModel(ExternalLoginInfo loginInfo) { if (loginInfo == null) throw new ArgumentNullException("loginInfo"); var model = new ProfileViewModel(); model.Name = loginInfo.ExternalIdentity.Name; model.Email = loginInfo.Email; var claim = loginInfo.ExternalIdentity.Claims.FirstOrDefault(c => c.Type == "urn:facebook:first_name"); if (claim != null) model.FirstName = claim.Value; claim = loginInfo.ExternalIdentity.Claims.FirstOrDefault(c => c.Type == "urn:facebook:last_name"); if (claim != null) model.LastName = claim.Value; claim = loginInfo.ExternalIdentity.Claims.FirstOrDefault(c => c.Type == "urn:facebook:gender"); if (claim != null) model.Gender = claim.Value; DateTime birthDay = DateTime.MinValue; claim = loginInfo.ExternalIdentity.Claims.FirstOrDefault(c => c.Type == "urn:facebook:birthday"); if (claim != null && DateTime.TryParseExact(claim.Value, "MM/dd/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out birthDay)) model.BirthDay = birthDay; return model; }
public async Task<ActionResult> ExternalLoginConfirmation(ProfileViewModel model, string returnUrl) { if (User.Identity.IsAuthenticated) return RedirectToAction("Manage"); if (ModelState.IsValid) { // Get the information about the user from the external login provider var info = await AuthenticationManager.GetExternalLoginInfoAsync(); if (info == null) return View("ExternalLoginFailure"); var user = new ApplicationUser() { UserName = model.Email, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, Gender = model.Gender, BirthDay = model.BirthDay, StateID = model.StateID, ProvinceID = model.CityID }; IdentityResult result = await UserManager.CreateAsync(user); if (result.Succeeded) { result = await UserManager.AddLoginAsync(user.Id, info.Login); if (result.Succeeded) { await SignInAsync(user, isPersistent: false); // 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); // SendEmail(user.Email, callbackUrl, "Confirm your account", "Please confirm your account by clicking this link"); return RedirectToLocal(returnUrl); } } AddErrors(result); } ViewBag.ReturnUrl = returnUrl; return View(model); }
public async Task<ActionResult> EditProfile(ProfileViewModel model) { if (ModelState.IsValid) { var userId = User.Identity.GetUserId(); var user = await UserManager.FindByIdAsync(userId); user.FirstName = model.FirstName; user.LastName = model.FirstName; user.Gender = model.Gender; user.BirthDay = model.BirthDay; user.StateID = model.StateID; user.ProvinceID = model.CityID; var result = await UserManager.UpdateAsync(user); if (result.Succeeded) { return RedirectToAction("Manage", "Account"); } else { AddErrors(result); } } // If we got this far, something failed, redisplay form return View(model); }
public ActionResult EditProfile() { var model = new ProfileViewModel(); var userId = User.Identity.GetUserId(); var user = UserManager.FindById(userId); if (user != null) { model.FirstName = user.FirstName; model.LastName = user.LastName; model.Email = user.Email; model.BirthDay = user.BirthDay.HasValue ? user.BirthDay.Value : DateTime.Now; model.StateID = user.StateID; model.CityID = user.ProvinceID; } return View(model); }