(24/30) Everyone learn Blazor together: <AuthorizeRouteView>Use

(24/30) Everyone learn Blazor together: <AuthorizeRouteView>Use

The day before yesterday, I added verified and failed display content to 'Blog.razor'. However, if the system wants to present the same content that failed to verify, it would be a waste of time to write it in every Component. You can use 'App.razor' to help us do unified presentation.

最后更新 12/23/2021 10:51 PM
StrayaWorker
预计阅读 2 分钟
分类
Blazor
专题
Learn the Blazor series together
标签
.NET C# ASP.NET Core Blazor

前天有在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. Then start the website again. If you don't verify it, you will see the same content.

目前只要通过验证的用户都能看到相同 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 **

Keep Exploring

延伸阅读

更多文章
同分类 / 同标签 12/25/2021

(29/30) Everyone learn Blazor together: Blazor unit testing

Probably the most boring process of developing a system is to solve bugs, especially the error of trying to value null objects (`Object reference not set to an instance of an object.`). This should be the most common problem that most people encounter when they first step into the programming field. In order to relieve themselves from the boring process of solving bugs, this article introduces 'unit testing'.

继续阅读
同分类 / 同标签 12/25/2021

(28/30) Everyone learns Blazor together: Policy-based authorization

It was mentioned before that 'ASP.NET Core Identity' uses 'Claim' based authentication. In fact,'ASP.NET Core Identity' has different types of authorization methods, the simplest are 'login authorization','role authorization', and 'Claim authorization', but all of the above are implemented in one way: 'Policy-based authorization'.

继续阅读