Merge pull request #3928 from sbwalker/dev

improvements to IdentityRevalidatingAuthenticationStateProvider
This commit is contained in:
Shaun Walker 2024-03-01 12:16:39 -05:00 committed by GitHub
commit d723ef0a13
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 28 additions and 34 deletions

View File

@ -157,23 +157,23 @@
editmode = true; // querystring can set edit mode
}
// get user
if (PageState == null || PageState.Refresh || refresh || PageState.Alias.SiteId != SiteState.Alias.SiteId)
// verify user is authenticated for current site
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
if (authState.User.Identity.IsAuthenticated && authState.User.Claims.Any(item => item.Type == "sitekey" && item.Value == SiteState.Alias.SiteKey))
{
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
// verify user is authenticated for current site
if (authState.User.Identity.IsAuthenticated && authState.User.Claims.Any(item => item.Type == "sitekey" && item.Value == SiteState.Alias.SiteKey))
if (PageState == null || PageState.Refresh || refresh || PageState.Alias.SiteId != SiteState.Alias.SiteId)
{
// get user
user = await UserService.GetUserAsync(authState.User.Identity.Name, SiteState.Alias.SiteId);
if (user != null)
{
user.IsAuthenticated = authState.User.Identity.IsAuthenticated;
}
}
}
else
{
user = PageState.User;
else
{
user = PageState.User;
}
}
// process any sync events

View File

@ -4,49 +4,43 @@ using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.Security.Claims;
using System.Threading.Tasks;
using System.Threading;
using System;
using Oqtane.Infrastructure;
using Oqtane.Extensions;
using Oqtane.Managers;
namespace Oqtane.Providers
{
internal sealed class IdentityRevalidatingAuthenticationStateProvider(
ILoggerFactory loggerFactory,
IServiceScopeFactory scopeFactory,
IOptions<IdentityOptions> options)
: RevalidatingServerAuthenticationStateProvider(loggerFactory)
public class IdentityRevalidatingAuthenticationStateProvider(ILoggerFactory loggerFactory, IServiceScopeFactory scopeFactory, IOptions<IdentityOptions> options) : RevalidatingServerAuthenticationStateProvider(loggerFactory)
{
protected override TimeSpan RevalidationInterval => TimeSpan.FromMinutes(30);
// defines how often the authentication state should be asynchronously validated
// note that there is no property within IdentityOptions which allows this to be customized
protected override TimeSpan RevalidationInterval
{
get
{
// suppresses the unused compiler warning for options
var revalidationInterval = TimeSpan.FromMinutes(30); // default Identity value
return (options != null) ? revalidationInterval : revalidationInterval;
}
}
protected override async Task<bool> ValidateAuthenticationStateAsync(AuthenticationState authenticationState, CancellationToken cancellationToken)
protected override async Task<bool> ValidateAuthenticationStateAsync(AuthenticationState authState, CancellationToken cancellationToken)
{
await using var scope = scopeFactory.CreateAsyncScope();
var tenantManager = scope.ServiceProvider.GetRequiredService<ITenantManager>();
tenantManager.SetTenant(authenticationState.User.TenantId());
var userManager = scope.ServiceProvider.GetRequiredService<UserManager<IdentityUser>>();
return await ValidateSecurityStampAsync(userManager, authenticationState.User);
}
private async Task<bool> ValidateSecurityStampAsync(UserManager<IdentityUser> userManager, ClaimsPrincipal principal)
{
var user = await userManager.FindByNameAsync(principal.Identity.Name);
if (user is null)
tenantManager.SetTenant(authState.User.TenantId());
var userManager = scope.ServiceProvider.GetRequiredService<IUserManager>();
var user = userManager.GetUser(authState.User.Identity.Name, authState.User.SiteId());
if (user == null || user.IsDeleted)
{
return false;
}
else if (!userManager.SupportsUserSecurityStamp)
{
return true;
}
else
{
var principalStamp = principal.FindFirstValue(options.Value.ClaimsIdentity.SecurityStampClaimType);
var userStamp = await userManager.GetSecurityStampAsync(user);
//return principalStamp == userStamp; // security stamps need to be persisted in principal - they are stored in AspNetUsers
return true;
return true;
}
}
}