Skip to content

Commit

Permalink
Update to .Net Core 3.0 (#390)
Browse files Browse the repository at this point in the history
Update to .Net Core 3.0
  • Loading branch information
skoruba authored Nov 10, 2019
2 parents 985f972 + e06301b commit b3e7421
Show file tree
Hide file tree
Showing 39 changed files with 335 additions and 290 deletions.
31 changes: 21 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

This is currently in **beta version**

The application is written in the **Asp.Net Core MVC - using .NET Core 2.2**
The application is written in the **Asp.Net Core MVC - using .NET Core 3.0**

**NOTE:** Works only with **IdentityServer4 version 2.3.0 and higher** 🚀
**NOTE:** Works only with **IdentityServer4 version 3.0.0 and higher** 🚀

## Requirements

Expand Down Expand Up @@ -147,6 +147,13 @@ Add-Migration IdentityServerPersistedGrantsDbInit -context IdentityServerPersist
Update-Database -context IdentityServerPersistedGrantDbContext
```

#### Migrations for AuditLogging DbContext:

```powershell
Add-Migration AuditLoggingDbInit -context AuditLoggingDbContext -output Data/Migrations/AuditLogging
Update-Database -context AuditLoggingDbContext
```

### Or via `dotnet CLI`:

#### Migrations for Asp.Net Core Identity DbContext:
Expand Down Expand Up @@ -177,23 +184,29 @@ dotnet ef migrations add IdentityServerPersistedGrantsDbInit -c IdentityServerPe
dotnet ef database update -c IdentityServerPersistedGrantDbContext
```

#### Migrations for AuditLogging DbContext:

```powershell
dotnet ef migrations add AuditLoggingDbInit -c AuditLoggingDbContext -o Data/Migrations/AuditLogging
dotnet ef database update -c AuditLoggingDbContext
```

Migrations are not a part of the repository - they are ignored in `.gitignore`.

### We suggest to use seed data:

- In `Program.cs` -> `Main`, uncomment `DbMigrationHelpers.EnsureSeedData(host)` or use dotnet CLI `dotnet run /seed`
- The `Clients` and `Resources` files in `Configuration/IdentityServer` are the initial data, based on a sample from IdentityServer4
- The `Users` file in `Configuration/Identity` contains the default admin username and password for the first login
- The `Clients` and `Resources` files in `appsettings.json` (section called: IdentityServerData) - are the initial data, based on a sample from IdentityServer4
- The `Users` file in `appsettings.json` (section called: IdentityData) contains the default admin username and password for the first login

### Using other database engines - PostgreSQL, SQLite, MySQL etc.

- [Follow these steps for setup other database engines](docs/EFMigration.md)

## Authentication and Authorization

- Change the specific URLs and names for the IdentityServer and Authentication settings in `Constants/AuthenticationConsts` or `appsettings.json`
- `Constants/AuthorizationConsts.cs` contains configuration of constants connected with authorization - definition of the default name of admin policy
- In the controllers is used the policy which name is stored in - `AuthorizationConsts.AdministrationPolicy`. In the policy - `AuthorizationConsts.AdministrationPolicy` is defined required role stored in - `AuthorizationConsts.AdministrationRole`.
- Change the specific URLs and names for the IdentityServer and Authentication settings in `appsettings.json`
- In the controllers is used the policy which name is stored in - `AuthorizationConsts.AdministrationPolicy`. In the policy - `AuthorizationConsts.AdministrationPolicy` is defined required role stored in - `appsettings.json` - `AdministrationRole`.
- With the default configuration, it is necessary to configure and run instance of IdentityServer4. It is possible to use initial migration for creating the client as it mentioned above

### Login Configuration
Expand Down Expand Up @@ -447,9 +460,7 @@ It is possible to define the configuration according the client type - by defaul
- [x] IdentityServer4
- [x] Asp.Net Core Identity
- [x] Add swagger support

### 1.1.0
- [ ] Add audit logs to track changes ([#61](https://github.com/skoruba/IdentityServer4.Admin/issues/61))
[x] Add audit logs to track changes ([#61](https://github.com/skoruba/IdentityServer4.Admin/issues/61))

### 2.0.0:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public ApiAuditSubject(IHttpContextAccessor accessor, AuditLoggingConfiguration
var clientIdClaim = accessor.HttpContext.User.FindFirst(auditLoggingConfiguration.ClientIdClaim);

SubjectIdentifier = subClaim == null ? clientIdClaim.Value : subClaim.Value;
SubjectName = subClaim == null ? clientIdClaim.Value : nameClaim.Value;
SubjectName = subClaim == null ? clientIdClaim.Value : nameClaim?.Value;
SubjectType = subClaim == null ? AuditSubjectTypes.Machine : AuditSubjectTypes.User;

SubjectAdditionalData = new
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Authorization;
using Swashbuckle.AspNetCore.Swagger;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;

namespace Skoruba.IdentityServer4.Admin.Api.Configuration.Authorization
Expand All @@ -14,20 +14,28 @@ public AuthorizeCheckOperationFilter(AdminApiConfiguration adminApiConfiguration
{
_adminApiConfiguration = adminApiConfiguration;
}

public void Apply(Operation operation, OperationFilterContext context)
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
var hasAuthorize = context.MethodInfo.DeclaringType.GetCustomAttributes(true)
.Union(context.MethodInfo.GetCustomAttributes(true))
.OfType<AuthorizeAttribute>().Any();

if (hasAuthorize)
{
operation.Responses.Add("401", new Response { Description = "Unauthorized" });
operation.Responses.Add("403", new Response { Description = "Forbidden" });

operation.Security = new List<IDictionary<string, IEnumerable<string>>> {
new Dictionary<string, IEnumerable<string>> {{"oauth2", new[] { _adminApiConfiguration.OidcApiName } }}
operation.Responses.Add("401", new OpenApiResponse { Description = "Unauthorized" });
operation.Responses.Add("403", new OpenApiResponse { Description = "Forbidden" });
var oAuthScheme = new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "oauth2"
}
};
operation.Security = new List<OpenApiSecurityRequirement> {
new OpenApiSecurityRequirement {
[oAuthScheme] = new[] { _adminApiConfiguration.OidcApiName }
}
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ public static IServiceCollection AddAuditEventLogging<TAuditLoggingDbContext, TA
{
services.TryAddTransient(typeof(IGenericControllerLocalizer<>), typeof(GenericControllerLocalizer<>));

services.AddMvc(o => { o.Conventions.Add(new GenericControllerRouteConvention()); })
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
services.AddControllersWithViews(o => { o.Conventions.Add(new GenericControllerRouteConvention()); })
.AddDataAnnotationsLocalization()
.ConfigureApplicationPartManager(m =>
{
Expand Down
1 change: 1 addition & 0 deletions src/Skoruba.IdentityServer4.Admin.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public static void Main(string[] args)

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseIISIntegration()
.UseStartup<Startup>();
}
}
Original file line number Diff line number Diff line change
@@ -1,44 +1,46 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
<UserSecretsId>1cc472a2-4e4b-48ce-846b-5219f71fc643</UserSecretsId>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<UserSecretsId>1cc472a2-4e4b-48ce-846b-5219f71fc643</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoMapper" Version="9.0.0" />
<PackageReference Include="IdentityServer4.AccessTokenValidation" Version="2.7.0" />
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="4.0.1" />
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="4.0.1" />
<PackageReference Include="Serilog.AspNetCore" Version="3.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.1.0" />
<PackageReference Include="Serilog.Sinks.File" Version="4.0.0" />
<PackageReference Include="Serilog.Sinks.MSSqlServer" Version="5.1.3-dev-00236" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="3.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.0.0" />
<PackageReference Include="AutoMapper" Version="9.0.0" />
<PackageReference Include="IdentityServer4.AccessTokenValidation" Version="3.0.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0-rc4" />
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="5.0.0-rc4" />
<PackageReference Include="Serilog.AspNetCore" Version="3.1.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.1.0" />
<PackageReference Include="Serilog.Sinks.File" Version="4.1.0" />
<PackageReference Include="Serilog.Sinks.MSSqlServer" Version="5.1.3" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Skoruba.IdentityServer4.Admin.BusinessLogic.Identity\Skoruba.IdentityServer4.Admin.BusinessLogic.Identity.csproj" />
<ProjectReference Include="..\Skoruba.IdentityServer4.Admin.BusinessLogic.Shared\Skoruba.IdentityServer4.Admin.BusinessLogic.Shared.csproj" />
<ProjectReference Include="..\Skoruba.IdentityServer4.Admin.BusinessLogic\Skoruba.IdentityServer4.Admin.BusinessLogic.csproj" />
<ProjectReference Include="..\Skoruba.IdentityServer4.Admin.EntityFramework.Shared\Skoruba.IdentityServer4.Admin.EntityFramework.Shared.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Skoruba.IdentityServer4.Admin.BusinessLogic.Identity\Skoruba.IdentityServer4.Admin.BusinessLogic.Identity.csproj" />
<ProjectReference Include="..\Skoruba.IdentityServer4.Admin.BusinessLogic.Shared\Skoruba.IdentityServer4.Admin.BusinessLogic.Shared.csproj" />
<ProjectReference Include="..\Skoruba.IdentityServer4.Admin.BusinessLogic\Skoruba.IdentityServer4.Admin.BusinessLogic.csproj" />
<ProjectReference Include="..\Skoruba.IdentityServer4.Admin.EntityFramework.Shared\Skoruba.IdentityServer4.Admin.EntityFramework.Shared.csproj" />
</ItemGroup>

<ItemGroup>
<Compile Update="Resources\ApiErrorResource.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>ApiErrorResource.resx</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<Compile Update="Resources\ApiErrorResource.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>ApiErrorResource.resx</DependentUpon>
</Compile>
</ItemGroup>

<ItemGroup>
<EmbeddedResource Update="Resources\ApiErrorResource.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>ApiErrorResource.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Update="Resources\ApiErrorResource.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>ApiErrorResource.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>

</Project>
32 changes: 20 additions & 12 deletions src/Skoruba.IdentityServer4.Admin.Api/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
using Skoruba.AuditLogging.EntityFramework.Entities;
using Skoruba.IdentityServer4.Admin.Api.Configuration;
using Skoruba.IdentityServer4.Admin.Api.Configuration.Authorization;
Expand All @@ -20,7 +22,7 @@ namespace Skoruba.IdentityServer4.Admin.Api
{
public class Startup
{
public Startup(IHostingEnvironment env)
public Startup(IWebHostEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
Expand All @@ -40,7 +42,7 @@ public Startup(IHostingEnvironment env)

public IConfiguration Configuration { get; }

public IHostingEnvironment HostingEnvironment { get; }
public IWebHostEnvironment HostingEnvironment { get; }

public void ConfigureServices(IServiceCollection services)
{
Expand Down Expand Up @@ -77,24 +79,29 @@ public void ConfigureServices(IServiceCollection services)

services.AddSwaggerGen(options =>
{
options.SwaggerDoc(adminApiConfiguration.ApiVersion, new Info { Title = adminApiConfiguration.ApiName, Version = adminApiConfiguration.ApiVersion });
options.SwaggerDoc(adminApiConfiguration.ApiVersion, new OpenApiInfo { Title = adminApiConfiguration.ApiName, Version = adminApiConfiguration.ApiVersion });

options.AddSecurityDefinition("oauth2", new OAuth2Scheme
options.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
{
Flow = "implicit",
AuthorizationUrl = $"{adminApiConfiguration.IdentityServerBaseUrl}/connect/authorize",
Scopes = new Dictionary<string, string> {
{ adminApiConfiguration.OidcApiName, adminApiConfiguration.ApiName }
Type = SecuritySchemeType.OAuth2,
Flows = new OpenApiOAuthFlows
{
Implicit = new OpenApiOAuthFlow
{
AuthorizationUrl = new Uri($"{adminApiConfiguration.IdentityServerBaseUrl}/connect/authorize"),
Scopes = new Dictionary<string, string> {
{ adminApiConfiguration.OidcApiName, adminApiConfiguration.ApiName }
}
}
}
});

options.OperationFilter<AuthorizeCheckOperationFilter>();
});

services.AddAuditEventLogging<AdminAuditLogDbContext, AuditLog>(Configuration);
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, AdminApiConfiguration adminApiConfiguration)
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, AdminApiConfiguration adminApiConfiguration)
{
app.AddLogging(Configuration);

Expand All @@ -104,7 +111,6 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, AdminApi
}

app.UseAuthentication();

app.UseSwagger();
app.UseSwaggerUI(c =>
{
Expand All @@ -114,7 +120,9 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, AdminApi
c.OAuthAppName(adminApiConfiguration.ApiName);
});

app.UseMvc();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints => { endpoints.MapDefaultControllerRoute(); });
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ public IdentityMapperProfile()
CreateMap<TUserLogin, TUserProviderDto>(MemberList.Destination);

// model to entity
CreateMap<TRoleDto, TRole>(MemberList.Source);
CreateMap<TRoleDto, TRole>(MemberList.Source)
.ForMember(dest => dest.Id, opt => opt.Condition(srs => srs.Id != null)); ;

CreateMap<TRoleClaimsDto, TRoleClaim>(MemberList.Source);

Expand All @@ -103,7 +104,8 @@ public IdentityMapperProfile()
opt => opt.MapFrom(src => src.ClaimId));

// model to entity
CreateMap<TUserDto, TUser>(MemberList.Source);
CreateMap<TUserDto, TUser>(MemberList.Source)
.ForMember(dest => dest.Id, opt => opt.Condition(srs => srs.Id != null)); ;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFramework>netcoreapp3.0</TargetFramework>
<Version>1.0.0-beta7</Version>
<Authors>Jan Škoruba</Authors>
<Description>Business Logic layer for the administration of the Asp.Net Core Identity and IdentityServer4</Description>
Expand All @@ -12,8 +12,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="IdentityServer4.EntityFramework" Version="2.5.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="2.2.0" />
<PackageReference Include="IdentityServer4.EntityFramework" Version="3.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFramework>netcoreapp3.0</TargetFramework>
<Version>1.0.0-beta7</Version>
<Authors>Jan Škoruba</Authors>
<PackageTags>IdentityServer4 Admin OpenIDConnect OAuth2 Identity</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFramework>netcoreapp3.0</TargetFramework>
<Version>1.0.0-beta7</Version>
<Authors>Jan Škoruba</Authors>
<Description>Business Logic layer for the administration of the IdentityServer4</Description>
Expand All @@ -12,7 +12,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="IdentityServer4.EntityFramework" Version="2.5.0" />
<PackageReference Include="IdentityServer4.EntityFramework" Version="3.0.2" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFramework>netcoreapp3.0</TargetFramework>
<Version>1.0.0-beta7</Version>
<Authors>Jan Škoruba</Authors>
<PackageTags>IdentityServer4 Admin OpenIDConnect OAuth2 Identity</PackageTags>
Expand Down
Loading

0 comments on commit b3e7421

Please sign in to comment.