【Abp.VNext】Abp.Vnext框架模块学习

发布于:2025-08-14 ⋅ 阅读:(18) ⋅ 点赞:(0)

1、Abp.Vnext-集成

Volo.Abp.Core

2、Abp.vNext-Web模块

Volo.Abp.AspNetCore.MVC

框架(framework文件夹)

七、Abp.vNext-应用模块-Identity身份认证

业务模块(modules文件夹->identity)

1、添加领域模型
Volo.Abp.Identity.Domain
typeof(AbpIdentityDomainMoule)

核心 IdentityUser模型类

2、实体框架:生成相应的表(生成迁移文件,生成对应的表)

1、集成

  • (1)、添加实体框架
Volo.Abp.Identity.EntityFrameworkCore
typeof(AbpIdentityEntityFrameworkCoreModule)
  • (2)、重写 OnModelCreating
 /// <summary>
 ///  根据模型创建迁移文件。进一步创建表
 /// </summary>
 /// <param name="modelBuilder"></param>
 protected override void OnModelCreating(ModelBuilder builder)
 {
     // 1、根据Identity 生成对应的数据库迁移文件。
     AbpIdentityDbProperties.DbTablePrefix = "Sys_";
     builder.ConfigureIdentity();
}
dotnet ef migrations add 迁移文件名称 -c 指定上下文
dotnet ef database update

会生成

1、abpusers #用户表
2、abproles #角色表
3...

  • (3)、集成Application
Volo.Abp.Identity.Application
typeof(AbpIdentityApplicationModule),
  • (4)、集成WebApi (自动添加Controller
Volo.Abp.Identity.HttpApi
typeof(AbpIdentityHttpApiModule),

启动会报错:报权限错误(因为:[Authorize(IdentityPermissions.Users.Default)] 代表有权限才能调用)
需要引用权限模块(因为引用了权限模块的EfCore : Volo.Abp.PermissionManagement.EntityFrameworkCore

Volo.Abp.PermissionManagement.EntityFrameworkCore
typeof(AbpPermissionManagementEntityFrameworkCoreModule)

多了Role(角色)、User(用户)、UserLookUp(向上查找、搜索用户用的) 三个模块

添加身份认证权限,否则调用添加用户会报错权限问题

// 10、添加身份认证
context.Services.AddAuthentication().AddCookie();

// 11、添加权限校验
context.Services.AddAuthorization();

// 12、去掉所有的权限
context.Services.AddAlwaysAllowAuthorization();

注意:需要先添加角色,否则会报 角色字符串不存在。,然后再调用创建用户接口 才会成功!

1、ConfigureAwait(false) 是一种优化手段,确保异步操作完成后不会强制切换回原始上下文,通常用于提升性能并避免死锁风险。

2、.ToListAsync(cancellationToken: cancellationToken) 的作用是:

异步执行查询,将结果转为 List<T>。
支持取消,通过 CancellationToken 实现可控的中断机制。
在你的代码中,它高效地加载了用户与组织单元角色的关联数据,同时允许外部控制查询的生命周期。

它有自己的数据库上下文 IdentityDbContext ,这个模块用的也是自己的数据库上下文。

2、自定义框架
(1)、自定义表前缀(EbusinessModule)

 public override void ConfigureServices(ServiceConfigurationContext context)
 {
	// 13、identity身份模块访问新前缀
	AbpIdentityDbProperties.DbTablePrefix = "Sys_";
}

/// <summary>
///  根据模型创建迁移文件。进一步创建表(ABPEbusinessContext)
/// </summary>
/// <param name="modelBuilder"></param>
protected override void OnModelCreating(ModelBuilder builder)
{
     // 1、根据Identity 生成对应的数据库迁移文件。
     AbpIdentityDbProperties.DbTablePrefix = "Sys_";
}

不建议改源码 表的后缀

(2)、自定义用户模型、

/// <summary>
/// (1)、用户自定义字段
/// </summary>
public class SysUser : IdentityUser
{
    public string CreateTony { set; get; }
}

public DbSet<SysUser> sys_users { set; get; }


 /// <summary>
 /// (2.1)用户自定义仓储 用来实现abp提供的仓储
 /// </summary>

 public interface IUserABPRepository : IRepository<SysUser, Guid>
 {
    Task<SysUser> FindByNormalizedUserNameAsync(
         [NotNull] string normalizedUserName,
         bool includeDetails = true,
         CancellationToken cancellationToken = default
     );
 ...
 }

 /// <summary>
 /// (2.2)用户仓储自定义实现
 /// </summary>
 [Dependency(ServiceLifetime.Singleton)]
 public class UserABPRepository : EfCoreRepository<ABPEbusinessContext, SysUser, Guid>, IUserABPRepository
 {
     public UserABPRepository(IDbContextProvider<ABPEbusinessContext> dbContextProvider) : base(dbContextProvider)
     {
     }
...
}

 //3. 改动dto

 /// <summary>
 /// 4.1 用户自定义service
 /// </summary>
 public interface IUserABPService : ICrudAppService<
     SysUserDto,
     Guid,
     GetSysUserInput,
     SysUserCreateDto,
     SysUserUpdateDto>
 {
      Task<ListResultDto<IdentityRoleDto>> GetRolesAsync(Guid id);
}

 /// <summary>
 /// 4.2 用户service自定义实现
 /// </summary>
 [Dependency(ServiceLifetime.Singleton)]
 //[Authorize]
 public class UserABPService : CrudAppService<
     SysUser,
     SysUserDto,
     Guid,
     GetSysUserInput,
     SysUserCreateDto,
     SysUserUpdateDto>, IUserABPService, IRemoteService
 {

     public UserABPService(IUserABPRepository userABPRepository) : base(userABPRepository)
     {

     }

     public Task<SysUserDto> FindByEmailAsync(string email)
     {
         throw new NotImplementedException();
     }
}

/// <summary>
/// 5、使用:配置自定义映射类型。dto---->实体,
/// </summary>
public class EbusinessAutoMapperProfile : Profile
{
    public EbusinessAutoMapperProfile()
    {
        // 1、具体类型映射配置
        CreateMap<ProductDto, Product>();

        // 2、配置实体到dto的类型
        CreateMap<Product, ProductDto>();

       

        CreateMap<SysUser,SysUserDto>().ReverseMap();
    }
}

 /// <summary>
 /// 6、用户自定义字段
 /// </summary>
 public class SysUserDto : ExtensibleFullAuditedEntityDto<Guid>, IMultiTenant, IHasConcurrencyStamp, IHasEntityVersion
 {
     public string CreateTony { set; get; }

     public Guid? TenantId { get; set; }

     public string UserName { get; set; }

     public string Name { get; set; }

     public string Surname { get; set; }

     public string Email { get; set; }

     public bool EmailConfirmed { get; set; }

     public string PhoneNumber { get; set; }

     public bool PhoneNumberConfirmed { get; set; }

     public bool IsActive { get; set; }

     public bool LockoutEnabled { get; set; }

     public DateTimeOffset? LockoutEnd { get; set; }

     public string ConcurrencyStamp { get; set; }

     public int EntityVersion { get; set; }
 }
八、Abp.vNext-应用模块-Account账户模块

系统提供的登录和注册

  • 注册:将用户信息存储到数据库
  • 登录:将用户信息存储到Cookie内存。

缺少Domain(领域层)、EFCore层

//集成应用模块,account账户模块
typeof(AbpAccountApplicationModule),
typeof(AbpAccountHttpApiModule),
typeof(AbpAccountWebModule)
九、Abp.vNext-应用模块-Permission权限模块
abppermission

网站公告

今日签到

点亮在社区的每一天
去签到