Skip to content

Commit 1d24373

Browse files
committed
#28 Move to EF Core.
1 parent 2b00cc0 commit 1d24373

32 files changed

+3977
-30
lines changed

aspnet-core/AbpCompanyName.AbpProjectName.sln

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbpCompanyName.AbpProjectNa
1313
EndProject
1414
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbpCompanyName.AbpProjectName.Tests", "test\AbpCompanyName.AbpProjectName.Tests\AbpCompanyName.AbpProjectName.Tests.csproj", "{0D4C5D00-C144-4213-A007-4B8944113AB1}"
1515
EndProject
16-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbpCompanyName.AbpProjectName.EntityFramework", "src\AbpCompanyName.AbpProjectName.EntityFramework\AbpCompanyName.AbpProjectName.EntityFramework.csproj", "{DC780BC4-4EAC-4C63-9052-6F3169A59FA4}"
17-
EndProject
1816
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbpCompanyName.AbpProjectName.Migrator", "src\AbpCompanyName.AbpProjectName.Migrator\AbpCompanyName.AbpProjectName.Migrator.csproj", "{880B3591-E057-46FE-B525-10BD83828B93}"
1917
EndProject
2018
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbpCompanyName.AbpProjectName.Web.Host", "src\AbpCompanyName.AbpProjectName.Web.Host\AbpCompanyName.AbpProjectName.Web.Host.csproj", "{38E184BD-E874-4633-A947-AED4FDB73F40}"
@@ -23,6 +21,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbpCompanyName.AbpProjectNa
2321
EndProject
2422
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbpCompanyName.AbpProjectName.Web.Mvc", "src\AbpCompanyName.AbpProjectName.Web.Mvc\AbpCompanyName.AbpProjectName.Web.Mvc.csproj", "{A2213374-BB48-48FD-BBD4-81E6A961D866}"
2523
EndProject
24+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbpCompanyName.AbpProjectName.EntityFrameworkCore", "src\AbpCompanyName.AbpProjectName.EntityFrameworkCore\AbpCompanyName.AbpProjectName.EntityFrameworkCore.csproj", "{E0580562-F8F2-4EBB-B07A-ABFC6F2C314F}"
25+
EndProject
2626
Global
2727
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2828
Debug|Any CPU = Debug|Any CPU
@@ -41,10 +41,6 @@ Global
4141
{0D4C5D00-C144-4213-A007-4B8944113AB1}.Debug|Any CPU.Build.0 = Debug|Any CPU
4242
{0D4C5D00-C144-4213-A007-4B8944113AB1}.Release|Any CPU.ActiveCfg = Release|Any CPU
4343
{0D4C5D00-C144-4213-A007-4B8944113AB1}.Release|Any CPU.Build.0 = Release|Any CPU
44-
{DC780BC4-4EAC-4C63-9052-6F3169A59FA4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
45-
{DC780BC4-4EAC-4C63-9052-6F3169A59FA4}.Debug|Any CPU.Build.0 = Debug|Any CPU
46-
{DC780BC4-4EAC-4C63-9052-6F3169A59FA4}.Release|Any CPU.ActiveCfg = Release|Any CPU
47-
{DC780BC4-4EAC-4C63-9052-6F3169A59FA4}.Release|Any CPU.Build.0 = Release|Any CPU
4844
{880B3591-E057-46FE-B525-10BD83828B93}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
4945
{880B3591-E057-46FE-B525-10BD83828B93}.Debug|Any CPU.Build.0 = Debug|Any CPU
5046
{880B3591-E057-46FE-B525-10BD83828B93}.Release|Any CPU.ActiveCfg = Release|Any CPU
@@ -61,6 +57,10 @@ Global
6157
{A2213374-BB48-48FD-BBD4-81E6A961D866}.Debug|Any CPU.Build.0 = Debug|Any CPU
6258
{A2213374-BB48-48FD-BBD4-81E6A961D866}.Release|Any CPU.ActiveCfg = Release|Any CPU
6359
{A2213374-BB48-48FD-BBD4-81E6A961D866}.Release|Any CPU.Build.0 = Release|Any CPU
60+
{E0580562-F8F2-4EBB-B07A-ABFC6F2C314F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
61+
{E0580562-F8F2-4EBB-B07A-ABFC6F2C314F}.Debug|Any CPU.Build.0 = Debug|Any CPU
62+
{E0580562-F8F2-4EBB-B07A-ABFC6F2C314F}.Release|Any CPU.ActiveCfg = Release|Any CPU
63+
{E0580562-F8F2-4EBB-B07A-ABFC6F2C314F}.Release|Any CPU.Build.0 = Release|Any CPU
6464
EndGlobalSection
6565
GlobalSection(SolutionProperties) = preSolution
6666
HideSolutionNode = FALSE
@@ -69,10 +69,10 @@ Global
6969
{0FA75A5B-AB83-4FD0-B545-279774C01E87} = {AFAA0841-BD93-466F-B8F4-FB4EEC86F1FC}
7070
{3870C648-4AEA-4B85-BA3F-F2F63B96136A} = {AFAA0841-BD93-466F-B8F4-FB4EEC86F1FC}
7171
{0D4C5D00-C144-4213-A007-4B8944113AB1} = {F10AA149-2626-486E-85BB-9CD5365F3016}
72-
{DC780BC4-4EAC-4C63-9052-6F3169A59FA4} = {AFAA0841-BD93-466F-B8F4-FB4EEC86F1FC}
7372
{880B3591-E057-46FE-B525-10BD83828B93} = {AFAA0841-BD93-466F-B8F4-FB4EEC86F1FC}
7473
{38E184BD-E874-4633-A947-AED4FDB73F40} = {AFAA0841-BD93-466F-B8F4-FB4EEC86F1FC}
7574
{22CFE0D2-8DCA-42D7-AD7D-784C3862493F} = {AFAA0841-BD93-466F-B8F4-FB4EEC86F1FC}
7675
{A2213374-BB48-48FD-BBD4-81E6A961D866} = {AFAA0841-BD93-466F-B8F4-FB4EEC86F1FC}
76+
{E0580562-F8F2-4EBB-B07A-ABFC6F2C314F} = {AFAA0841-BD93-466F-B8F4-FB4EEC86F1FC}
7777
EndGlobalSection
7878
EndGlobal

aspnet-core/src/AbpCompanyName.AbpProjectName.Application/MultiTenancy/TenantAppService.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using AbpCompanyName.AbpProjectName.Authorization.Users;
1313
using AbpCompanyName.AbpProjectName.Editions;
1414
using AbpCompanyName.AbpProjectName.MultiTenancy.Dto;
15+
using Microsoft.AspNetCore.Identity;
1516

1617
namespace AbpCompanyName.AbpProjectName.MultiTenancy
1718
{
@@ -22,17 +23,20 @@ public class TenantAppService : AbpProjectNameAppServiceBase, ITenantAppService
2223
private readonly RoleManager _roleManager;
2324
private readonly EditionManager _editionManager;
2425
private readonly IAbpZeroDbMigrator _abpZeroDbMigrator;
26+
private readonly IPasswordHasher<User> _passwordHasher;
2527

2628
public TenantAppService(
2729
TenantManager tenantManager,
2830
RoleManager roleManager,
2931
EditionManager editionManager,
30-
IAbpZeroDbMigrator abpZeroDbMigrator)
32+
IAbpZeroDbMigrator abpZeroDbMigrator,
33+
IPasswordHasher<User> passwordHasher)
3134
{
3235
_tenantManager = tenantManager;
3336
_roleManager = roleManager;
3437
_editionManager = editionManager;
3538
_abpZeroDbMigrator = abpZeroDbMigrator;
39+
_passwordHasher = passwordHasher;
3640
}
3741

3842
public ListResultDto<TenantListDto> GetTenants()
@@ -78,7 +82,8 @@ public async Task CreateTenant(CreateTenantInput input)
7882
await _roleManager.GrantAllPermissionsAsync(adminRole);
7983

8084
//Create admin user for the tenant
81-
var adminUser = User.CreateTenantAdminUser(tenant.Id, input.AdminEmailAddress, User.DefaultPassword);
85+
var adminUser = User.CreateTenantAdminUser(tenant.Id, input.AdminEmailAddress);
86+
adminUser.Password = _passwordHasher.HashPassword(adminUser, User.DefaultPassword);
8287
await UserManager.CreateAsync(adminUser);
8388
await CurrentUnitOfWork.SaveChangesAsync(); //To get admin user's id
8489

aspnet-core/src/AbpCompanyName.AbpProjectName.Core/AbpCompanyName.AbpProjectName.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="1.1.1" />
2525
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.1" />
2626
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="1.1.1" />
27+
<PackageReference Include="Castle.Windsor.MsDependencyInjection" Version="2.0.0" />
2728
</ItemGroup>
2829

2930
<ItemGroup Condition=" '$(TargetFramework)' == 'net461' ">

aspnet-core/src/AbpCompanyName.AbpProjectName.Core/Authorization/Users/User.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public static string CreateRandomPassword()
1313
return Guid.NewGuid().ToString("N").Truncate(16);
1414
}
1515

16-
public static User CreateTenantAdminUser(int tenantId, string emailAddress, string password)
16+
public static User CreateTenantAdminUser(int tenantId, string emailAddress)
1717
{
1818
return new User
1919
{
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using AbpCompanyName.AbpProjectName.Authorization.Roles;
2+
using AbpCompanyName.AbpProjectName.Authorization.Users;
3+
using AbpCompanyName.AbpProjectName.MultiTenancy;
4+
using Microsoft.Extensions.DependencyInjection;
5+
6+
namespace AbpCompanyName.AbpProjectName.Identity
7+
{
8+
public static class IdentityRegistrar
9+
{
10+
public static void Register(ServiceCollection services)
11+
{
12+
services.AddLogging();
13+
14+
services.AddAbpIdentity<Tenant, User, Role, SecurityStampValidator>(options =>
15+
{
16+
options.Cookies.ApplicationCookie.AuthenticationScheme = "AbpZeroTemplateAuthSchema";
17+
options.Cookies.ApplicationCookie.CookieName = "AbpZeroTemplateAuth";
18+
})
19+
.AddUserManager<UserManager>()
20+
.AddRoleManager<RoleManager>()
21+
.AddSignInManager<SignInManager>()
22+
.AddClaimsPrincipalFactory<UserClaimsPrincipalFactory>()
23+
.AddDefaultTokenProviders();
24+
}
25+
}
26+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Abp.Authorization;
2+
using AbpCompanyName.AbpProjectName.Authorization.Roles;
3+
using AbpCompanyName.AbpProjectName.Authorization.Users;
4+
using AbpCompanyName.AbpProjectName.MultiTenancy;
5+
using Microsoft.AspNetCore.Builder;
6+
using Microsoft.Extensions.Options;
7+
8+
namespace AbpCompanyName.AbpProjectName.Identity
9+
{
10+
public class SecurityStampValidator : AbpSecurityStampValidator<Tenant, Role, User>
11+
{
12+
public SecurityStampValidator(
13+
IOptions<IdentityOptions> options,
14+
SignInManager signInManager)
15+
: base(options, signInManager)
16+
{
17+
}
18+
}
19+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using Abp.Authorization;
2+
using Abp.Configuration;
3+
using Abp.Domain.Uow;
4+
using AbpCompanyName.AbpProjectName.Authorization.Roles;
5+
using AbpCompanyName.AbpProjectName.Authorization.Users;
6+
using AbpCompanyName.AbpProjectName.MultiTenancy;
7+
using Microsoft.AspNetCore.Builder;
8+
using Microsoft.AspNetCore.Http;
9+
using Microsoft.AspNetCore.Identity;
10+
using Microsoft.Extensions.Logging;
11+
using Microsoft.Extensions.Options;
12+
13+
namespace AbpCompanyName.AbpProjectName.Identity
14+
{
15+
public class SignInManager : AbpSignInManager<Tenant, Role, User>
16+
{
17+
public SignInManager(
18+
UserManager userManager,
19+
IHttpContextAccessor contextAccessor,
20+
UserClaimsPrincipalFactory claimsFactory,
21+
IOptions<IdentityOptions> optionsAccessor,
22+
ILogger<SignInManager<User>> logger,
23+
IUnitOfWorkManager unitOfWorkManager,
24+
ISettingManager settingManager
25+
) : base(
26+
userManager,
27+
contextAccessor,
28+
claimsFactory,
29+
optionsAccessor,
30+
logger,
31+
unitOfWorkManager,
32+
settingManager)
33+
{
34+
}
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>net461;netcoreapp1.1</TargetFrameworks>
5+
<RootNamespace>AbpCompanyName.AbpProjectName</RootNamespace>
6+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="1.1.1" />
11+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.1" />
12+
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.1.0" />
13+
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.1" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<ProjectReference Include="..\AbpCompanyName.AbpProjectName.Core\AbpCompanyName.AbpProjectName.Core.csproj" />
18+
</ItemGroup>
19+
20+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using Abp.Zero.EntityFrameworkCore;
2+
using AbpCompanyName.AbpProjectName.Authorization.Roles;
3+
using AbpCompanyName.AbpProjectName.Authorization.Users;
4+
using AbpCompanyName.AbpProjectName.MultiTenancy;
5+
using Microsoft.EntityFrameworkCore;
6+
7+
namespace AbpCompanyName.AbpProjectName.EntityFrameworkCore
8+
{
9+
public class AbpProjectNameDbContext : AbpZeroDbContext<Tenant, Role, User, AbpProjectNameDbContext>
10+
{
11+
/* Define an IDbSet for each entity of the application */
12+
13+
public AbpProjectNameDbContext(DbContextOptions<AbpProjectNameDbContext> options)
14+
: base(options)
15+
{
16+
17+
}
18+
19+
protected override void OnModelCreating(ModelBuilder modelBuilder)
20+
{
21+
base.OnModelCreating(modelBuilder);
22+
23+
//...
24+
}
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using Abp.EntityFrameworkCore.Configuration;
2+
using Abp.Modules;
3+
using Abp.Reflection.Extensions;
4+
using Abp.Zero.EntityFrameworkCore;
5+
using AbpCompanyName.AbpProjectName.EntityFrameworkCore.Seed;
6+
7+
namespace AbpCompanyName.AbpProjectName.EntityFrameworkCore
8+
{
9+
[DependsOn(
10+
typeof(AbpProjectNameCoreModule),
11+
typeof(AbpZeroCoreEntityFrameworkCoreModule))]
12+
public class AbpProjectNameEntityFrameworkModule : AbpModule
13+
{
14+
/* Used it tests to skip dbcontext registration, in order to use in-memory database of EF Core */
15+
public bool SkipDbContextRegistration { get; set; }
16+
17+
public bool SkipDbSeed { get; set; }
18+
19+
20+
public override void PreInitialize()
21+
{
22+
if (!SkipDbContextRegistration)
23+
{
24+
Configuration.Modules.AbpEfCore().AddDbContext<AbpProjectNameDbContext>(configuration =>
25+
{
26+
AbpProjectNameDbContextConfigurer.Configure(configuration.DbContextOptions, configuration.ConnectionString);
27+
});
28+
}
29+
}
30+
31+
public override void Initialize()
32+
{
33+
IocManager.RegisterAssemblyByConvention(typeof(AbpProjectNameEntityFrameworkModule).GetAssembly());
34+
}
35+
36+
public override void PostInitialize()
37+
{
38+
if (!SkipDbSeed)
39+
{
40+
SeedHelper.SeedHostDb(IocManager);
41+
}
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)