public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl = null) { if (User.Identity.IsAuthenticated) { return RedirectToAction("Index", "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, Name = model.Name }; var result = await UserManager.CreateAsync(user); #if TESTING //Just for automated testing adding a claim named 'ManageStore' - Not required for production var manageClaim = info.ExternalIdentity.Claims.Where(c => c.Type == Areas.Admin.AdminConstants.ManageStore.Name).FirstOrDefault(); if (manageClaim != null) { await UserManager.AddClaimAsync(user, manageClaim, cancellationToken: Context.RequestAborted); } #endif if (result.Succeeded) { 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); }
private static void CreateAdminUser(UserManager<ApplicationUser> userManager) { var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(new ContosoWebContext(StaticConfig.DbContext.WebConnectionStringName))); if (!roleManager.RoleExists(AdminConstants.Role)) { roleManager.Create(new IdentityRole(AdminConstants.Role)); } var username = ConfigurationHelpers.GetString("Authentication.Administrator.UserName"); var password = ConfigurationHelpers.GetString("Authentication.Administrator.Password"); var user = userManager.FindByName(username); if (user == null) { user = new ApplicationUser { UserName = username, Email = username }; var result = userManager.Create(user, password); if (!result.Succeeded) throw new Exception(string.Format("Failed to create admin user: {0}", string.Join(",", result.Errors))); user = userManager.FindByName(username); userManager.AddToRole(user.Id, AdminConstants.Role); userManager.AddClaim(user.Id, new Claim(AdminConstants.ManageStore.Name, AdminConstants.ManageStore.Allowed)); } }
public async Task<ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { //Bug: Remember browser option missing? //Uncomment this and comment the later part if account verification is not needed. //await SignInManager.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); await UserManager.SendEmailAsync(user.Id, "Confirm your account", string.Format("Please confirm your account by clicking <a href=\"{0}\">here</a>", callbackUrl)); #if !DEMO return RedirectToAction("Index", "Home"); #else //To display the email link in a friendly page instead of sending email ViewBag.Link = callbackUrl; return View("DemoLinkDisplay"); #endif } AddErrors(result); } // If we got this far, something failed, redisplay form return View(model); }