private async Task CreateRoles(IServiceProvider serviceProvider) { var roleManager = serviceProvider.GetRequiredService <RoleManager <IdentityRole> >(); var userManager = serviceProvider.GetRequiredService <UserManager <CodecampUser> >(); string[] roleNames = { "Admin", "Speaker", "Volunteer", "Attendee" }; IdentityResult roleResult; // Create the application roles, if they do not exist foreach (var roleName in roleNames) { var roleExists = await roleManager.RoleExistsAsync(roleName); if (!roleExists) { roleResult = await roleManager.CreateAsync(new IdentityRole(roleName)); } } // Create the site admin user var adminUsername = Configuration.GetSection("AppSettings")["AdminUser"]; var adminEmail = Configuration.GetSection("AppSettings")["AdminUser"]; var adminPassword = Configuration.GetSection("AppSettings")["AdminPass"]; if (adminUsername.Length != 0 && adminPassword.Length != 0 && adminEmail.Length != 0) { var sysAdmin = new CodecampUser { UserName = adminUsername, Email = adminEmail, EmailConfirmed = true, }; var _user = await userManager.FindByEmailAsync(adminEmail); if (_user == null) { var createAdminUserResult = await userManager.CreateAsync(sysAdmin, adminPassword); if (createAdminUserResult.Succeeded) { // Assign the sys admin user to the admin role await userManager.AddToRoleAsync(sysAdmin, "Admin"); } } } }
public ApiUser(CodecampUser webUser, bool includeDetails = false) { Id = webUser.Id; FirstName = webUser.FirstName; LastName = webUser.LastName; // TODO Future //EventId = webSpeaker.EventId; if (!includeDetails) { return; } EmailAddress = webUser.Email; }
private async Task LoadSharedKeyAndQrCodeUriAsync(CodecampUser user) { // Load the authenticator key & QR code URI to display on the form var unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user); if (string.IsNullOrEmpty(unformattedKey)) { await _userManager.ResetAuthenticatorKeyAsync(user); unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user); } SharedKey = FormatKey(unformattedKey); var email = await _userManager.GetEmailAsync(user); AuthenticatorUri = GenerateQrCodeUri(email, unformattedKey); }
public async Task <IActionResult> OnPostConfirmationAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); // Get the information about the user from the external login provider var info = await _signInManager.GetExternalLoginInfoAsync(); if (info == null) { ErrorMessage = "Error loading external login information during confirmation."; return(RedirectToPage("./Login", new { ReturnUrl = returnUrl })); } if (ModelState.IsValid) { var user = new CodecampUser { UserName = Input.Email, Email = Input.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(LocalRedirect(returnUrl)); } } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } LoginProvider = info.LoginProvider; ReturnUrl = returnUrl; return(Page()); }
public async Task <bool> UpdateUser(CodecampUser user) { try { _context.CodecampUsers.Update(user); await _context.SaveChangesAsync(); return(true); } catch (DbUpdateConcurrencyException) { if (!await UserExists(user.Id)) { return(false); } else { throw; } } }
public async Task <IActionResult> OnPostAsync(string returnUrl = null) { if (ModelState.IsValid) { // Get the current event var theEvent = await _eventBL.GetActiveEvent(); var user = new CodecampUser { IsAttending = true, IsSpeaker = false, UserName = Input.Email, Email = Input.Email, IsVolunteer = Input.IsVolunteer, EventId = theEvent != null ? theEvent.EventId : (int?)null }; var result = await _userManager.CreateAsync(user, Input.Password); if (result.Succeeded) { _logger.LogInformation("User created a new account with password."); await _userManager.AddToRoleAsync(user, "Attendee"); // Save the DB changes await _context.SaveChangesAsync(); var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); var callbackUrl = Url.Page( "/Account/ConfirmEmail", pageHandler: null, values: new { userId = user.Id, code = code }, protocol: Request.Scheme); // Generate and send a confirmation email to the user await _emailSender.SendEmailAsync(Input.Email, "Confirm your email", $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>."); // Redirect to the registration almost complete page return(RedirectToPage("./RegistrationAlmostComplete")); } foreach (var error in result.Errors) { if (error.Code == "DuplicateUserName") { // The account currently exists, redirect to login page, note // this is to complete speaker registration. return(RedirectToPage("./Login", new { ReturnUrl = "./Manage/Index", LoginWithRegistration = "Attendee", Email = Input.Email // pass the email/username for convenience })); } ModelState.AddModelError(string.Empty, error.Description); } } // If we got this far, something failed, redisplay form return(Page()); }