Example #1
0
        public async Task GetUserSessionsAsync_for_valid_sub_should_succeed()
        {
            await _subject.CreateUserSessionAsync(new UserSession
            {
                Key       = Guid.NewGuid().ToString(),
                Ticket    = "ticket",
                SubjectId = "sub1",
                SessionId = "sid1_1",
            });

            await _subject.CreateUserSessionAsync(new UserSession
            {
                Key       = Guid.NewGuid().ToString(),
                Ticket    = "ticket",
                SubjectId = "sub1",
                SessionId = "sid1_2",
            });

            await _subject.CreateUserSessionAsync(new UserSession
            {
                Key       = Guid.NewGuid().ToString(),
                Ticket    = "ticket",
                SubjectId = "sub2",
                SessionId = "sid2_1",
            });

            await _subject.CreateUserSessionAsync(new UserSession
            {
                Key       = Guid.NewGuid().ToString(),
                Ticket    = "ticket",
                SubjectId = "sub2",
                SessionId = "sid2_2",
            });

            await _subject.CreateUserSessionAsync(new UserSession
            {
                Key       = Guid.NewGuid().ToString(),
                Ticket    = "ticket",
                SubjectId = "sub2",
                SessionId = "sid2_3",
            });

            await _subject.CreateUserSessionAsync(new UserSession
            {
                Key       = Guid.NewGuid().ToString(),
                Ticket    = "ticket",
                SubjectId = "sub3",
                SessionId = "sid3_1",
            });

            var items = await _subject.GetUserSessionsAsync(new UserSessionsFilter { SubjectId = "sub2" });

            items.Count().Should().Be(3);
            items.Select(x => x.SubjectId).Distinct().Should().BeEquivalentTo(new[] { "sub2" });
            items.Select(x => x.SessionId).Should().BeEquivalentTo(new[] { "sid2_1", "sid2_2", "sid2_3", });
        }
Example #2
0
        /// <inheritdoc />
        public async Task <IReadOnlyCollection <AuthenticationTicket> > GetUserTicketsAsync(UserSessionsFilter filter, CancellationToken cancellationToken)
        {
            var list = new List <AuthenticationTicket>();

            var sessions = await _store.GetUserSessionsAsync(filter, cancellationToken);

            foreach (var session in sessions)
            {
                var ticket = session.Deserialize(_protector, _logger);
                if (ticket != null)
                {
                    list.Add(ticket);
                }
                else
                {
                    // if we failed to get a ticket, then remove DB record
                    _logger.LogWarning("Failed to deserialize authentication ticket from store, deleting record for key {key}", session.Key);
                    await RemoveAsync(session.Key);
                }
            }

            return(list);
        }