目录

  1. 建立默认带身份验证 Blazor 程序
  2. 角色/组件/特性/过程逻辑
  3. DB 改 Sqlite
  4. 将自定义字段添加到用户表
  5. 脚手架拉取IDS文件,本地化资源
  6. freesql 生成实体类,freesql 管理ids数据表
  7. 初始化 Roles,freesql 外键 => 导航属性
  8. 完善 freesql 和 bb 特性

本节源码

https://github.com/densen2014/Blazor100/tree/Blazor-教程15-6/b16blazorIDS2

截图

安装 FreeSql.Generator 命令行代码生成器生成实体类

对于此工具的使用可参考 https://github.com/dotnetcore/FreeSql/wiki/DbFirst , 也可直接运行命令查看 FreeSql.Generator

安装 dotnet-tool 生成实体类

dotnet tool install -g FreeSql.Generator

生成实体

  1. 项目右键添加 Model 目录
  2. 右键选择在终端中打开
  3. 输入命令
FreeSql.Generator  -NameOptions 0,0,0,0 -NameSpace b16blazorIDS2.Models.ids -DB "Sqlite,Data Source=../ids.db;" -Filter "View+StoreProcedure" -FileName "{name}.cs"

解释

  • -NameOptions 0,0,0,0 首字母大写, 首字母大写,其他小写, 全部小写, 下划线转驼峰
  • -DB "Sqlite,Data Source=../ids.db;" 数据库类型和连接字符串,本例数据库在上一级目录,所以是../ids.db
  • -Filter "View+StoreProcedure" 不生成视图和存储过程

生成的实体

添加 BootstrapBlazor 组件库

相关步骤往日文章有写,不再赘述,只贴上关键部分.

引用Nuget包

        <PackageReference Include="BootstrapBlazor" Version="7.*" />
<PackageReference Include="BootstrapBlazor.FontAwesome" Version="7.*" />
<PackageReference Include="Densen.Extensions.BootstrapBlazor" Version="7.*" />
<PackageReference Include="Densen.FreeSql.Extensions.BootstrapBlazor" Version="7.*" />
<PackageReference Include="FreeSql.Provider.Sqlite" Version="3.2.685" />
<PackageReference Include="Magicodes.IE.Core" Version="2.7.1" />
<PackageReference Include="Magicodes.IE.Excel" Version="2.7.1" />
<PackageReference Include="Magicodes.IE.Html" Version="2.7.1" />
<PackageReference Include="Magicodes.IE.Pdf" Version="2.7.1" />
<PackageReference Include="Magicodes.IE.Word" Version="2.7.1" />
<PackageReference Include="HtmlToOpenXml.dll" Version="2.3.0" />
<PackageReference Include="Haukcode.WkHtmlToPdfDotNet" Version="1.5.86" />

App.razor

<BootstrapBlazorRoot>
<CascadingAuthenticationState>
...
</CascadingAuthenticationState>
</BootstrapBlazorRoot>

_Imports.razor

添加的代码

@using BootstrapBlazor.Components
@using AME.Services
@using Blazor100.Service
@using System.Diagnostics.CodeAnalysis

Pages_Host.cshtml

    <!-- 删掉这行 <link rel="stylesheet" href="css/bootstrap/bootstrap.min.css" /> !-->
<link href="css/site.css" rel="stylesheet" /> <!-- 添加下面两行 !-->
<link href="_content/BootstrapBlazor.FontAwesome/css/font-awesome.min.css" rel="stylesheet">
<link href="_content/BootstrapBlazor/css/bootstrap.blazor.bundle.min.css" rel="stylesheet">
<!-- 添加上面两行 !--> ... <!-- <script src="_framework/blazor.server.js"></script> 之前增加这行 !-->
<script src="_content/BootstrapBlazor/js/bootstrap.blazor.bundle.min.js"></script>

添加导入导出服务

新建 Service 文件夹, 新建 ImportExportsService.cs 文件

using BootstrapBlazor.Components;
using Magicodes.ExporterAndImporter.Core;
using Magicodes.ExporterAndImporter.Excel;
using Magicodes.ExporterAndImporter.Html;
using Magicodes.ExporterAndImporter.Pdf;
using Magicodes.ExporterAndImporter.Word; namespace Blazor100.Service
{
/// <summary>
/// 通用导入导出服务类
/// </summary>
public class ImportExportsService
{
public enum ExportType
{
Excel,
Pdf,
Word,
Html
} public async Task<string> Export<T>(string filePath, List<T>? items = null, ExportType exportType = ExportType.Excel) where T : class, new()
{
switch (exportType)
{
case ExportType.Pdf:
var exporterPdf = new PdfExporter();
items = items ?? new List<T>();
var resultPdf = await exporterPdf.ExportListByTemplate(filePath + ".pdf", items);
return resultPdf.FileName;
case ExportType.Word:
var exporterWord = new WordExporter();
items = items ?? new List<T>();
var resultWord = await exporterWord.ExportListByTemplate(filePath + ".docx", items);
return resultWord.FileName;
case ExportType.Html:
var exporterHtml = new HtmlExporter();
items = items ?? new List<T>();
var resultHtml = await exporterHtml.ExportListByTemplate(filePath + ".html", items);
return resultHtml.FileName;
default:
IExporter exporter = new ExcelExporter();
items = items ?? new List<T>();
var result = await exporter.Export(filePath + ".xlsx", items);
return result.FileName;
}
} public async Task<(IEnumerable<T>? items,string error)> ImportFormExcel<T>(string filePath) where T : class, new()
{
IExcelImporter Importer = new ExcelImporter();
var import = await Importer.Import<T>(filePath);
if (import.Data == null )
{
return (null, import.Exception.Message);
}
return (import.Data!.ToList(),"");
} }
}

Program.cs

顶上添加

using Densen.DataAcces.FreeSql;
using Blazor100.Service;

builder.Services.AddSingleton<WeatherForecastService>(); 下面添加

builder.Services.AddFreeSql(option =>
{
option.UseConnectionString(FreeSql.DataType.Sqlite, "Data Source=ids.db;") //也可以写到配置文件中
#if DEBUG
//开发环境:自动同步实体
.UseAutoSyncStructure(true)
.UseNoneCommandParameter(true)
//调试sql语句输出
.UseMonitorCommand(cmd => System.Console.WriteLine(cmd.CommandText))
#endif
;
});
builder.Services.AddSingleton(typeof(FreeSqlDataService<>)); builder.Services.AddTransient<ImportExportsService>();
builder.Services.AddDensenExtensions();
builder.Services.ConfigureJsonLocalizationOptions(op =>
{
// 忽略文化信息丢失日志
op.IgnoreLocalizerMissing = true;
});

管理页面

Pages 添加组件 DataAdmin.razor

@page "/DataAdmin"
@using b16blazorIDS2.Models.ids
@using static Blazor100.Service.ImportExportsService <PageTitle>管理</PageTitle> <Tab IsLazyLoadTabItem="true">
<TabItem Text="Users">
<Table TItem="AspNetUsers"
IsPagination="true"
IsStriped="true"
IsBordered="true"
AutoGenerateColumns="true"
ShowSearch="true"
ShowToolbar="true"
ShowExtendButtons="true"
DoubleClickToEdit=true
ShowColumnList=true
ShowCardView=true> <TableToolbarTemplate>
<TableToolbarButton TItem="AspNetUsers" Color="Color.Primary" Text="自由编辑" OnClick="@IsExcelToggle" />
</TableToolbarTemplate> </Table>
</TabItem>
<TabItem Text="Roles">
<Table TItem="AspNetRoles"
IsPagination="true"
IsStriped="true"
IsBordered="true"
AutoGenerateColumns="true"
ShowSearch="true"
ShowToolbar="true"
ShowExtendButtons="true"
DoubleClickToEdit=true
ShowColumnList=true
ShowCardView=true> <TableToolbarTemplate>
<TableToolbarButton TItem="AspNetRoles" Color="Color.Primary" Text="自由编辑" OnClick="@IsExcelToggle" />
</TableToolbarTemplate> </Table>
</TabItem>
<TabItem Text="Logins">
<Table TItem="AspNetUserLogins"
IsPagination="true"
IsStriped="true"
IsBordered="true"
AutoGenerateColumns="true"
ShowSearch="true"
ShowToolbar="true"
ShowExtendButtons="true"
DoubleClickToEdit=true
ShowColumnList=true
ShowCardView=true> <TableToolbarTemplate>
<TableToolbarButton TItem="AspNetUserLogins" Color="Color.Primary" Text="自由编辑" OnClick="@IsExcelToggle" />
</TableToolbarTemplate> </Table>
</TabItem>
</Tab>

组件 DataAdmin.razor 后置代码 DataAdmin.razor.cs

using Blazor100.Service;
using BootstrapBlazor.Components;
using Microsoft.AspNetCore.Components;
using System.Diagnostics.CodeAnalysis; namespace b16blazorIDS2.Pages
{
public partial class DataAdmin
{ [Inject]
IWebHostEnvironment? HostEnvironment { get; set; } [Inject]
[NotNull]
NavigationManager? NavigationManager { get; set; } [Inject]
[NotNull]
ImportExportsService? ImportExportsService { get; set; } [Inject]
[NotNull]
ToastService? ToastService { get; set; } // 由于使用了FreeSql ORM 数据服务,可以直接取对象
[Inject]
[NotNull]
IFreeSql? fsql { get; set; } [Inject] ToastService? toastService { get; set; }
[Inject] SwalService? SwalService { get; set; } public bool IsExcel { get; set; }
public bool DoubleClickToEdit { get; set; } = true;
protected string UploadPath = "";
protected string? uploadstatus;
long maxFileSize = 1024 * 1024 * 15;
string? tempfilename; private Task IsExcelToggle()
{
IsExcel = !IsExcel;
DoubleClickToEdit = !IsExcel;
StateHasChanged();
return Task.CompletedTask;
} }
}

运行截图

本节源码

https://github.com/densen2014/Blazor100/tree/Blazor-教程15-6/b16blazorIDS2

源代码

https://github.com/densen2014/Blazor100

https://gitee.com/densen2014/Blazor100 (镜像/非最新版)

Blazor入门100天 : 身份验证和授权 (6) - 使用 FreeSql orm 管理ids数据的更多相关文章

  1. ASP.NET Web API身份验证和授权

    英语原文地址:http://www.asp.net/web-api/overview/security/authentication-and-authorization-in-aspnet-web-a ...

  2. ASP.NET MVC5学习系列——身份验证、授权

    一.什么是身份验证和授权 人们有时对用户身份验证和用户授权之间的区别感到疑惑.用户身份验证是指通过某种形式的登录机制(包括用户名/密码.OpenID.OAuth等说明身份的项)来核实用户的身份.授权验 ...

  3. 使用JWT的ASP.NET CORE令牌身份验证和授权(无Cookie)——第1部分

    原文:使用JWT的ASP.NET CORE令牌身份验证和授权(无Cookie)--第1部分 原文链接:https://www.codeproject.com/Articles/5160941/ASP- ...

  4. 从零搭建一个IdentityServer——聊聊Asp.net core中的身份验证与授权

    OpenIDConnect是一个身份验证服务,而Oauth2.0是一个授权框架,在前面几篇文章里通过IdentityServer4实现了基于Oauth2.0的客户端证书(Client_Credenti ...

  5. 使用 cookie 的身份验证和授权

    前言 在上一章 学学 dotnet core 中的身份验证和授权-1-概念 中,我们大致明白了身份验证和授权两者的关系.那么在本文中,我们将使用 cookie 来做一个简单的身份验证和授权. 本文中我 ...

  6. 学学dotnet core中的身份验证和授权-1-概念

    前言 身份验证: Authentication 授权: Authorization net core 中的身份验证和授权这两个部分,是相辅相成的.当初我在学在部分的时候,是看的 net core 官网 ...

  7. ASP.NET WEBAPI 的身份验证和授权

    定义 身份验证(Authentication):确定用户是谁. 授权(Authorization):确定用户能做什么,不能做什么. 身份验证 WebApi 假定身份验证发生在宿主程序称中.对于 web ...

  8. ASP.NET MVC5(五):身份验证、授权

    使用Authorize特性进行身份验证 通常情况下,应用程序都是要求用户登录系统之后才能访问某些特定的部分.在ASP.NET MVC中,可以通过使用Authorize特性来实现,甚至可以对整个应用程序 ...

  9. mongo的身份验证和授权

    问题来源 刚装好的mongo,准备登陆进去测一把的,结果就给我报这个错,鄙人是新手,还不太清楚这个,现学一下~ Mongo的身份验证 在上一篇安装mongo的博客中(https://www.cnblo ...

  10. shiro系列二、身份验证和授权

    一.身份验证 先来看看身份验证的流程 流程如下: 1.首先调用Subject.login(token)进行登录,其会自动委托给Security Manager,调用之前必须通过SecurityUtil ...

随机推荐

  1. VP记录

    预计在最后的日子里适量VP 简单记录一下 CF 1037 Link 上来秒了ABCD,很快啊 A是二进制拆分,B是一眼贪心,C是一个非常简单且好写的dp D把边遍历顺序按照所需的bfs顺序排序,最后比 ...

  2. LoadRunner11脚本小技能之同步/异步接口分离+批量替换请求头

    最近在公司又进行了一次LoadRunner11性能测试,技能又get了一点,继续Mark起来!!! 一.异步/同步接口分离 之前在另一篇博文中有提到"事务拆分"的小节,即一个htm ...

  3. R数据分析:孟德尔随机化实操

    好多同学询问孟德尔随机化的问题,我再来尝试着梳理一遍,希望对大家有所帮助,首先看下图1分钟,盯着看将下图印在脑海中: 上图是工具变量(不知道工具变量请翻之前的文章)的模式图,明确一个点:我们做孟德尔的 ...

  4. DL账号密码生命周期信息流图

  5. 深度学习之step by step搭建神经网络

    声明 本文参考Deep-Learning-Specialization-Coursera/Convolution_model_Step_by_Step_v1.ipynb at main · abdur ...

  6. 基于.NET 7 的 WebTransport 实现双向通信

    Web Transport 简介 WebTransport 是一个新的 Web API,使用 HTTP/3 协议来支持双向传输.它用于 Web 客户端和 HTTP/3 服务器之间的双向通信.它支持通过 ...

  7. C++ 一个简洁的CHECK宏

    #define CHECK2(condition, message) \ (!(condition)) ? (std::cerr << "Assertion failed: (& ...

  8. python编程学习方法及计算机基础理论

    **从零开始学习编程 ** 一.学习前语 在学习python之前首先先说几点学习建议,首先是培养自己能解决问题的能力: 1.遇到问题时给自己设置一个解决该问题的时间限制 0-5min:自己解决问题(百 ...

  9. typora实现多平台发布文章

    源码下载 前言 之前写过一片文章,typora 使用CSDN作为图床,用来存储 markdown 文章的图片资源文件.后来发现 typora 还可以自定义导出命令,那么也可以利用这个功能实现直接发布到 ...

  10. MySQL库,表,数据的操作

    数据库的操作 1. 创建数据库 create database [if not exists] `数据库名` charset=字符编码(utf8mb4); 如果多次创建会报错 如果不指定字符编码,默认 ...