From e5c202207beddbdd0b7daeec22178e26e4554439 Mon Sep 17 00:00:00 2001 From: NicoSilve22 Date: Tue, 18 Oct 2022 16:27:09 +0200 Subject: [PATCH] packages update project re-organization: separated authentication and authorization with a new class library project, added configurations to EF Core Identity entities --- .../AuthenticationDbContext.cs | 30 ++----------------- .../ApplicationUserConfiguration.cs | 14 +++++++++ .../ApplicationUserRoleConfiguration.cs | 19 ++++++++++++ .../Configurations/TenantConfiguration.cs | 19 ++++++++++++ .../Filters/RoleAuthorizeAttribute.cs | 11 ------- .../IdentitySample.Authentication.csproj | 4 +-- .../Settings/JwtSettings.cs | 4 +-- .../Filters/RoleAuthorizeAttribute.cs | 11 +++++++ .../Handlers}/MinimumAgeHandler.cs | 6 ++-- .../Handlers}/UserActiveHandler.cs | 7 +++-- .../IdentitySample.Authorization.csproj | 16 ++++++++++ .../Requirements/MinimumAgeRequirement.cs | 2 +- .../Requirements/UserActiveRequirement.cs | 2 +- .../IdentitySample.BusinessLayer.csproj | 2 +- .../Services/IdentityService.cs | 12 ++++---- .../IdentitySample.DataAccessLayer.csproj | 2 +- .../IdentitySample.StorageProviders.csproj | 2 +- IdentitySample/IdentitySample.sln | 8 ++++- .../IdentitySample/IdentitySample.csproj | 5 ++-- IdentitySample/IdentitySample/Program.cs | 9 +++--- 20 files changed, 119 insertions(+), 66 deletions(-) create mode 100644 IdentitySample/IdentitySample.Authentication/Configurations/ApplicationUserConfiguration.cs create mode 100644 IdentitySample/IdentitySample.Authentication/Configurations/ApplicationUserRoleConfiguration.cs create mode 100644 IdentitySample/IdentitySample.Authentication/Configurations/TenantConfiguration.cs delete mode 100644 IdentitySample/IdentitySample.Authentication/Filters/RoleAuthorizeAttribute.cs rename IdentitySample/{IdentitySample.BusinessLayer => IdentitySample.Authentication}/Settings/JwtSettings.cs (83%) create mode 100644 IdentitySample/IdentitySample.Authorization/Filters/RoleAuthorizeAttribute.cs rename IdentitySample/{IdentitySample.Authentication/Requirements => IdentitySample.Authorization/Handlers}/MinimumAgeHandler.cs (74%) rename IdentitySample/{IdentitySample.Authentication/Requirements => IdentitySample.Authorization/Handlers}/UserActiveHandler.cs (85%) create mode 100644 IdentitySample/IdentitySample.Authorization/IdentitySample.Authorization.csproj rename IdentitySample/{IdentitySample.Authentication => IdentitySample.Authorization}/Requirements/MinimumAgeRequirement.cs (81%) rename IdentitySample/{IdentitySample.Authentication => IdentitySample.Authorization}/Requirements/UserActiveRequirement.cs (67%) diff --git a/IdentitySample/IdentitySample.Authentication/AuthenticationDbContext.cs b/IdentitySample/IdentitySample.Authentication/AuthenticationDbContext.cs index e69b21f..b46619f 100644 --- a/IdentitySample/IdentitySample.Authentication/AuthenticationDbContext.cs +++ b/IdentitySample/IdentitySample.Authentication/AuthenticationDbContext.cs @@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; +using System.Reflection; namespace IdentitySample.Authentication; @@ -18,33 +19,6 @@ public AuthenticationDbContext(DbContextOptions options protected override void OnModelCreating(ModelBuilder builder) { base.OnModelCreating(builder); - - builder.Entity(user => - { - user.Property(u => u.FirstName).HasMaxLength(256).IsRequired(); - user.Property(u => u.LastName).HasMaxLength(256); - }); - - builder.Entity(userRole => - { - userRole.HasKey(ur => new { ur.UserId, ur.RoleId }); - - userRole.HasOne(ur => ur.Role) - .WithMany(r => r.UserRoles).HasForeignKey(ur => ur.RoleId).IsRequired(); - - userRole.HasOne(ur => ur.User) - .WithMany(u => u.UserRoles).HasForeignKey(ur => ur.UserId).IsRequired(); - }); - - builder.Entity(tenant => - { - tenant.ToTable("Tenants"); - tenant.HasKey(t => t.Id); - tenant.Property(t => t.Id).ValueGeneratedOnAdd(); - - tenant.Property(t => t.ConnectionString).HasMaxLength(4000).IsRequired().IsUnicode(false); - tenant.Property(t => t.StorageConnectionString).HasMaxLength(4000).IsUnicode(false); - tenant.Property(t => t.ContainerName).HasMaxLength(256).IsUnicode(false); - }); + builder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly()); } } diff --git a/IdentitySample/IdentitySample.Authentication/Configurations/ApplicationUserConfiguration.cs b/IdentitySample/IdentitySample.Authentication/Configurations/ApplicationUserConfiguration.cs new file mode 100644 index 0000000..a74a299 --- /dev/null +++ b/IdentitySample/IdentitySample.Authentication/Configurations/ApplicationUserConfiguration.cs @@ -0,0 +1,14 @@ +using IdentitySample.Authentication.Entities; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; + +namespace IdentitySample.Authentication.Configurations; + +public class ApplicationUserConfiguration : IEntityTypeConfiguration +{ + public void Configure(EntityTypeBuilder builder) + { + builder.Property(u => u.FirstName).HasMaxLength(256).IsRequired(); + builder.Property(u => u.LastName).HasMaxLength(256); + } +} \ No newline at end of file diff --git a/IdentitySample/IdentitySample.Authentication/Configurations/ApplicationUserRoleConfiguration.cs b/IdentitySample/IdentitySample.Authentication/Configurations/ApplicationUserRoleConfiguration.cs new file mode 100644 index 0000000..afa3c7b --- /dev/null +++ b/IdentitySample/IdentitySample.Authentication/Configurations/ApplicationUserRoleConfiguration.cs @@ -0,0 +1,19 @@ +using IdentitySample.Authentication.Entities; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; + +namespace IdentitySample.Authentication.Configurations; + +public class ApplicationUserRoleConfiguration : IEntityTypeConfiguration +{ + public void Configure(EntityTypeBuilder builder) + { + builder.HasKey(ur => new { ur.UserId, ur.RoleId }); + + builder.HasOne(ur => ur.Role) + .WithMany(r => r.UserRoles).HasForeignKey(ur => ur.RoleId).IsRequired(); + + builder.HasOne(ur => ur.User) + .WithMany(u => u.UserRoles).HasForeignKey(ur => ur.UserId).IsRequired(); + } +} \ No newline at end of file diff --git a/IdentitySample/IdentitySample.Authentication/Configurations/TenantConfiguration.cs b/IdentitySample/IdentitySample.Authentication/Configurations/TenantConfiguration.cs new file mode 100644 index 0000000..55ff5fc --- /dev/null +++ b/IdentitySample/IdentitySample.Authentication/Configurations/TenantConfiguration.cs @@ -0,0 +1,19 @@ +using IdentitySample.Authentication.Entities; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; + +namespace IdentitySample.Authentication.Configurations; + +public class TenantConfiguration : IEntityTypeConfiguration +{ + public void Configure(EntityTypeBuilder builder) + { + builder.ToTable("Tenants"); + builder.HasKey(t => t.Id); + builder.Property(t => t.Id).ValueGeneratedOnAdd(); + + builder.Property(t => t.ConnectionString).HasMaxLength(4000).IsRequired().IsUnicode(false); + builder.Property(t => t.StorageConnectionString).HasMaxLength(4000).IsUnicode(false); + builder.Property(t => t.ContainerName).HasMaxLength(256).IsUnicode(false); + } +} \ No newline at end of file diff --git a/IdentitySample/IdentitySample.Authentication/Filters/RoleAuthorizeAttribute.cs b/IdentitySample/IdentitySample.Authentication/Filters/RoleAuthorizeAttribute.cs deleted file mode 100644 index 9e8ef98..0000000 --- a/IdentitySample/IdentitySample.Authentication/Filters/RoleAuthorizeAttribute.cs +++ /dev/null @@ -1,11 +0,0 @@ -using Microsoft.AspNetCore.Authorization; - -namespace IdentitySample.Authentication.Filters; - -public class RoleAuthorizeAttribute : AuthorizeAttribute -{ - public RoleAuthorizeAttribute(params string[] roles) - { - Roles = string.Join(",", roles); - } -} diff --git a/IdentitySample/IdentitySample.Authentication/IdentitySample.Authentication.csproj b/IdentitySample/IdentitySample.Authentication/IdentitySample.Authentication.csproj index 9fb71ae..8b1b4f3 100644 --- a/IdentitySample/IdentitySample.Authentication/IdentitySample.Authentication.csproj +++ b/IdentitySample/IdentitySample.Authentication/IdentitySample.Authentication.csproj @@ -6,8 +6,8 @@ - - + + diff --git a/IdentitySample/IdentitySample.BusinessLayer/Settings/JwtSettings.cs b/IdentitySample/IdentitySample.Authentication/Settings/JwtSettings.cs similarity index 83% rename from IdentitySample/IdentitySample.BusinessLayer/Settings/JwtSettings.cs rename to IdentitySample/IdentitySample.Authentication/Settings/JwtSettings.cs index 4c502d9..932f8c5 100644 --- a/IdentitySample/IdentitySample.BusinessLayer/Settings/JwtSettings.cs +++ b/IdentitySample/IdentitySample.Authentication/Settings/JwtSettings.cs @@ -1,4 +1,4 @@ -namespace IdentitySample.BusinessLayer.Settings; +namespace IdentitySample.Authentication.Settings; public class JwtSettings { @@ -11,4 +11,4 @@ public class JwtSettings public int AccessTokenExpirationMinutes { get; init; } public int RefreshTokenExpirationMinutes { get; init; } -} +} \ No newline at end of file diff --git a/IdentitySample/IdentitySample.Authorization/Filters/RoleAuthorizeAttribute.cs b/IdentitySample/IdentitySample.Authorization/Filters/RoleAuthorizeAttribute.cs new file mode 100644 index 0000000..0f48c4a --- /dev/null +++ b/IdentitySample/IdentitySample.Authorization/Filters/RoleAuthorizeAttribute.cs @@ -0,0 +1,11 @@ +using Microsoft.AspNetCore.Authorization; + +namespace IdentitySample.Authorization.Filters; + +public class RoleAuthorizeAttribute : AuthorizeAttribute +{ + public RoleAuthorizeAttribute(params string[] roles) + { + Roles = string.Join(",", roles); + } +} \ No newline at end of file diff --git a/IdentitySample/IdentitySample.Authentication/Requirements/MinimumAgeHandler.cs b/IdentitySample/IdentitySample.Authorization/Handlers/MinimumAgeHandler.cs similarity index 74% rename from IdentitySample/IdentitySample.Authentication/Requirements/MinimumAgeHandler.cs rename to IdentitySample/IdentitySample.Authorization/Handlers/MinimumAgeHandler.cs index 616accc..d371e25 100644 --- a/IdentitySample/IdentitySample.Authentication/Requirements/MinimumAgeHandler.cs +++ b/IdentitySample/IdentitySample.Authorization/Handlers/MinimumAgeHandler.cs @@ -1,6 +1,8 @@ -using Microsoft.AspNetCore.Authorization; +using IdentitySample.Authentication; +using IdentitySample.Authorization.Requirements; +using Microsoft.AspNetCore.Authorization; -namespace IdentitySample.Authentication.Requirements; +namespace IdentitySample.Authorization.Handlers; public class MinimumAgeHandler : AuthorizationHandler { diff --git a/IdentitySample/IdentitySample.Authentication/Requirements/UserActiveHandler.cs b/IdentitySample/IdentitySample.Authorization/Handlers/UserActiveHandler.cs similarity index 85% rename from IdentitySample/IdentitySample.Authentication/Requirements/UserActiveHandler.cs rename to IdentitySample/IdentitySample.Authorization/Handlers/UserActiveHandler.cs index 2127179..f2dae31 100644 --- a/IdentitySample/IdentitySample.Authentication/Requirements/UserActiveHandler.cs +++ b/IdentitySample/IdentitySample.Authorization/Handlers/UserActiveHandler.cs @@ -1,10 +1,11 @@ -using System.Security.Claims; -using IdentitySample.Authentication.Entities; +using IdentitySample.Authentication.Entities; using IdentitySample.Authentication.Extensions; +using IdentitySample.Authorization.Requirements; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Identity; +using System.Security.Claims; -namespace IdentitySample.Authentication.Requirements; +namespace IdentitySample.Authorization.Handlers; public class UserActiveHandler : AuthorizationHandler { diff --git a/IdentitySample/IdentitySample.Authorization/IdentitySample.Authorization.csproj b/IdentitySample/IdentitySample.Authorization/IdentitySample.Authorization.csproj new file mode 100644 index 0000000..9cf1083 --- /dev/null +++ b/IdentitySample/IdentitySample.Authorization/IdentitySample.Authorization.csproj @@ -0,0 +1,16 @@ + + + + net6.0 + enable + + + + + + + + + + + diff --git a/IdentitySample/IdentitySample.Authentication/Requirements/MinimumAgeRequirement.cs b/IdentitySample/IdentitySample.Authorization/Requirements/MinimumAgeRequirement.cs similarity index 81% rename from IdentitySample/IdentitySample.Authentication/Requirements/MinimumAgeRequirement.cs rename to IdentitySample/IdentitySample.Authorization/Requirements/MinimumAgeRequirement.cs index 4393c51..2a82d80 100644 --- a/IdentitySample/IdentitySample.Authentication/Requirements/MinimumAgeRequirement.cs +++ b/IdentitySample/IdentitySample.Authorization/Requirements/MinimumAgeRequirement.cs @@ -1,6 +1,6 @@ using Microsoft.AspNetCore.Authorization; -namespace IdentitySample.Authentication.Requirements; +namespace IdentitySample.Authorization.Requirements; public class MinimumAgeRequirement : IAuthorizationRequirement { diff --git a/IdentitySample/IdentitySample.Authentication/Requirements/UserActiveRequirement.cs b/IdentitySample/IdentitySample.Authorization/Requirements/UserActiveRequirement.cs similarity index 67% rename from IdentitySample/IdentitySample.Authentication/Requirements/UserActiveRequirement.cs rename to IdentitySample/IdentitySample.Authorization/Requirements/UserActiveRequirement.cs index 7089fe4..96c3426 100644 --- a/IdentitySample/IdentitySample.Authentication/Requirements/UserActiveRequirement.cs +++ b/IdentitySample/IdentitySample.Authorization/Requirements/UserActiveRequirement.cs @@ -1,6 +1,6 @@ using Microsoft.AspNetCore.Authorization; -namespace IdentitySample.Authentication.Requirements; +namespace IdentitySample.Authorization.Requirements; public class UserActiveRequirement : IAuthorizationRequirement { diff --git a/IdentitySample/IdentitySample.BusinessLayer/IdentitySample.BusinessLayer.csproj b/IdentitySample/IdentitySample.BusinessLayer/IdentitySample.BusinessLayer.csproj index 135cba1..ffa7745 100644 --- a/IdentitySample/IdentitySample.BusinessLayer/IdentitySample.BusinessLayer.csproj +++ b/IdentitySample/IdentitySample.BusinessLayer/IdentitySample.BusinessLayer.csproj @@ -9,7 +9,7 @@ - + diff --git a/IdentitySample/IdentitySample.BusinessLayer/Services/IdentityService.cs b/IdentitySample/IdentitySample.BusinessLayer/Services/IdentityService.cs index 04e5782..c02edf9 100644 --- a/IdentitySample/IdentitySample.BusinessLayer/Services/IdentityService.cs +++ b/IdentitySample/IdentitySample.BusinessLayer/Services/IdentityService.cs @@ -1,16 +1,16 @@ -using System.IdentityModel.Tokens.Jwt; -using System.Security.Claims; -using System.Security.Cryptography; -using System.Text; -using IdentitySample.Authentication; +using IdentitySample.Authentication; using IdentitySample.Authentication.Entities; using IdentitySample.Authentication.Extensions; -using IdentitySample.BusinessLayer.Settings; +using IdentitySample.Authentication.Settings; using IdentitySample.Contracts; using IdentitySample.Shared.Models; using Microsoft.AspNetCore.Identity; using Microsoft.Extensions.Options; using Microsoft.IdentityModel.Tokens; +using System.IdentityModel.Tokens.Jwt; +using System.Security.Claims; +using System.Security.Cryptography; +using System.Text; namespace IdentitySample.BusinessLayer.Services; diff --git a/IdentitySample/IdentitySample.DataAccessLayer/IdentitySample.DataAccessLayer.csproj b/IdentitySample/IdentitySample.DataAccessLayer/IdentitySample.DataAccessLayer.csproj index cd280d3..99e3245 100644 --- a/IdentitySample/IdentitySample.DataAccessLayer/IdentitySample.DataAccessLayer.csproj +++ b/IdentitySample/IdentitySample.DataAccessLayer/IdentitySample.DataAccessLayer.csproj @@ -6,7 +6,7 @@ - + diff --git a/IdentitySample/IdentitySample.StorageProviders/IdentitySample.StorageProviders.csproj b/IdentitySample/IdentitySample.StorageProviders/IdentitySample.StorageProviders.csproj index 7211c3c..f8dab10 100644 --- a/IdentitySample/IdentitySample.StorageProviders/IdentitySample.StorageProviders.csproj +++ b/IdentitySample/IdentitySample.StorageProviders/IdentitySample.StorageProviders.csproj @@ -7,7 +7,7 @@ - + diff --git a/IdentitySample/IdentitySample.sln b/IdentitySample/IdentitySample.sln index db49388..04d1ec9 100644 --- a/IdentitySample/IdentitySample.sln +++ b/IdentitySample/IdentitySample.sln @@ -21,7 +21,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IdentitySample.DataAccessLa EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IdentitySample.Contracts", "IdentitySample.Contracts\IdentitySample.Contracts.csproj", "{F01DED0D-AE45-444C-B419-7346CDD6890B}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IdentitySample.StorageProviders", "IdentitySample.StorageProviders\IdentitySample.StorageProviders.csproj", "{A7CA3DBA-96B5-458F-ADB8-D88A18D06B70}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IdentitySample.StorageProviders", "IdentitySample.StorageProviders\IdentitySample.StorageProviders.csproj", "{A7CA3DBA-96B5-458F-ADB8-D88A18D06B70}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IdentitySample.Authorization", "IdentitySample.Authorization\IdentitySample.Authorization.csproj", "{427D3DE5-C6BD-4F4B-A2FE-3FB33F278713}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -69,6 +71,10 @@ Global {A7CA3DBA-96B5-458F-ADB8-D88A18D06B70}.Debug|Any CPU.Build.0 = Debug|Any CPU {A7CA3DBA-96B5-458F-ADB8-D88A18D06B70}.Release|Any CPU.ActiveCfg = Release|Any CPU {A7CA3DBA-96B5-458F-ADB8-D88A18D06B70}.Release|Any CPU.Build.0 = Release|Any CPU + {427D3DE5-C6BD-4F4B-A2FE-3FB33F278713}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {427D3DE5-C6BD-4F4B-A2FE-3FB33F278713}.Debug|Any CPU.Build.0 = Debug|Any CPU + {427D3DE5-C6BD-4F4B-A2FE-3FB33F278713}.Release|Any CPU.ActiveCfg = Release|Any CPU + {427D3DE5-C6BD-4F4B-A2FE-3FB33F278713}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/IdentitySample/IdentitySample/IdentitySample.csproj b/IdentitySample/IdentitySample/IdentitySample.csproj index 958d5ab..c1d9e9f 100644 --- a/IdentitySample/IdentitySample/IdentitySample.csproj +++ b/IdentitySample/IdentitySample/IdentitySample.csproj @@ -8,12 +8,13 @@ - - + + + diff --git a/IdentitySample/IdentitySample/Program.cs b/IdentitySample/IdentitySample/Program.cs index 1f739e4..3c6ff17 100644 --- a/IdentitySample/IdentitySample/Program.cs +++ b/IdentitySample/IdentitySample/Program.cs @@ -1,10 +1,9 @@ -using System.Reflection; -using System.Text; using IdentitySample.Authentication; using IdentitySample.Authentication.Entities; -using IdentitySample.Authentication.Requirements; +using IdentitySample.Authentication.Settings; +using IdentitySample.Authorization.Handlers; +using IdentitySample.Authorization.Requirements; using IdentitySample.BusinessLayer.Services; -using IdentitySample.BusinessLayer.Settings; using IdentitySample.Contracts; using IdentitySample.DataAccessLayer; using IdentitySample.Services; @@ -17,6 +16,8 @@ using Microsoft.IdentityModel.Tokens; using Microsoft.Net.Http.Headers; using Microsoft.OpenApi.Models; +using System.Reflection; +using System.Text; var builder = WebApplication.CreateBuilder(args); ConfigureServices(builder.Services, builder.Configuration);