public async Task <IActionResult> ExternalLoginConfirmation(ExternalLoginViewModel model, string returnUrl = null) { if (ModelState.IsValid) { // Get the information about the user from the external login provider var info = await _signInManager.GetExternalLoginInfoAsync(); if (info == null) { throw new ApplicationException("Error loading external login information during confirmation."); } var user = new AspNetCore2Angular5User { UserName = model.Email, Email = model.Email }; var result = await _userManager.CreateAsync(user); if (result.Succeeded) { result = await _userManager.AddLoginAsync(user, info); if (result.Succeeded) { await _signInManager.SignInAsync(user, isPersistent : false); _logger.LogInformation("User created an account using {Name} provider.", info.LoginProvider); return(RedirectToLocal(returnUrl)); } } AddErrors(result); } ViewData["ReturnUrl"] = returnUrl; return(View(nameof(ExternalLogin), model)); }
public async Task <IActionResult> Register(RegisterViewModel model, string returnUrl = null) { ViewData["ReturnUrl"] = returnUrl; if (ModelState.IsValid) { var user = new AspNetCore2Angular5User { UserName = model.Email, Email = model.Email }; var result = await _userManager.CreateAsync(user, model.Password); if (result.Succeeded) { _logger.LogInformation("User created a new account with password."); var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); var callbackUrl = Url.EmailConfirmationLink(user.Id, code, Request.Scheme); await _emailSender.SendEmailConfirmationAsync(model.Email, callbackUrl); await _signInManager.SignInAsync(user, isPersistent : false); _logger.LogInformation("User created a new account with password."); return(RedirectToLocal(returnUrl)); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }
/// <summary> /// Creates a Site Admin /// </summary> /// <param name="serviceProvider">IService Provider</param> private static async Task <IdentityResult> CreateAdminUserAsync(IServiceProvider serviceProvider) { var _configuration = Startup.Configuration; var adminRole = _configuration[defaultAdminRoleName]; IdentityResult ir = new IdentityResult(); var _userManager = serviceProvider.GetService <UserManager <AspNetCore2Angular5User> >(); var _roleManager = serviceProvider.GetService <RoleManager <AspNetCore2Angular5Role> >(); if (!await _roleManager.RoleExistsAsync(adminRole)) { await _roleManager.CreateAsync(new AspNetCore2Angular5Role() { Name = adminRole, Description = "Site Administrator", CreateBy = _configuration[defaultAdminUserName], EntryDate = DateTime.UtcNow }); } var user = await _userManager.FindByNameAsync(_configuration[defaultAdminUserName]) ?? await _userManager.FindByEmailAsync(_configuration[defaultAdminEmail]); if (user == null) { user = new AspNetCore2Angular5User { UserName = _configuration[defaultAdminUserName], Email = _configuration[defaultAdminEmail], JoinDate = DateTime.UtcNow, LastLogInDate = DateTime.UtcNow, EmailLinkDate = DateTime.UtcNow }; ir = await _userManager.CreateAsync(user, _configuration[defaultAdminPassword]); if (ir.Succeeded) { ir = await _userManager.AddToRoleAsync(user, adminRole); } if (ir.Succeeded) { ir = await _userManager.AddClaimAsync(user, new Claim("SiteManager", "Allowed")); } } return(ir); }