Skip to content

Commit

Permalink
when renewing server-side session, create new entry if current sessio…
Browse files Browse the repository at this point in the history
…n not found
  • Loading branch information
brockallen committed Aug 17, 2022
1 parent 15fbba8 commit 13b9e6f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
12 changes: 10 additions & 2 deletions src/Duende.Bff.EntityFramework/Store/UserSessionStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,23 @@ public UserSessionStore(IOptions<DataProtectionOptions> options, ISessionDbConte
}

/// <inheritdoc/>
public Task CreateUserSessionAsync(UserSession session, CancellationToken cancellationToken)
public async Task CreateUserSessionAsync(UserSession session, CancellationToken cancellationToken)
{
var item = new UserSessionEntity()
{
ApplicationName = _applicationDiscriminator
};
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);
}
}

/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -67,8 +74,6 @@ await _store.DeleteUserSessionsAsync(new UserSessionsFilter
};

await _store.CreateUserSessionAsync(session);

return key;
}

/// <inheritdoc />
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit 13b9e6f

Please sign in to comment.