private async Task SignInAsync(User user, bool isPersistent) { AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie); var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie); AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity); }
public async Task<ActionResult> Register(RegisterViewModel model, string returnUrl) { if (model.Password != model.ConfirmPassword) { ModelState.AddModelError("ConfirmPassword", "Пароль и Подтверждение пароля должны совпадать"); } if (ModelState.IsValid) { User user = null; if (!string.IsNullOrWhiteSpace(model.DriverLicense)) { user = db.Users.FirstOrDefault(u => u.Licenses.Any(n => n.Number == model.DriverLicense && n.Type == LicenseType.Driver) && u.PasswordHash == null); } if (user==null && !string.IsNullOrWhiteSpace(model.MarchalLicense)) { user = db.Users.FirstOrDefault(u => u.Licenses.Any(n => n.Number == model.MarchalLicense && n.Type==LicenseType.Marchal) && u.PasswordHash == null); } if (user == null) { user = new User() { UserName = model.UserName, Address = model.Address, BirthDate = model.BirthDate, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, Location = model.Location, Passport = model.Passport, Phone = model.Phone, Licenses = new List<License>() }; if (model.DriverLicense != null) { user.Licenses.Add(new License() { Number = model.DriverLicense, Season = 2014, IssuesOn = DateTime.Today }); } if (model.MarchalLicense != null) { user.Licenses.Add(new License() { Number = model.MarchalLicense, Season = 2014, IssuesOn = DateTime.Today, Type = LicenseType.Marchal }); } var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { await SignInAsync(user, isPersistent: false); if (string.IsNullOrWhiteSpace(returnUrl)) return Redirect("~/"); else return Redirect(returnUrl); } else { AddErrors(result); } } else { user.UserName = model.UserName; await db.SaveChangesAsync(); var result = await UserManager.AddPasswordAsync(model.UserName, model.Password); if (result.Succeeded) { await SignInAsync(user, isPersistent: false); if (string.IsNullOrWhiteSpace(returnUrl)) return Redirect("~/"); else return Redirect(returnUrl); } else { AddErrors(result); } } } // If we got this far, something failed, redisplay form return View(model); }
public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel 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 User() { UserName = model.UserName, Address = model.Address, BirthDate = model.BirthDate, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, Location = model.Location, Passport = model.Passport, Phone = model.Phone, Licenses = new List<License>() }; var result = await UserManager.CreateAsync(user); if (result.Succeeded) { result = await UserManager.AddLoginAsync(user.Id, info.Login); if (result.Succeeded) { await SignInAsync(user, isPersistent: false); return RedirectToLocal(returnUrl); } } AddErrors(result); } ViewBag.ReturnUrl = returnUrl; return View(model); }