/// <summary>
        /// Adds a password validator that checks the user's Email is not the same as the provided password
        /// </summary>
        /// <param name="builder">The Microsoft.AspNetCore.Identity.IdentityBuilder instance this method extends</param>
        /// <param name="invalidPhrases"></param>
        /// <typeparam name="TUser">The user type whose password will be validated.</typeparam>
        /// <returns>The current Microsoft.AspNetCore.Identity.IdentityBuilder instance.</returns>
        public static IdentityBuilder AddInvalidPhraseValidator <TUser>(this IdentityBuilder builder, IEnumerable <string> invalidPhrases)
            where TUser : IdentityUser
        {
            var validator = new InvalidPhraseValidator <TUser>(invalidPhrases);

            builder.Services.AddSingleton(typeof(IPasswordValidator <>).MakeGenericType(builder.UserType), validator);
            return(builder);
        }
Ejemplo n.º 2
0
        public async Task SuccessIfNotContained(string password, params string[] phrases)
        {
            var manager   = MockHelpers.TestUserManager <TestUser>();
            var validator = new InvalidPhraseValidator <TestUser>(phrases);
            var user      = new TestUser();

            IdentityResultAssert.IsSuccess(await validator.ValidateAsync(manager, user, password));
        }
Ejemplo n.º 3
0
        public async Task FailsIfContained(string password, params string[] phrases)
        {
            var manager   = MockHelpers.TestUserManager <TestUser>();
            var validator = new InvalidPhraseValidator <TestUser>(phrases);
            var user      = new TestUser();
            var error     = $"You cannot use '{password}' as your password";

            IdentityResultAssert.IsFailure(await validator.ValidateAsync(manager, user, password), error);
        }
Ejemplo n.º 4
0
        public async Task ValidateDoesNotThrowWithNullUserTest()
        {
            // Setup
            var validator = new InvalidPhraseValidator <TestUser>(new [] { "phrase" });
            var manager   = MockHelpers.TestUserManager <TestUser>();
            var password  = "******";

            // Act
            // Assert
            IdentityResultAssert.IsSuccess(await validator.ValidateAsync(manager, null, password));
        }
Ejemplo n.º 5
0
        public async Task ValidateThrowsWithNullTest()
        {
            // Setup
            var validator = new InvalidPhraseValidator <TestUser>(new [] { "phrase" });

            // Act
            // Assert
            await Assert.ThrowsAsync <ArgumentNullException>("password", () => validator.ValidateAsync(null, null, null));

            await Assert.ThrowsAsync <ArgumentNullException>("manager", () => validator.ValidateAsync(null, null, "foo"));
        }