(21/30) Everyone learns Blazor together: ASP.NET Core Identity(1)

(21/30) Everyone learns Blazor together: ASP.NET Core Identity(1)

Today's part of implementing authentication

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

今天来实现身分验证的部分,笔者此前是用ASP.NET Core Web API 搭配Blazor,用户第一次成功登录时,在后端将RoleClaim 等权限储存在JWT,将JWT 存在浏览器的LocalStorage 里面,前端再自己重写AuthenticationStateProvider,去检查LocalStorageJWT,接着将AuthenticationState 当作CascadingParameter 层层传递到各Component,这样就不需要不停跟后端交换数据,这是个很宝贵的经验,让笔者对身分验证有深入了解,这次笔者试试看ASP.NET Core 自己的Identity

首先去 NeGet 下载 3 个组件,分别为Microsoft.AspNetCore.Identity.EntityFrameworkCoreMicrosoft.AspNetCore.Identity.UIMicrosoft.VisualStudio.Web.CodeGeneration.Design,第 1 个是Identity 必备组件,如果想自己实现JWT 的话,只需要下载第一个组件,再下载JWT 相关组件(Microsoft.AspNetCore.Authentication.JwtBearer)即可,后面 2 个都是让ASP.NET Core Identity帮我们生成预置Identity 页面的组件。

接着去BlazorServer.ModelsAppDbContext,将继承的DbContext改为IdentityDbContext,代表接下来用的DBIdentity有关系。

BlazorServer 项目按右键,选择「添加」,选择「新搭建基架的项目」,切换到「标识」页签,选择标识

勾选「替代所有文件」,数据上下文类选择AppDbContext,要注意的是,如果刚才没将继承的类别改成IdentityDbContext,就不会有AppDbContext可以选,必须点右边的「+」符号自己新增一个IdentityDbContext

这时候有可能遇到这种「FileUpload.OnInitializedAsync(): 没有找到适合的方法来重写」的错误信息,这通常是Visual Studio 的问题,先将这里注释,重复一次上一段的做法就可以。

接着去Program.cs加上身分验证的服务,Identity 预置将验证信息存在 Cookie。

接着在程序包管理器控制台执行两段命令,Add-Migration IdentitySupport新增MigrationUpdate-Database更新DB,去看数据库,可以看到多了6 张表,其中最常用到的就是AspNetUsersAspNetRolesAspNetUserRoles,如果以Claim 处理权限的话,就会用到AspNetUserClaims

项目则多了一个Areas 文件夹,里面就是ASP.NET Core Identity 的实现,包括了登录系统账号系统管理系统

我们去NavMenu.razor加上通往LoginNavLink,在相对路径中AreasPages 可以省略。

<div class="nav-item px-3">
    <NavLink class="nav-link" href="Identity/Account/Login" Match="NavLinkMatch.All">
        <span class="bi bi-file-earmark-lock2 h4 p-2 mb-0" aria-hidden="true"></span> Login
    </NavLink>
</div>

After launching the website, go to the Login page from Nav on the left. You can see that there is already a complete login system, including functions such as registration, login, forgetting password, etc. There are even rules for registering passwords. We register an account according to the rules, and the database also generated the newly registered account.

注册界面填写正确的邮箱和密码,AspNetUsers表即可查看注册的用户信息:

** Quotes **

  1. Claims-based authorization in ASP.NET Core
  2. Claim type and claim value in claims policy based authorization in asp net core
  3. ASP NET Core Identity tutorial from scratch
  4. Unable to resolve service for type IEmailSender while attempting to activate RegisterModel

** 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'.

继续阅读