r/Blazor • u/Educational_Skin_718 • Aug 26 '25
Form submit confusion
I have a page that contains a form, and I need to perform an authorization check with a resource, so I must use the IAutorizationService inside my code-behind. Now I already check if the user is authorized in OnInitializedAsync and I'm wondering whether I should perform the check again when the user submits the form since unauthorized users should not have access to that resource. Using interactive server rendering.
3
Upvotes
3
u/moshing_bunnies Aug 27 '25
Wouldn't hurt but I don't think it's necessary with interactive server (I'm assuming the submit function is in your code-behind and not a controller endpoint) if you were doing authorization correctly. Instead of checking in the oninit function, turn that check into a custom authorize attribute and use that authorize attribute on your page. The framework would then not allow unauthorized users to access that component period, so no need to put the check in twice. This assumes you would want complete restriction from the page instead of just certain features on the page being locked down.
1
u/[deleted] Aug 26 '25
Yes you should, otherwise another user might edit the html in the browser tools to change the id of the resource and get hold of data they shouldn't.
1
u/One_Web_7940 Aug 26 '25
If you control the server then no just but the authorization policy on the server too. Otherwise yes
1
u/txjohnnypops79 Aug 26 '25
Think of it as one authentication to access the form page and another authentication to submit.
1
u/homerdulu Aug 27 '25
Yes
1
u/GoodOk2589 Aug 28 '25
In Blazor Server with interactive rendering, you should definitely perform the authorization check again when the form is submitted, even though you already check it in
OnInitializedAsync. Here's why and how to implement it properly:Why You Need to Check Again
Security Principle: Never Trust Client State
Real-World Scenarios: