第6章:ASP.NET Core MVC

学习分享 丨作者 / 郑 子 铭 丨公众号 / DotNet NB / CloudNative NB

任务40:介绍

  • 1.Individual authentication 模板

  • 2.EF Core Migration

  • 3.Identity MVC:UI

  • 4.Identity MVC: EF + Identity实现

  • 5.Identity MVC:注册逻辑实现

  • 6.Identity MVC:登录逻辑实现

  • 7.Identity MVC:ReturnUrl实现

  • 8.Identity MVC:Model后端验证

  • 9.Identity MVC:Model前端验证

  • 10.Identity MVC:DbContextSeed初始化

任务41:Individual authentication 模板

dotnet new mvc --help
Options:
  -au|--auth                      The type of authentication to use
                                      None             - No authentication
                                      Individual       - Individual authentication
                                      IndividualB2C    - Individual authentication with Azure AD B2C
                                      SingleOrg        - Organizational authentication for a single tenant
                                      MultiOrg         - Organizational authentication for multiple tenants
                                      Windows          - Windows authentication
                                  Default: None

解决VScode终端乱码

默认创建localdb,Identity

appsettings.json

Startup.cs

初始化数据库,根据Data/Migrations文件夹下的数据库文件创建更新数据库

报错:

在stackoverflow找到解决方法:

https://stackoverflow.com/questions/45091909/dotnet-ef-database-update-no-executable-found-matching-command-dotnet-ef?r=SearchResultsarrow-up-right

在csproj文件的ItemGroup中添加引用

访问https://localhost:5001

点击Register进入注册页面

输入邮箱密码登陆

登陆成功

点击邮箱进入Manage your account

通过SSMS连接localdb

获取实例管道名称

解决PowerShell中文乱码问题,勾选UTF-8

通过实例管道名称连接localdb

任务42:EF Core Migration

数据库新增

添加列之前

在Models文件夹下新增ApplicationUser.cs

自动生成文件

执行成功后刷新数据库,可以看到数据库中多了一列NewColumn

在ApplicationUser.cs中新增Address

执行成功后刷新数据库,可以看到数据库中多了一列Address

数据库回滚

执行成功后刷新数据库,可以看到数据库中Address不见了

执行成功后移除AddAddress.cs以及AddAddress.Designer.cs文件

生成sql脚本命令

拷贝出来后可在数据库执行

任务43:Identity MVC:UI

以MvcCookieAuthSample项目为基础,通过ef core以及Identity实现注册登陆UI整个过程

AccountController.cs新增Register,Login

在Views文件夹下新建Account文件夹,在Account文件夹下新增Register.cshtml以及Login.cshtml

Register.cshtml

Login.cshtml

新建ViewModels文件夹,在ViewModels文件夹下新建RegisterViewModel.cs

RegisterViewModel.cs

在Views/Shared目录下的_Layout.cshtml中增加Register以及Login

点击进入Register以及Login页面

任务44:Identity MVC: EF + Identity实现

在Models文件夹新增ApplicationUser.cs以及ApplicationUserRole.cs

ApplicationUser.cs

ApplicationUserRole.cs

新建Data文件夹,在Data文件夹下新建ApplicationDbContext.cs

在appsettings.json中添加ConnectionStrings

Startup.cs添加以下内容

AccountController.cs添加以下内容

添加nuget包:Microsoft.EntityFrameworkCore.Tools

VSCode报错:Versioning information could not be retrieved from the NuGet package repository. Please try again later.

使用Visual Studio添加nuget包

报错:There is already an object named 'AspNetRoles' in the database.

删除之前的数据库实例

主键为int

点击Register,成功跳回主页

在数据库中查看数据

任务45:Identity MVC:注册逻辑实现

AccountController.cs

启动项目,重新注册一个

看到Cookie,登陆成功

修改Views/Shared文件夹下的_Layout.cshtml

启动项目

任务46:Identity MVC:登录逻辑实现

AccountController.cs

_Layout.cshtml

Views/Account文件夹中的Login.cshtml

启动项目

点击Log out,回到主页

点击Login

登陆成功

换另一个邮箱,登陆成功

任务47:Identity MVC:ReturnUrl实现

AccountController.cs

Register.cshtml

Login.cshtml

启动项目,访问:https://localhost:44387/adminarrow-up-right

点击Log out,再次访问:https://localhost:44387/admin,跳转到登陆界面arrow-up-right

登陆之后直接到admin页面

任务48:Identity MVC:Model后端验证

RegisterViewModel.cs

在ViewModels文件夹下新增LoginViewModel.cs

AccountController.cs第一个参数类型由RegisterViewModel修改为LoginViewModel

Login.cshtml

启动项目,不输入邮箱密码直接点击登陆

Register.cshtml

AccountController.cs

启动项目,直接点击注册

Startup.cs

Register.cshtml添加text-danger

AccountController.cs

启动项目,随便输入密码123

点击注册

任务49:Identity MVC:Model前端验证

将Shared文件夹中的_ValidationScriptsPartial.cshtml的jquery.validate组件添加到Login.cshtml最下面以及Register.cshtml最下面

启动项目,直接点击登陆,注册,不会产生网络请求

任务50:Identity MVC:DbContextSeed初始化

启动的时候判断是否第一次执行,如果第一次执行则添加一个记录,比如用户账号第一次进来为管理员

在Data文件夹新增ApplicationDbContextSeed.cs

在Data文件夹新增扩展方法调用ApplicationDbContextSeed

WebHostMigrationExtensions.cs

Program.cs

删除数据库

控制台方式启动项目,先进行数据库初始化,再启动WebHost

数据库自动插入数据

输入邮箱,密码:Password$123

登陆

end

Last updated