Cookie fix
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Authentication.Cookies;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
|
||||
namespace maintenance.Authentication.Core;
|
||||
|
||||
public sealed class MemoryCacheTicketStore : ITicketStore
|
||||
{
|
||||
private const string KeyPrefix = "auth-ticket-";
|
||||
private readonly IMemoryCache _cache;
|
||||
private readonly TimeSpan _fallbackLifetime = TimeSpan.FromHours(8);
|
||||
|
||||
public MemoryCacheTicketStore(IMemoryCache cache)
|
||||
{
|
||||
_cache = cache;
|
||||
}
|
||||
|
||||
public async Task<string> StoreAsync(AuthenticationTicket ticket)
|
||||
{
|
||||
var key = $"{KeyPrefix}{Guid.NewGuid():N}";
|
||||
await RenewAsync(key, ticket);
|
||||
return key;
|
||||
}
|
||||
|
||||
public Task RenewAsync(string key, AuthenticationTicket ticket)
|
||||
{
|
||||
var expiration = ticket.Properties.ExpiresUtc?.UtcDateTime ?? DateTime.UtcNow.Add(_fallbackLifetime);
|
||||
_cache.Set(key, ticket, expiration);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task<AuthenticationTicket?> RetrieveAsync(string key)
|
||||
{
|
||||
_cache.TryGetValue(key, out AuthenticationTicket? ticket);
|
||||
return Task.FromResult(ticket);
|
||||
}
|
||||
|
||||
public Task RemoveAsync(string key)
|
||||
{
|
||||
_cache.Remove(key);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -49,7 +49,7 @@ public sealed class OidcAuthProvider : IAuthProvider
|
||||
options.CallbackPath = callbackPath;
|
||||
options.SignedOutCallbackPath = signedOutCallbackPath;
|
||||
options.ResponseType = "code";
|
||||
options.SaveTokens = true;
|
||||
options.SaveTokens = false;
|
||||
options.GetClaimsFromUserInfoEndpoint = true;
|
||||
|
||||
options.Scope.Clear();
|
||||
|
||||
@@ -41,6 +41,8 @@ builder.Services.AddDbContext<AppDbContext>(options =>
|
||||
{
|
||||
options.UseSqlServer(primaryDbConnection);
|
||||
});
|
||||
builder.Services.AddMemoryCache();
|
||||
builder.Services.AddSingleton<ITicketStore, MemoryCacheTicketStore>();
|
||||
|
||||
if (adminGroupSource.Equals(AdminGroupSources.Ad, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
@@ -86,6 +88,12 @@ authBuilder.AddCookie(options =>
|
||||
options.Cookie.Name = "errorpages.auth";
|
||||
options.AccessDeniedPath = "/AccessDenied";
|
||||
});
|
||||
builder.Services
|
||||
.AddOptions<CookieAuthenticationOptions>(CookieAuthenticationDefaults.AuthenticationScheme)
|
||||
.Configure<ITicketStore>((options, ticketStore) =>
|
||||
{
|
||||
options.SessionStore = ticketStore;
|
||||
});
|
||||
|
||||
var providers = new IAuthProvider[]
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user