private static void CreateAdminUser(FaToolIdentityDbContext context) { using (var store = new FaToolUserStore(context)) using (var manager = new FaToolUserManager(store)) { var user = manager.FindByName(FaToolUsers.ADMIN); if (user == null) { user = new FaToolUser(FaToolUsers.ADMIN); user.Email = "*****@*****.**"; user.FirstName = "FirstName"; user.LastName = "LastName"; user.Company = "Company"; user.Address = "Address"; user.City = "City"; user.ZipCode = "ZipCode"; user.State = "State"; user.Country = Countries.Default.Code; var result = manager.Create(user, "Admin123#"); if (result.Succeeded == false) { throw new ApplicationException( string.Format("Error creating admin user, reason: {0}.", result.Errors.FirstOrDefault())); } result = manager.AddToRoles(user.Id, FaToolRoles.Values.ToArray()); if (result.Succeeded == false) { throw new ApplicationException( string.Format("Error grant admin user to roles, reason: {0}.", result.Errors.FirstOrDefault())); } } } }
public static async Task <IdentityResult> LockAccountAsync(this FaToolUserManager um, FaToolUser user, int?forMinutes) { if (user.IsAdminUser()) { return(IdentityResult.Failed("Can't lock admin user account.")); } var result = await um.SetLockoutEnabledAsync(user.Id, true); if (result.Succeeded) { if (forMinutes.HasValue) { result = await um.SetLockoutEndDateAsync(user.Id, DateTimeOffset.UtcNow.AddMinutes(forMinutes.Value)); } else { result = await um.SetLockoutEndDateAsync(user.Id, DateTimeOffset.MaxValue); } } return(result); }
public static async Task <IdentityResult> UpdateRolesAsync(this FaToolUserManager um, FaToolUser user, params string[] roles) { if (roles == null) { roles = new string[] { } } ; var currentRoles = await um.GetRolesAsync(user.Id); var result = await um.RemoveFromRolesAsync(user.Id, currentRoles.ToArray()); if (result.Succeeded) { result = await um.AddToRolesAsync(user.Id, roles); } return(result); }
public static async Task <IdentityResult> DeleteAccountAsync(this FaToolUserManager um, FaToolUser user) { if (user.IsAdminUser()) { return(IdentityResult.Failed("Can't delete admin user account.")); } var currentRoles = await um.GetRolesAsync(user.Id); var result = await um.RemoveFromRolesAsync(user.Id, currentRoles.ToArray()); if (result.Succeeded) { result = await um.DeleteAsync(user); } return(result); }
public static async Task <IdentityResult> CreateAccountAsync(this FaToolUserManager um, FaToolUser user, string password, params string[] roles) { var result = await um.CreateAsync(user, password); if (result.Succeeded && roles != null && roles.Length > 0) { result = await um.AddToRolesAsync(user.Id, roles); } return(result); }
public static async Task <IdentityResult> UnlockAccountAsync(this FaToolUserManager um, FaToolUser user) { if (await um.IsLockedOutAsync(user.Id) == false) { return(IdentityResult.Success); } var result = await um.SetLockoutEndDateAsync(user.Id, DateTimeOffset.UtcNow); if (!result.Succeeded) { return(result); } result = await um.SetLockoutEnabledAsync(user.Id, false); if (!result.Succeeded) { return(result); } result = await um.ResetAccessFailedCountAsync(user.Id); if (!result.Succeeded) { return(result); } return(result); }