Blazor Server: don't bind event listener for non-rendered element
See original GitHub issueI’m interested in keeping authorization simple by using the AuthorizeView
component. This request is an elaboration of the documentation issue: dotnet/AspNetCore.Docs/issues/20462.
In short, say I have Blazor pages, each of which requires authentication and role-based authorization:
@page "/page1"
@attribute [Authorize]
<AuthorizeView Role="page1">
<!-- SECURE CONTENT -->
<button @onclick="SecureAction">Secure Action</button>
</AuthorizeView>
@code {
private void SecureAction()
{
// Unauthorized users should not be able to execute this code path
}
}
It would be nice if it was impossible for clients to manually trigger the SecureAction
method. In this case, because the only element to which it binds is not rendered, no SignalR event listener should be bound.
I would prefer not to inject the AuthenticationStateProvider
in every page and guard every protected task/method with it.
re: @guardrex
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (6 by maintainers)
Top Results From Across the Web
Javascript click eventlistener not working in blazor
I'm reasonably certain its all linking together properly as the stylesheets are getting applied properly, and the javascript is in the same ...
Read more >ASP.NET Core Blazor event handling
Delegate event handlers automatically trigger a UI render, so there's no need to manually call StateHasChanged . Exceptions are logged.
Read more >Event Handling In Blazor
The delegate event handler of Blazor has automatically triggered a UI render event, so there is no need to manually call StateHasChanged ...
Read more >How to Pass Arguments to Your onclick Functions in Blazor
Wiring event handlers to HTML events (onClick) in your Blazor components is easy, but how can you pass additional information and different ...
Read more >Blazor onclick not working. #10725 Closed rxelizondo opened thi
Binding the OnClick Event Using Blazor and C In this example, we'll use Blazor, ... Blazor OnClick is not working inside element with...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@shanemikel by default event handlers for non-rendered elements are unable to be invoked. The scenario @javiercn was talking of was that if your authorization status changes after an element is rendered, there is a small window (in the order of milliseconds) before a new render that event handlers could be triggered. (
[Initially authorized] --> [Unauthorized / StateHasChanged] … You can continue to trigger the event now … --> [Render](Now it’s no longer possible to trigger)
If you were concerned about the small window, you could consider using the pattern @javiercn suggested. Most users would not need to worry about this.
@guardrex I don’t think this needs to be documented.
Closing this issue since the original question was answered.