public async Task CanLogInWithRecoveryCode_WithGlobalAuthorizeFilter()
        {
            // Arrange
            void ConfigureTestServices(IServiceCollection services) =>
            services.SetupGlobalAuthorizeFilter();

            var server = ServerFactory
                         .WithWebHostBuilder(whb => whb.ConfigureServices(ConfigureTestServices));
            var client    = server.CreateClient();
            var newClient = server.CreateClient();

            var userName = $"{Guid.NewGuid()}@example.com";
            var password = $"!Test.Password1$";

            var loggedIn = await UserStories.RegisterNewUserAsync(client, userName, password);

            var showRecoveryCodes = await UserStories.EnableTwoFactorAuthentication(loggedIn);

            var recoveryCode = showRecoveryCodes.Context.RecoveryCodes.First();

            // Act & Assert
            // Use a new client to simulate a new browser session.
            await UserStories.LoginExistingUserRecoveryCodeAsync(newClient, userName, password, recoveryCode);
        }
Пример #2
0
        public async Task CanLogInWithTwoFactorAuthentication_WithGlobalAuthorizeFilter()
        {
            using (StartLog(out var loggerFactory))
            {
                // Arrange
                var server = ServerFactory.CreateServer(loggerFactory, builder =>
                                                        builder.ConfigureServices(services => services.SetupGlobalAuthorizeFilter()));
                var client    = ServerFactory.CreateDefaultClient(server);
                var newClient = ServerFactory.CreateDefaultClient(server);

                var userName = $"{Guid.NewGuid()}@example.com";
                var password = $"!Test.Password1$";

                var loggedIn = await UserStories.RegisterNewUserAsync(client, userName, password);

                var showRecoveryCodes = await UserStories.EnableTwoFactorAuthentication(loggedIn);

                var twoFactorKey = showRecoveryCodes.Context.AuthenticatorKey;

                // Act & Assert
                // Use a new client to simulate a new browser session.
                await UserStories.LoginExistingUser2FaAsync(newClient, userName, password, twoFactorKey);
            }
        }
Пример #3
0
        public async Task CanResetAuthenticator()
        {
            // Arrange
            var principals = new List <ClaimsPrincipal>();

            void ConfigureTestServices(IServiceCollection services) =>
            services
            .SetupTestThirdPartyLogin()
            .SetupGetUserClaimsPrincipal(user => principals.Add(user), IdentityConstants.ApplicationScheme);

            var server = ServerFactory
                         .WithWebHostBuilder(whb => whb.ConfigureTestServices(ConfigureTestServices));

            var client    = server.CreateClient();
            var newClient = server.CreateClient();

            var userName = $"{Guid.NewGuid()}@example.com";
            var password = $"[PLACEHOLDER]-1a";

            // Act
            var loggedIn = await UserStories.RegisterNewUserAsync(client, userName, password);

            var showRecoveryCodes = await UserStories.EnableTwoFactorAuthentication(loggedIn);

            var twoFactorKey = showRecoveryCodes.Context.AuthenticatorKey;

            // Use a new client to simulate a new browser session.
            await UserStories.AcceptCookiePolicy(newClient);

            var index = await UserStories.LoginExistingUser2FaAsync(newClient, userName, password, twoFactorKey);

            await UserStories.ResetAuthenticator(index);

            // RefreshSignIn generates a new security stamp claim
            AssertClaimsNotEqual(principals[1], principals[2], "AspNet.Identity.SecurityStamp");
        }
Пример #4
0
        public async Task CanDownloadPersonalData(bool twoFactor, bool social)
        {
            // Arrange
            void ConfigureTestServices(IServiceCollection services) =>
            services.SetupTestThirdPartyLogin();

            var client = ServerFactory
                         .WithWebHostBuilder(whb => whb.ConfigureTestServices(ConfigureTestServices))
                         .CreateClient();

            ServerFactory.EnsureDatabaseCreated();

            var userName = $"{Guid.NewGuid()}@example.com";
            var guid     = Guid.NewGuid();
            var email    = userName;

            var index = social
                ? await UserStories.RegisterNewUserWithSocialLoginAsync(client, userName, email)
                : await UserStories.RegisterNewUserAsync(client, email, "!TestPassword1");

            if (twoFactor)
            {
                await UserStories.EnableTwoFactorAuthentication(index);
            }

            // Act & Assert
            var jsonData = await UserStories.DownloadPersonalData(index, userName);

            Assert.NotNull(jsonData);
            Assert.True(jsonData.ContainsKey("Id"));
            Assert.NotNull(jsonData["Id"]);
            Assert.True(jsonData.ContainsKey("UserName"));
            Assert.Equal(userName, (string)jsonData["UserName"]);
            Assert.True(jsonData.ContainsKey("Email"));
            Assert.Equal(userName, (string)jsonData["Email"]);
            Assert.True(jsonData.ContainsKey("EmailConfirmed"));
            Assert.False((bool)jsonData["EmailConfirmed"]);
            Assert.True(jsonData.ContainsKey("PhoneNumber"));
            Assert.Equal("null", (string)jsonData["PhoneNumber"]);
            Assert.True(jsonData.ContainsKey("PhoneNumberConfirmed"));
            Assert.False((bool)jsonData["PhoneNumberConfirmed"]);
            Assert.Equal(twoFactor, (bool)jsonData["TwoFactorEnabled"]);

            if (twoFactor)
            {
                Assert.NotNull(jsonData["Authenticator Key"]);
            }
            else
            {
                Assert.Null((string)jsonData["Authenticator Key"]);
            }

            if (social)
            {
                Assert.Equal(userName, (string)jsonData["Contoso external login provider key"]);
            }
            else
            {
                Assert.Null((string)jsonData["Contoso external login provider key"]);
            }
        }