public async Task <IActionResult> OnPostAsync(string NetID) { WFUser = await Context.WudFilmAppUser.FindAsync(NetID); var contact = await Context .WudFilmAppUser.AsNoTracking() .FirstOrDefaultAsync(m => m.NetID.Equals(NetID)); if (contact == null) { return(NotFound()); } var isAuthorized = await AuthorizationService.AuthorizeAsync( User, contact, ContactOperations.Delete); if (!isAuthorized.Succeeded) { return(new ChallengeResult()); } Context.WudFilmAppUser.Remove(WFUser); await Context.SaveChangesAsync(); return(RedirectToPage("./Index")); }
public IActionResult OnGet() { WudFilmAppUser = new WudFilmAppUser { NetID = "WUD", Email = "*****@*****.**" }; return(Page()); }
public async Task <IActionResult> DeleteUser(string id) { string email = string.Empty; if (!String.IsNullOrEmpty(id)) { WudFilmAppUser applicationUser = await userManager.FindByIdAsync(id); if (applicationUser != null) { email = applicationUser.Email; } } return(PartialView("_DeleteUser", email)); }
public async Task <IActionResult> DeleteUser(string id, IFormCollection form) { if (!String.IsNullOrEmpty(id)) { WudFilmAppUser applicationUser = await userManager.FindByIdAsync(id); if (applicationUser != null) { IdentityResult result = await userManager.DeleteAsync(applicationUser); if (result.Succeeded) { return(RedirectToAction("Index")); } } } return(View()); }
private async Task LoadSharedKeyAndQrCodeUriAsync(WudFilmAppUser 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> OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); if (ModelState.IsValid) { var user = new WudFilmAppUser { UserName = Input.NetID, Email = Input.NetID + "@wisc.edu", NetID = Input.NetID }; var result = await _userManager.CreateAsync(user, Input.Password); if (result.Succeeded) { await _userManager.AddToRoleAsync(user, "Student"); _logger.LogInformation("User created a new account with password."); var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); var callbackUrl = Url.Page( "/Account/ConfirmEmail", pageHandler: null, values: new { userId = user.Id, code = code }, protocol: Request.Scheme); await _emailSender.SendEmailAsync(Input.NetID + "@wisc.edu", "Confirm your email", $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>."); await _signInManager.SignInAsync(user, isPersistent : false); return(LocalRedirect(returnUrl)); } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } // If we got this far, something failed, redisplay form return(Page()); }
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 WudFilmAppUser { 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 <IActionResult> OnGetAsync(string NetID) { WFUser = await Context.WudFilmAppUser.FirstOrDefaultAsync( m => m.NetID == NetID); if (WFUser == null) { return(NotFound()); } var isAuthorized = await AuthorizationService.AuthorizeAsync( User, WFUser, ContactOperations.Update); if (!isAuthorized.Succeeded) { return(new ChallengeResult()); } return(Page()); }
public async Task <IActionResult> EditUser(string id, EditUserViewModel model) { if (ModelState.IsValid) { WudFilmAppUser user = await userManager.FindByIdAsync(id); if (user != null) { //user.Name = model.Name; user.Email = model.Email; string existingRole = userManager.GetRolesAsync(user).Result.Single(); string existingRoleId = roleManager.Roles.Single(r => r.Name == existingRole).Id; IdentityResult result = await userManager.UpdateAsync(user); if (result.Succeeded) { if (existingRoleId != model.ApplicationRoleId) { IdentityResult roleResult = await userManager.RemoveFromRoleAsync(user, existingRole); if (roleResult.Succeeded) { IdentityRole applicationRole = await roleManager.FindByIdAsync(model.ApplicationRoleId); if (applicationRole != null) { IdentityResult newRoleResult = await userManager.AddToRoleAsync(user, applicationRole.Name); if (newRoleResult.Succeeded) { return(RedirectToAction("Index")); } } } } } } } return(PartialView("_EditUser", model)); }
public async Task <IActionResult> OnGetAsync(string NetID) { WFUser = await Context.WudFilmAppUser.FirstOrDefaultAsync(m => m.NetID.Equals(NetID)); if (WFUser == null) { return(NotFound()); } var isAuthorized = User.IsInRole(Constants.ContactManagersRole) || User.IsInRole(Constants.ContactAdministratorsRole); var currentUserId = UserManager.GetUserId(User); if (!isAuthorized && currentUserId != WFUser.OwnerID && WFUser.Status != ContactStatus.Approved) { return(new ChallengeResult()); } return(Page()); }
public async Task <IActionResult> EditUser(string id) { EditUserViewModel model = new EditUserViewModel(); model.ApplicationRoles = roleManager.Roles.Select(r => new SelectListItem { Text = r.Name, Value = r.Id }).ToList(); if (!String.IsNullOrEmpty(id)) { WudFilmAppUser user = await userManager.FindByIdAsync(id); if (user != null) { //model.Name = user.Name; model.Email = user.Email; model.ApplicationRoleId = roleManager.Roles.Single(r => r.Name == userManager.GetRolesAsync(user).Result.Single()).Id; } } return(PartialView("_EditUser", model)); }
private async Task CreateUserRoles(IServiceProvider serviceProvider) { var RoleManager = serviceProvider.GetRequiredService <RoleManager <IdentityRole> >(); var UserManager = serviceProvider.GetRequiredService <UserManager <WudFilmAppUser> >(); string[] roleNames = { "Admin", "Student" }; IdentityResult roleResult; foreach (var roleName in roleNames) { var roleExist = await RoleManager.RoleExistsAsync(roleName); // ensure that the role does not exist if (!roleExist) { //create the roles and seed them to the database: roleResult = await RoleManager.CreateAsync(new IdentityRole(roleName)); } } // Creates Initial admin var poweruser = new WudFilmAppUser { NetID = "WUD", UserName = "******", Email = "*****@*****.**", }; string adminPassword = "******"; var createPowerUser = await UserManager.CreateAsync(poweruser, adminPassword); if (createPowerUser.Succeeded) { await UserManager.AddToRoleAsync(poweruser, "Admin"); } }