前天有在Blog.razor加入通过验证及未通过的显示内容,但如果系统要呈现的未通过验证内容都一样,在每个 Component 都这样写就太浪费时间了,可以利用App.razor帮我们做统一呈现的事。
先把Blog.razor原本包覆内容的<AuthorizeView>删除,剪切<NotAuthorized>的部分,再加上@attribute [Authorize]这行,告知Blazor 这个 Component 需要验证。

Then change the original single tag of App.razor to paired tags, and post the one you just cut

目前只要通过验证的用户都能看到相同 Component,不过大部分系统通常都会有角色或是权限划分,以区分不同用户,明天就来尝试用Role 区分授权。
** Note: The code in this article is refactored through. NET 6 + Visual Studio 2022. You can click on the original link to compare and learn the refactored code. Thank you for reading and support the original author **