diff --git a/src/Duende.Bff.EntityFramework/Store/UserSessionStore.cs b/src/Duende.Bff.EntityFramework/Store/UserSessionStore.cs index 372fc63a..09ce1554 100644 --- a/src/Duende.Bff.EntityFramework/Store/UserSessionStore.cs +++ b/src/Duende.Bff.EntityFramework/Store/UserSessionStore.cs @@ -38,7 +38,7 @@ public UserSessionStore(IOptions options, ISessionDbConte } /// - public Task CreateUserSessionAsync(UserSession session, CancellationToken cancellationToken) + public async Task CreateUserSessionAsync(UserSession session, CancellationToken cancellationToken) { var item = new UserSessionEntity() { @@ -46,7 +46,15 @@ public Task CreateUserSessionAsync(UserSession session, CancellationToken cancel }; session.CopyTo(item); _sessionDbContext.UserSessions.Add(item); - return _sessionDbContext.SaveChangesAsync(cancellationToken); + + try + { + await _sessionDbContext.SaveChangesAsync(cancellationToken); + } + catch (DbUpdateException ex) + { + _logger.LogWarning("Exception creating new server-side session in database: {error}", ex.Message); + } } /// diff --git a/src/Duende.Bff/SessionManagement/TicketStore/ServerSideTicketStore.cs b/src/Duende.Bff/SessionManagement/TicketStore/ServerSideTicketStore.cs index 795c9fd4..d1a741b6 100644 --- a/src/Duende.Bff/SessionManagement/TicketStore/ServerSideTicketStore.cs +++ b/src/Duende.Bff/SessionManagement/TicketStore/ServerSideTicketStore.cs @@ -52,7 +52,14 @@ await _store.DeleteUserSessionsAsync(new UserSessionsFilter }); var key = CryptoRandom.CreateUniqueId(format: CryptoRandom.OutputFormat.Hex); - + + await CreateNewSessionAsync(key, ticket); + + return key; + } + + private async Task CreateNewSessionAsync(string key, AuthenticationTicket ticket) + { _logger.LogDebug("Creating entry in store for AuthenticationTicket, key {key}, with expiration: {expiration}", key, ticket.GetExpiration()); var session = new UserSession @@ -67,8 +74,6 @@ await _store.DeleteUserSessionsAsync(new UserSessionsFilter }; await _store.CreateUserSessionAsync(session); - - return key; } /// @@ -103,7 +108,9 @@ public async Task RenewAsync(string key, AuthenticationTicket ticket) var session = await _store.GetUserSessionAsync(key); if (session == null) { - throw new InvalidOperationException($"No matching item in store for key `{key}`"); + // https://github.com/dotnet/aspnetcore/issues/41516#issuecomment-1178076544 + await CreateNewSessionAsync(key, ticket); + return; } _logger.LogDebug("Renewing AuthenticationTicket for key {key}, with expiration: {expiration}", key, ticket.GetExpiration());