Skip to content
IGeekFan 开发者文档IGeekFan 开发者文档
CMS
.NET指南
FreeKit
Docker
关于
博客
github icon
    • FreeKit
      • 基础包
        • aspnetcore identity freesql 的实现
          • IGeekFan.AspNetCore.Identity.FreeSql
            • 扩展用户、角色
              • 配置用户、角色的FulentAPI
                • 配置 Identity+FreeSql
              • Email 邮件
                • Extras 扩展包
                  • Modularity 模块化
                    • 本地化

                    aspnetcore identity freesql 的实现

                    calendar icon2022年6月2日timer icon大约 1 分钟word icon约 344 字

                    此页内容
                    • IGeekFan.AspNetCore.Identity.FreeSql
                      • 扩展用户、角色
                      • 配置用户、角色的FulentAPI
                      • 配置 Identity+FreeSql

                    # aspnetcore identity freesql 的实现

                    # IGeekFan.AspNetCore.Identity.FreeSql

                    asp.net core 6 的identity的freesql实现

                    • 安装包
                    dotnet add package IGeekFan.AspNetCore.Identity.FreeSql
                    
                    1
                    • 新增 FreeSql的Provider 相关包
                    dotnet add package FreeSql.Provider.MySqlConnector
                    
                    1

                    # 扩展用户、角色

                    public class AppUser : IdentityUser<Guid>
                    {
                    }
                    public class AppRole : IdentityRole<Guid>
                    {
                    
                    }
                    public class IdentityContext : IdentityDbContext<AppUser, AppRole, Guid>
                    {
                        public IdentityContext(IOptions<IdentityOptions> identityOptions, IFreeSql fsql, DbContextOptions options)
                        : base(identityOptions.Value, fsql, options)
                        {
                        }
                    
                        protected override void OnConfiguring(DbContextOptionsBuilder builder)
                        {
                            //这里直接指定一个静态的 IFreeSql 对象即可,切勿重新 Build()
                        }
                    
                        protected override void OnModelCreating(ICodeFirst codefirst)
                        {
                            base.OnModelCreating(codefirst);
                        }
                    }
                    
                    
                    1
                    2
                    3
                    4
                    5
                    6
                    7
                    8
                    9
                    10
                    11
                    12
                    13
                    14
                    15
                    16
                    17
                    18
                    19
                    20
                    21
                    22
                    23
                    24
                    25

                    # 配置用户、角色的FulentAPI

                    public class AppUserConfiguration : IEntityTypeConfiguration<AppUser>
                    {
                        public void Configure(EfCoreTableFluent<AppUser> model)
                        {
                            model.ToTable("app_user");
                        }
                    }
                    public class AppRoleConfiguration : IEntityTypeConfiguration<AppRole>
                    {
                        public void Configure(EfCoreTableFluent<AppRole> model)
                        {
                            model.ToTable("app_role");
                        }
                    }
                    
                    1
                    2
                    3
                    4
                    5
                    6
                    7
                    8
                    9
                    10
                    11
                    12
                    13
                    14
                    • appsettings.json 该配置通过方法UseConnectionString读取如下配置
                    "ConnectionStrings": {
                        "MySql": "Data Source=localhost;Port=3306;User ID=root;Password=root;Initial Catalog=file;Charset=utf8mb4;SslMode=none;Max pool size=1;Connection LifeTime=20"
                    }
                    
                    1
                    2
                    3

                    # 配置 Identity+FreeSql

                    • 新增一个扩展方法,引用 aspnetcore identity 相关服务
                    public static IServiceCollection AddFreeSql(this IServiceCollection services, IConfiguration configuration)
                    {
                        IFreeSql fsql = new FreeSqlBuilder()
                                .UseConnectionString(DataType.MySql, configuration["ConnectionStrings:MySql"])
                                .UseNameConvert(NameConvertType.PascalCaseToUnderscoreWithLower)
                                .UseAutoSyncStructure(true) //自动同步实体结构到数据库,FreeSql不会扫描程序集,只有CRUD时才会生成表。
                                .UseMonitorCommand(cmd =>
                                {
                                    Trace.WriteLine(cmd.CommandText + ";");
                                })
                                .Build();
                        //软删除
                        fsql.GlobalFilter.Apply<ISoftDelete>("IsDeleted", a => a.IsDeleted == false);
                    
                        services.AddSingleton<IFreeSql>(fsql);
                        services.AddFreeRepository();
                        services.AddScoped<UnitOfWorkManager>();
                    
                        //只有实例化了ToDoContext,才能正常调用OnModelCreating,不然直接使用仓储,无法调用DbContext中的OnModelCreating方法,,配置的TodoConfiguration 就会没有生效
                        services.AddFreeDbContext<IdentityContext>(options => options
                                    .UseFreeSql(fsql)
                                    .UseOptions(new DbContextOptions()
                                    {
                                        EnableAddOrUpdateNavigateList = true
                                    })
                        );
                    
                        services.AddIdentityCore<AppUser>(options => options.SignIn.RequireConfirmedAccount = true)
                                .AddFreeSqlStores<IdentityContext>();
                    
                        //fsql.CodeFirst.ApplyConfiguration(new TodoConfiguration());
                    
                        return services;
                    }
                    
                    1
                    2
                    3
                    4
                    5
                    6
                    7
                    8
                    9
                    10
                    11
                    12
                    13
                    14
                    15
                    16
                    17
                    18
                    19
                    20
                    21
                    22
                    23
                    24
                    25
                    26
                    27
                    28
                    29
                    30
                    31
                    32
                    33
                    34
                    edit icon在 GitHub 上编辑此页open in new window
                    上次编辑于: 2022/6/29 19:17:23
                    贡献者: igeekfan
                    上一页
                    基础包
                    下一页
                    Email 邮件
                    MIT Licensed | Copyright © 2021-present luoyunchong
                    苏ICP备16046457号-1

                    该应用可以安装在你的 PC 或移动设备上。这将使该 Web 应用程序外观和行为与其他应用程序相同。它将在出现在应用程序列表中,并可以固定到主屏幕,开始菜单或任务栏。此 Web 应用程序还将能够与其他应用程序和你的操作系统安全地进行交互。

                    详情