public async void AddRemovePassword() { // Create user without password var user = new User(Guid.NewGuid()) { UserName = "******" }; await UserManager.CreateAsync(user); // Adding a password should succeed IdentityResult result = await UserManager.AddPasswordAsync(user.Id, "somePassword"); result.ShouldBeSuccess(); bool hasPassword = await UserManager.HasPasswordAsync(user.Id); hasPassword.Should().BeTrue(); // Now removing a password should succeed result = await UserManager.RemovePasswordAsync(user.Id); result.ShouldBeSuccess(); hasPassword = await UserManager.HasPasswordAsync(user.Id); hasPassword.Should().BeFalse(); }
public async void ChangeUsername() { // Create user, then lookup by Id var originalUser = new User(Guid.NewGuid()) { UserName = "******" }; await UserManager.CreateAsync(originalUser); User foundUser = await UserManager.FindByIdAsync(originalUser.Id); // Change the username and update const string newUserName = "******"; foundUser.UserName = newUserName; IdentityResult result = await UserManager.UpdateAsync(foundUser); result.ShouldBeSuccess(); // Should not be able to find them by the old username foundUser = await UserManager.FindByNameAsync(originalUser.UserName); foundUser.Should().BeNull(); // Should still be able to find by id and new username foundUser = await UserManager.FindByIdAsync(originalUser.Id); foundUser.Should().NotBeNull(); foundUser.UserName.Should().Be(newUserName); foundUser = await UserManager.FindByNameAsync(newUserName); foundUser.Should().NotBeNull(); foundUser.Id.Should().Be(originalUser.Id); }
public async void DeleteUser() { // Create user, then lookup by Id var originalUser = new User(Guid.NewGuid()) { UserName = "******" }; await UserManager.CreateAsync(originalUser); User foundUser = await UserManager.FindByIdAsync(originalUser.Id); // Delete the user IdentityResult result = await UserManager.DeleteAsync(foundUser); result.ShouldBeSuccess(); // Should not be able to find by id or username foundUser = await UserManager.FindByIdAsync(originalUser.Id); foundUser.Should().BeNull(); foundUser = await UserManager.FindByNameAsync(originalUser.UserName); foundUser.Should().BeNull(); }
public async void LockUserOut() { // Create a user and enable lockout var user = new User(Guid.NewGuid()) { UserName = "******" }; await UserManager.CreateAsync(user); await UserManager.SetLockoutEnabledAsync(user.Id, true); // Should be able to record a failed login IdentityResult result = await UserManager.AccessFailedAsync(user.Id); result.ShouldBeSuccess(); // Since the test setup uses one as the threshold for lockouts, the user should now be locked out bool isLockedOut = await UserManager.IsLockedOutAsync(user.Id); isLockedOut.Should().BeTrue(); // Since the test setup set the lockout period to 15 mins, the lockout end date should be approximately 15 mins from now DateTimeOffset lockoutEndDate = await UserManager.GetLockoutEndDateAsync(user.Id); lockoutEndDate.Should().BeCloseTo(DateTimeOffset.UtcNow.Add(15.Minutes()), precision: 1000); // 1000 == Within 1 second }
public async void ResetPassword() { // Create a user with a password var user = new User(Guid.NewGuid()) { UserName = "******" }; await UserManager.CreateAsync(user, "somePassword"); // Generate a reset token and then reset the password should succeed string token = await UserManager.GeneratePasswordResetTokenAsync(user.Id); IdentityResult result = await UserManager.ResetPasswordAsync(user.Id, token, "someNewPassword"); result.ShouldBeSuccess(); // Should now be able to authenticate with new password user = await UserManager.FindByIdAsync(user.Id); bool authenticated = await UserManager.CheckPasswordAsync(user, "someNewPassword"); authenticated.Should().BeTrue(); // Should not be able to authenticate with old password authenticated = await UserManager.CheckPasswordAsync(user, "somePassword"); authenticated.Should().BeFalse(); }
public async void PhoneNumberConfirmation() { // Create a user with a phone number const string phone = "555-555-1212"; var user = new User(Guid.NewGuid()) { UserName = "******" }; await UserManager.CreateAsync(user); await UserManager.SetPhoneNumberAsync(user.Id, phone); // Should not be confirmed by default bool isConfirmed = await UserManager.IsPhoneNumberConfirmedAsync(user.Id); isConfirmed.Should().BeFalse(); // Generate a token to verify the phone number string token = await UserManager.GenerateChangePhoneNumberTokenAsync(user.Id, phone); IdentityResult result = await UserManager.ChangePhoneNumberAsync(user.Id, phone, token); result.ShouldBeSuccess(); // Phone number should now be confirmed isConfirmed = await UserManager.IsPhoneNumberConfirmedAsync(user.Id); isConfirmed.Should().BeTrue(); }
public async void GetAndSetEmail() { // Create a user var user = new User(Guid.NewGuid()) { UserName = "******" }; await UserManager.CreateAsync(user); // User should not have an email address initially string userEmail = await UserManager.GetEmailAsync(user.Id); userEmail.Should().BeNullOrEmpty(); // Set email address for user const string email = "*****@*****.**"; IdentityResult result = await UserManager.SetEmailAsync(user.Id, email); result.ShouldBeSuccess(); // Now user should have email userEmail = await UserManager.GetEmailAsync(user.Id); userEmail.Should().Be(email); }
public async void GetSetPhoneNumber() { // Create a user var user = new User(Guid.NewGuid()) { UserName = "******" }; await UserManager.CreateAsync(user); // Should not have phone number by default string phoneNumber = await UserManager.GetPhoneNumberAsync(user.Id); phoneNumber.Should().BeNullOrEmpty(); // Can set phone number const string phone = "555-555-1212"; IdentityResult result = await UserManager.SetPhoneNumberAsync(user.Id, phone); result.ShouldBeSuccess(); // Should now have phone number phoneNumber = await UserManager.GetPhoneNumberAsync(user.Id); phoneNumber.Should().Be(phone); }
public async void ChangeEmail() { // Create a user and set their email address const string email = "*****@*****.**"; var user = new User(Guid.NewGuid()) { UserName = "******" }; await UserManager.CreateAsync(user); await UserManager.SetEmailAsync(user.Id, email); // Change their email address const string newEmail = "*****@*****.**"; IdentityResult result = await UserManager.SetEmailAsync(user.Id, newEmail); result.ShouldBeSuccess(); // Should not be able to find the user by the old email address User foundUser = await UserManager.FindByEmailAsync(email); foundUser.Should().BeNull(); // Should be able to find the user by the new email address foundUser = await UserManager.FindByEmailAsync(newEmail); foundUser.ShouldBeEquivalentToUser(user); }
public async void ChangePassword() { // Create a user with a password var user = new User(Guid.NewGuid()) { UserName = "******" }; await UserManager.CreateAsync(user, "somePassword"); // Should be able to change the password IdentityResult result = await UserManager.ChangePasswordAsync(user.Id, "somePassword", "someNewPassword"); result.ShouldBeSuccess(); // Should be able to authenticate with new password user = await UserManager.FindByIdAsync(user.Id); bool authenticated = await UserManager.CheckPasswordAsync(user, "someNewPassword"); authenticated.Should().BeTrue(); // Should not be able to authenticate with old password authenticated = await UserManager.CheckPasswordAsync(user, "somePassword"); authenticated.Should().BeFalse(); }
public async void EmailConfirmation() { // Create a user and set their email address const string email = "*****@*****.**"; var user = new User(Guid.NewGuid()) { UserName = "******" }; await UserManager.CreateAsync(user); await UserManager.SetEmailAsync(user.Id, email); // Email should NOT be confirmed by default bool confirmed = await UserManager.IsEmailConfirmedAsync(user.Id); confirmed.Should().BeFalse(); // Generate a token and confirm the email string token = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); IdentityResult result = await UserManager.ConfirmEmailAsync(user.Id, token); result.ShouldBeSuccess(); // Email should now be confirmed confirmed = await UserManager.IsEmailConfirmedAsync(user.Id); confirmed.Should().BeTrue(); }
public async void DeleteRole() { // Create role, then lookup by Id var originalRole = new Role(Guid.NewGuid()) { Name = "deletedRole" }; await RoleManager.CreateAsync(originalRole); Role foundRole = await RoleManager.FindByIdAsync(originalRole.Id); // Delete the role IdentityResult result = await RoleManager.DeleteAsync(foundRole); result.ShouldBeSuccess(); // Should not be able to find by id or rolename foundRole = await RoleManager.FindByIdAsync(originalRole.Id); foundRole.Should().BeNull(); foundRole = await RoleManager.FindByNameAsync(originalRole.Name); foundRole.Should().BeNull(); }
public async void ChangeRolename() { // Create role, then lookup by Id var originalRole = new Role(Guid.NewGuid()) { Name = "originalName" }; await RoleManager.CreateAsync(originalRole); Role foundRole = await RoleManager.FindByIdAsync(originalRole.Id); // Change the rolename and update const string newName = "testRole2"; foundRole.Name = newName; IdentityResult result = await RoleManager.UpdateAsync(foundRole); result.ShouldBeSuccess(); // Should not be able to find them by the old rolename foundRole = await RoleManager.FindByNameAsync(originalRole.Name); foundRole.Should().BeNull(); // Should still be able to find by id and new rolename foundRole = await RoleManager.FindByIdAsync(originalRole.Id); foundRole.Should().NotBeNull(); foundRole.Name.Should().Be(newName); foundRole = await RoleManager.FindByNameAsync(newName); foundRole.Should().NotBeNull(); foundRole.Id.Should().Be(originalRole.Id); }
public async void CreateUser() { // Create user var originalUser = new User(Guid.NewGuid()) { UserName = "******" }; IdentityResult result = await UserManager.CreateAsync(originalUser); result.ShouldBeSuccess(); // Try to find users by id and username User foundUser = await UserManager.FindByIdAsync(originalUser.Id); foundUser.ShouldBeEquivalentToUser(originalUser); foundUser = await UserManager.FindByNameAsync(originalUser.UserName); foundUser.ShouldBeEquivalentToUser(originalUser); }
public async void CreateRole() { // Create role var originalRole = new Role(Guid.NewGuid()) { Name = "testRole1" }; IdentityResult result = await RoleManager.CreateAsync(originalRole); result.ShouldBeSuccess(); // Try to find roles by id and rolename Role foundRole = await RoleManager.FindByIdAsync(originalRole.Id); foundRole.ShouldBeEquivalentToRole(originalRole); foundRole = await RoleManager.FindByNameAsync(originalRole.Name); foundRole.ShouldBeEquivalentToRole(originalRole); }
public async void GetSetLockoutEndDate() { // Create a user and enable lockout var user = new User(Guid.NewGuid()) { UserName = "******" }; await UserManager.CreateAsync(user); await UserManager.SetLockoutEnabledAsync(user.Id, true); // Should be able to set lockout end date DateTimeOffset lockoutDate = DateTimeOffset.UtcNow; IdentityResult result = await UserManager.SetLockoutEndDateAsync(user.Id, lockoutDate); result.ShouldBeSuccess(); // Should be able to retrieve that lockout date DateTimeOffset lookupDate = await UserManager.GetLockoutEndDateAsync(user.Id); lookupDate.Should().BeCloseTo(lockoutDate); // Use CloseTo because C* is not accurate down to the ticks level }
public async void GetSetLockoutEnabled() { // Create a user var user = new User(Guid.NewGuid()) { UserName = "******" }; await UserManager.CreateAsync(user); // Lockout should not be enabled by default bool isLockoutEnabled = await UserManager.GetLockoutEnabledAsync(user.Id); isLockoutEnabled.Should().BeFalse(); // Should be able to turn on lockout for a user IdentityResult result = await UserManager.SetLockoutEnabledAsync(user.Id, true); result.ShouldBeSuccess(); // Should now be enabled isLockoutEnabled = await UserManager.GetLockoutEnabledAsync(user.Id); isLockoutEnabled.Should().BeTrue(); }
public async void EnableDisableTwoFactor() { // Create a user var user = new User(Guid.NewGuid()) { UserName = "******" }; await UserManager.CreateAsync(user); // 2FA should be disabled by default bool isEnabled = await UserManager.GetTwoFactorEnabledAsync(user.Id); isEnabled.Should().BeFalse(); // Can set 2FA enabled IdentityResult result = await UserManager.SetTwoFactorEnabledAsync(user.Id, true); result.ShouldBeSuccess(); // Should be enabled now isEnabled = await UserManager.GetTwoFactorEnabledAsync(user.Id); isEnabled.Should().BeTrue(); }