Using distributed cache with Duende AccessTokenManagement results in endless loop #254
-
|
I upgraded my Blazor Serverside app from NET5 to NET8 and I am now using So far everything works fine when using the same In my app I use distributed SQL Server cache and I implement a custom public class UserTokenStore(
IDistributedCache cache) : IUserTokenStore
{
private const string KeyPrefix = "UserTokenStore-";
public Task<UserToken> GetTokenAsync(ClaimsPrincipal user, UserTokenRequestParameters? parameters = null)
{
var sub = user.FindFirst("sub")?.Value ?? throw new InvalidOperationException("No sub claim");
if (cache.TryGetValue(sub, out UserToken value))
{
return Task.FromResult(value);
}
return Task.FromResult(new UserToken { Error = "Not found" });
}
public Task StoreTokenAsync(ClaimsPrincipal user, UserToken token, UserTokenRequestParameters? parameters = null)
{
var sub = user.FindFirst("sub")?.Value ?? throw new InvalidOperationException("No sub claim");
cache.Set($"{KeyPrefix}{sub}", token, new()
{
AbsoluteExpiration = token.Expiration,
//SlidingExpiration = TimeSpan.FromMinutes(60)
});
return Task.CompletedTask;
}
public Task ClearTokenAsync(ClaimsPrincipal user, UserTokenRequestParameters? parameters = null)
{
var sub = user.FindFirst("sub")?.Value ?? throw new InvalidOperationException("No sub claim");
cache.Remove($"{KeyPrefix}{sub}");
return Task.CompletedTask;
}
}So now I am using distributed cache instead of the However, once I did this, the app enters into an endless loop navigating between the OIDC providers connect/authorize URL and the app's return URL. Is there a reason why this is happening? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
|
I'm assuming you've based your solution on this sample code: https://github.com/DuendeSoftware/foss/tree/atm-3.2.0/access-token-management/samples/BlazorServer Did you also implement the custom |
Beta Was this translation helpful? Give feedback.
-
|
Problem solved. I was mixing cache keys. |
Beta Was this translation helpful? Give feedback.
Ah yes, now I spotted
if (cache.TryGetValue(sub, out UserToken value))rather thanif (cache.TryGetValue($"{KeyPrefix}{sub}", out UserToken value))! Glad you got it resolved :)