Browse Source

Checking dynamic claims at intervals rather than every time.

Related to https://github.com/abpframework/abp/pull/19579 and https://github.com/abpframework/abp/pull/19605
pull/19742/head
maliming 2 years ago
parent
commit
658ae6e48f
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 10
      framework/src/Volo.Abp.AspNetCore.SignalR/Volo/Abp/AspNetCore/SignalR/AbpSignalROptions.cs
  2. 16
      framework/src/Volo.Abp.AspNetCore.SignalR/Volo/Abp/AspNetCore/SignalR/Authentication/AbpAuthenticationHubFilter.cs

10
framework/src/Volo.Abp.AspNetCore.SignalR/Volo/Abp/AspNetCore/SignalR/AbpSignalROptions.cs

@ -1,11 +1,19 @@
namespace Volo.Abp.AspNetCore.SignalR;
using System;
namespace Volo.Abp.AspNetCore.SignalR;
public class AbpSignalROptions
{
public HubConfigList Hubs { get; }
/// <summary>
/// Default: 5 seconds.
/// </summary>
public TimeSpan? CheckDynamicClaimsInterval { get; set; }
public AbpSignalROptions()
{
Hubs = new HubConfigList();
CheckDynamicClaimsInterval = TimeSpan.FromSeconds(5);
}
}

16
framework/src/Volo.Abp.AspNetCore.SignalR/Volo/Abp/AspNetCore/SignalR/Authentication/AbpAuthenticationHubFilter.cs

@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Volo.Abp.Security.Claims;
@ -49,6 +51,20 @@ public class AbpAuthenticationHubFilter : IHubFilter
claimsPrincipal.Identity.IsAuthenticated &&
serviceProvider.GetRequiredService<IOptions<AbpClaimsPrincipalFactoryOptions>>().Value.IsDynamicClaimsEnabled)
{
var checkDynamicClaimsInterval = serviceProvider.GetRequiredService<IOptions<AbpSignalROptions>>().Value.CheckDynamicClaimsInterval;
if (checkDynamicClaimsInterval.HasValue &&
hubCallerContext.Items.TryGetValue(nameof(HandleDynamicClaimsPrincipalAsync), out var lastCheckDynamicClaimsTime) &&
lastCheckDynamicClaimsTime is DateTime lastCheckDynamicClaimsTimeValue)
{
if (DateTime.UtcNow.Subtract(lastCheckDynamicClaimsTimeValue) < checkDynamicClaimsInterval.Value)
{
// Dynamic claims are not checked because the interval has not passed yet.
return;
}
}
hubCallerContext.Items[nameof(HandleDynamicClaimsPrincipalAsync)] = DateTime.UtcNow;
claimsPrincipal = claimsPrincipal.Identity is ClaimsIdentity identity
? new ClaimsPrincipal(new ClaimsIdentity(claimsPrincipal.Claims, claimsPrincipal.Identity.AuthenticationType, identity.NameClaimType, identity.RoleClaimType))
: new ClaimsPrincipal(new ClaimsIdentity(claimsPrincipal.Claims, claimsPrincipal.Identity.AuthenticationType));

Loading…
Cancel
Save