public async Task <bool> CreateUserAsync(ApplicationUser user, Role role) { // Find role var appRole = await _roleManager.FindByNameAsync(ApplicationRole.GetRoleName(role)); if (appRole == null) { Log.Warning("Trying to add user: {user} into non-existing role: {role}", user, role); throw new EntityNotFoundException($"Role does not exist"); } // Create user user.EmailConfirmed = false; user.EmailConfirmationCode = StringGenerator.GetRandomString(16); var createResult = await _userManager.CreateAsync(user, StringGenerator.GetRandomString(16)); if (!createResult.Succeeded) { Log.Information("Could not create user: {createResult.Errors.ToList()}", createResult.Errors.ToList()); return(false); } // Assign user to role var addToRoleResult = await _userManager.AddToRoleAsync(user, appRole.Name); return(addToRoleResult.Succeeded); // TODO: send mail // Send email to user with his email confirmation code }
public static IServiceCollection AddAuthPolicies(this IServiceCollection services) { services.AddAuthorization(options => { options.AddPolicy("RequireAdministrator", policy => { policy.RequireRole(ApplicationRole.GetRoleName(Role.Admin)); }); }); services.AddAuthorization(options => { options.AddPolicy("RequireEmployer", policy => { policy.RequireRole(ApplicationRole.GetRoleName(Role.Employer)); }); }); services.AddAuthorization(options => { options.AddPolicy("RequireEmployee", policy => { policy.RequireRole(ApplicationRole.GetRoleName(Role.Employee)); }); }); return(services); }
public async Task <PaginatedList <ApplicationUser> > GetUsersAsync(UsersQuery usersQuery) { IQueryable <ApplicationUser> users; if (usersQuery.Role.HasValue) { users = (await _userManager.GetUsersInRoleAsync(ApplicationRole.GetRoleName(usersQuery.Role.Value))) .AsQueryable(); } else { users = _userManager.Users; } if (!string.IsNullOrEmpty(usersQuery.Name)) { users = from u in users where EF.Functions.Like(u.Name, $"%{usersQuery.Name}%") select u; } return(PaginatedList <ApplicationUser> .Create(users, usersQuery.Page ?? 1, 20)); }