目录
- 建立默认带身份验证 Blazor 程序
- 角色/组件/特性/过程逻辑
- DB 改 Sqlite
- 将自定义字段添加到用户表
- 脚手架拉取IDS文件,本地化资源
- freesql 生成实体类,freesql 管理ids数据表
- 初始化 Roles,freesql 外键 => 导航属性
- 完善 freesql 和 bb 特性
本节源码
https://github.com/densen2014/Blazor100/tree/Blazor-教程15-4/b15blazorIDS2
注:源码工程目录改为b16blazorIDS2,区分之前的教程例子
给默认 IdentityUser 类添加新字段
新建 Model
文件夹, 新建 WebAppIdentityUser.cs
文件
继承 IdentityUser 类,并添加一些附加字段.
using Microsoft.AspNetCore.Identity;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace b16blazorIDS2.Models
{
public class WebAppIdentityUser : IdentityUser
{
/// <summary>
/// Full name
/// </summary>
[PersonalData]
public string? Name { get; set; }
/// <summary>
/// Birth Date
/// </summary>
[PersonalData]
public DateTime? DOB { get; set; }
[Display(Name = "识别码")]
public string? UUID { get; set; }
[Display(Name = "外联")]
public string? provider { get; set; }
[Display(Name = "税号")]
[PersonalData]
public string? TaxNumber { get; set; }
[Display(Name = "街道地址")]
[PersonalData]
public string? Street { get; set; }
[Display(Name = "邮政编码")]
[PersonalData]
public string? Zip { get; set; }
[Display(Name = "区县")]
[PersonalData]
public string? County { get; set; }
[Display(Name = "城市")]
[PersonalData]
public string? City { get; set; }
[Display(Name = "省份")]
[PersonalData]
public string? Province { get; set; }
[Display(Name = "国家")]
[PersonalData]
public string? Country { get; set; }
[Display(Name = "类型")]
[PersonalData]
public string? UserRole { get; set; }
}
}
更改上下文
编辑 Data\ApplicationDbContext.cs
using b16blazorIDS2.Models;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
namespace b16blazorIDS2.Data
{
public class ApplicationDbContext : IdentityDbContext<WebAppIdentityUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
}
}
更改 Identity 依赖为新的类
编辑Program.cs
//注入Identity依赖 WebAppIdentityUser => WebAppIdentityUser
builder.Services.AddDefaultIdentity<WebAppIdentityUser>(o =>
...
builder.Services.AddScoped<AuthenticationStateProvider, RevalidatingIdentityAuthenticationStateProvider<WebAppIdentityUser>>();
...
把项目其他的 Identity 也替换为新的 WebAppIdentityUser 类
记得要添加 @using b16blazorIDS2.Models
_LoginPartial.cshtml
文件
@using Microsoft.AspNetCore.Identity
@using b16blazorIDS2.Models
@inject SignInManager<WebAppIdentityUser> SignInManager
@inject UserManager<WebAppIdentityUser> UserManager
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
...
LogOut.cshtml
文件
@using b16blazorIDS2.Models
@attribute [IgnoreAntiforgeryToken]
@inject SignInManager<WebAppIdentityUser> SignInManager
...
运行工程
如果出错,进入Packge Manager Console中输入
dotnet ef database update
本节源码
https://github.com/densen2014/Blazor100/tree/Blazor-教程15-4/b15blazorIDS2
源代码
https://github.com/densen2014/Blazor100
https://gitee.com/densen2014/Blazor100 (镜像/非最新版)
标签:set,string,自定义,get,身份验证,using,100,public,Name From: https://www.cnblogs.com/densen2014/p/17083937.html