private async Task LogResultAsync(
            UserCredentialsAuthenticationResult result,
            AuthenticateUserCredentialsQuery query,
            string uniqueUsername,
            IExecutionContext executionContext
            )
        {
            ICommand command;

            if (result.IsSuccess)
            {
                command = new LogSuccessfulAuthenticationCommand()
                {
                    UserId = result.User.UserId
                };
            }
            else
            {
                command = new LogFailedAuthenticationAttemptCommand(query.UserAreaCode, uniqueUsername);
            }

            await _domainRepository
            .WithContext(executionContext)
            .ExecuteCommandAsync(command);
        }
コード例 #2
0
        public async Task CanClearStaleAuthenticationData()
        {
            var uniqueData = UNIQUE_PREFIX + "CCStaleAuth";
            var seedDate   = new DateTime(1994, 8, 2, 0, 0, 0, DateTimeKind.Utc);

            using var app = _appFactory.Create();
            var contentRepository = app.Services.GetContentRepositoryWithElevatedPermissions();
            var dbContext         = app.Services.GetRequiredService <CofoundryDbContext>();

            var userId = await app.TestData.Users().AddAsync(uniqueData);

            var logCommand = new LogSuccessfulAuthenticationCommand()
            {
                UserId = userId
            };

            app.Mocks.MockDateTime(seedDate);
            await contentRepository.ExecuteCommandAsync(logCommand);

            app.Mocks.MockDateTime(seedDate.AddDays(50));
            await contentRepository.ExecuteCommandAsync(logCommand);

            app.Mocks.MockDateTime(seedDate.AddDays(70));
            await contentRepository
            .ExecuteCommandAsync(new CleanupUsersCommand()
            {
                UserAreaCode           = TestUserArea1.Code,
                DefaultRetentionPeriod = TimeSpan.FromDays(30)
            });

            var logs = await dbContext
                       .UserAuthenticationLogs
                       .AsNoTracking()
                       .Where(u => u.UserId == userId)
                       .ToListAsync();

            using (new AssertionScope())
            {
                logs.Should().HaveCount(1);
                logs.Single().CreateDate.Should().Be(seedDate.AddDays(50));
            }
        }