question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Blazor Server: don't bind event listener for non-rendered element

See original GitHub issue

I’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:closed
  • Created 3 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
pranavkmcommented, Nov 12, 2020

@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.

0reactions
javiercncommented, Nov 20, 2020

Closing this issue since the original question was answered.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found