-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
111 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/DkpWeb.Blazor/Services/AustinAntiforgeryStateProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
// Based on https://github.com/dotnet/aspnetcore/blob/8ad057426fa6a27cd648b05684afddab9d97d3d9/src/Components/Shared/src/DefaultAntiforgeryStateProvider.cs | ||
|
||
using Microsoft.AspNetCore.Components; | ||
using Microsoft.AspNetCore.Components.Forms; | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace DkpWeb.Blazor.Services | ||
{ | ||
public class AustinAntiforgeryStateProvider : AntiforgeryStateProvider, IDisposable | ||
{ | ||
private const string PersistenceKey = $"__austin__{nameof(AntiforgeryRequestToken)}"; | ||
private readonly PersistingComponentStateSubscription _subscription; | ||
private readonly AntiforgeryRequestToken? _currentToken; | ||
|
||
[UnconditionalSuppressMessage( | ||
"Trimming", | ||
"IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", | ||
Justification = $"{nameof(AustinAntiforgeryStateProvider)} uses the {nameof(PersistentComponentState)} APIs to deserialize the token, which are already annotated.")] | ||
public AustinAntiforgeryStateProvider(PersistentComponentState state) | ||
{ | ||
// Automatically flow the Request token to server/wasm through | ||
// persistent component state. This guarantees that the antiforgery | ||
// token is available on the interactive components, even when they | ||
// don't have access to the request. | ||
_subscription = state.RegisterOnPersisting(() => | ||
{ | ||
state.PersistAsJson(PersistenceKey, GetAntiforgeryToken()); | ||
return Task.CompletedTask; | ||
}); | ||
|
||
state.TryTakeFromJson(PersistenceKey, out _currentToken); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override AntiforgeryRequestToken? GetAntiforgeryToken() => _currentToken; | ||
|
||
/// <inheritdoc /> | ||
public void Dispose() => _subscription.Dispose(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/DkpWeb/Services/AustinEndpointAntiforgeryStateProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
// Based on https://github.com/dotnet/aspnetcore/blob/8ad057426fa6a27cd648b05684afddab9d97d3d9/src/Components/Endpoints/src/Forms/EndpointAntiforgeryStateProvider.cs | ||
|
||
using DkpWeb.Blazor.Services; | ||
using Microsoft.AspNetCore.Antiforgery; | ||
using Microsoft.AspNetCore.Components; | ||
using Microsoft.AspNetCore.Components.Forms; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace DkpWeb.Services | ||
{ | ||
public class AustinEndpointAntiforgeryStateProvider : AustinAntiforgeryStateProvider | ||
{ | ||
private readonly IAntiforgery antiforgery; | ||
private readonly IHttpContextAccessor accessor; | ||
|
||
public AustinEndpointAntiforgeryStateProvider(IAntiforgery antiforgery, PersistentComponentState state, IHttpContextAccessor accessor) | ||
: base(state) | ||
{ | ||
this.antiforgery = antiforgery; | ||
this.accessor = accessor; | ||
} | ||
|
||
public override AntiforgeryRequestToken GetAntiforgeryToken() | ||
{ | ||
var context = accessor.HttpContext; | ||
if (context == null) | ||
{ | ||
return null; | ||
} | ||
|
||
// We already have a callback setup to generate the token when the response starts if needed. | ||
// If we need the tokens before we start streaming the response, we'll generate and store them; | ||
// otherwise we'll just retrieve them. | ||
// In case there are no tokens available, we are going to return null and no-op. | ||
var tokens = !context.Response.HasStarted ? antiforgery.GetAndStoreTokens(context) : antiforgery.GetTokens(context); | ||
if (tokens.RequestToken is null) | ||
{ | ||
return null; | ||
} | ||
|
||
return new AntiforgeryRequestToken(tokens.RequestToken, tokens.FormFieldName); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters