This commit is contained in:
2023-03-27 21:26:30 +02:00
parent a7eebf8f38
commit 9b914b211d
13 changed files with 391 additions and 320 deletions

View File

@@ -1,129 +1,129 @@
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Tokens;
using skydiveLogs_api.DomainBusiness.Interfaces;
using skydiveLogs_api.Ioc;
using skydiveLogs_api.Settings;
using System.IO;
using System.Text;
namespace skydiveLogs_api
{
public class Startup
{
#region Public Constructors
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
#endregion Public Constructors
#region Public Methods
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.EnvironmentName == "Development")
{
app.UseDeveloperExceptionPage();
}
else
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseCors();
app.UseAuthentication();
app.UseMvc();
}
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(options => { options.EnableEndpointRouting = false; })
.SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
services.AddHttpContextAccessor();
// JWT
var jwtSection = Configuration.GetSection("JWT");
services.Configure<JwtSettings>(jwtSection);
var jwtSettings = new JwtSettings();
jwtSection.Bind(jwtSettings);
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.SaveToken = true;
options.TokenValidationParameters = new TokenValidationParameters()
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = jwtSettings.Issuer,
ValidAudience = jwtSettings.Issuer,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtSettings.Passphrase))
};
});
// CORS
var corsSettings = new CorsSettings();
Configuration.GetSection("Cors").Bind(corsSettings);
services.AddCors(options =>
{
options.AddDefaultPolicy(
builder =>
{
builder.WithOrigins(corsSettings.FrontUrl)
.AllowAnyHeader()
.AllowAnyMethod();
});
});
// IoC
var iocService = new IocService(services, Configuration);
iocService.Configure();
// AutoMapper
services.AddAutoMapper(typeof(Mapper.ModelProfile));
CheckAndInitDb(services);
}
#endregion Public Methods
#region Private Methods
private void CheckAndInitDb(IServiceCollection services)
{
string connectionString = Configuration.GetConnectionString("DefaultConnection");
string dbFile = connectionString.Replace("Filename=", string.Empty);
if (!File.Exists(dbFile))
{
var serviceProvider = services.BuildServiceProvider();
var initDbService = serviceProvider.GetRequiredService<IInitDbService>();
initDbService.GenerateDb();
}
}
#endregion Private Methods
#region Public Properties
public IConfiguration Configuration { get; }
#endregion Public Properties
}
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Tokens;
using skydiveLogs_api.DomainBusiness.Interfaces;
using skydiveLogs_api.Ioc;
using skydiveLogs_api.Settings;
using System.IO;
using System.Text;
namespace skydiveLogs_api
{
public class Startup
{
#region Public Constructors
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
#endregion Public Constructors
#region Public Methods
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.EnvironmentName == "Development")
{
app.UseDeveloperExceptionPage();
}
else
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseCors();
app.UseAuthentication();
app.UseMvc();
}
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(options => { options.EnableEndpointRouting = false; });
//.SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
services.AddHttpContextAccessor();
// JWT
var jwtSection = Configuration.GetSection("JWT");
services.Configure<JwtSettings>(jwtSection);
var jwtSettings = new JwtSettings();
jwtSection.Bind(jwtSettings);
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.SaveToken = true;
options.TokenValidationParameters = new TokenValidationParameters()
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = jwtSettings.Issuer,
ValidAudience = jwtSettings.Issuer,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtSettings.Passphrase))
};
});
// CORS
var corsSettings = new CorsSettings();
Configuration.GetSection("Cors").Bind(corsSettings);
services.AddCors(options =>
{
options.AddDefaultPolicy(
builder =>
{
builder.WithOrigins(corsSettings.FrontUrl)
.AllowAnyHeader()
.AllowAnyMethod();
});
});
// IoC
var iocService = new IocService(services, Configuration);
iocService.Configure();
// AutoMapper
services.AddAutoMapper(typeof(Mapper.ModelProfile));
CheckAndInitDb(services);
}
#endregion Public Methods
#region Private Methods
private void CheckAndInitDb(IServiceCollection services)
{
string connectionString = Configuration.GetConnectionString("DefaultConnection");
string dbFile = connectionString.Replace("Filename=", string.Empty);
if (!File.Exists(dbFile))
{
var serviceProvider = services.BuildServiceProvider();
var initDbService = serviceProvider.GetRequiredService<IInitDbService>();
initDbService.GenerateDb();
}
}
#endregion Private Methods
#region Public Properties
public IConfiguration Configuration { get; }
#endregion Public Properties
}
}

View File

@@ -1,34 +1,34 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
<RootNamespace>skydiveLogs_api</RootNamespace>
<UserSecretsId>9d7d268e-ee4d-43a2-a9b7-5b00b516f6f8</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<None Remove="Data\JumpsDb.db" />
</ItemGroup>
<ItemGroup>
<Content Include="Data\JumpsDb.db">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="AutoMapper" Version="10.1.1" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.9" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="5.0.2" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.12.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\skydiveLogs-api.DomainBusiness\skydiveLogs-api.DomainBusiness.csproj" />
<ProjectReference Include="..\skydiveLogs-api.Ioc\skydiveLogs-api.Ioc.csproj" />
<ProjectReference Include="..\skydiveLogs-api.Domain\skydiveLogs-api.Domain.csproj" />
</ItemGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
<RootNamespace>skydiveLogs_api</RootNamespace>
<UserSecretsId>9d7d268e-ee4d-43a2-a9b7-5b00b516f6f8</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<None Remove="Data\JumpsDb.db" />
</ItemGroup>
<ItemGroup>
<Content Include="Data\JumpsDb.db">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="AutoMapper" Version="12.0.0" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.11" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.10" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.25.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\skydiveLogs-api.DomainBusiness\skydiveLogs-api.DomainBusiness.csproj" />
<ProjectReference Include="..\skydiveLogs-api.Ioc\skydiveLogs-api.Ioc.csproj" />
<ProjectReference Include="..\skydiveLogs-api.Domain\skydiveLogs-api.Domain.csproj" />
</ItemGroup>
</Project>

View File

@@ -1,19 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Controller_SelectedScaffolderID>ApiControllerWithActionsScaffolder</Controller_SelectedScaffolderID>
<Controller_SelectedScaffolderCategoryPath>root/Controller</Controller_SelectedScaffolderCategoryPath>
<WebStackScaffolding_ControllerDialogWidth>600</WebStackScaffolding_ControllerDialogWidth>
<WebStackScaffolding_IsLayoutPageSelected>True</WebStackScaffolding_IsLayoutPageSelected>
<WebStackScaffolding_IsPartialViewSelected>False</WebStackScaffolding_IsPartialViewSelected>
<WebStackScaffolding_IsReferencingScriptLibrariesSelected>True</WebStackScaffolding_IsReferencingScriptLibrariesSelected>
<WebStackScaffolding_LayoutPageFile />
<WebStackScaffolding_IsAsyncSelected>False</WebStackScaffolding_IsAsyncSelected>
<NameOfLastUsedPublishProfile>FolderProfile</NameOfLastUsedPublishProfile>
<ShowAllFiles>false</ShowAllFiles>
<ActiveDebugProfile>skydiveLogs_api</ActiveDebugProfile>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DebuggerFlavor>ProjectDebugger</DebuggerFlavor>
</PropertyGroup>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Controller_SelectedScaffolderID>ApiControllerWithActionsScaffolder</Controller_SelectedScaffolderID>
<Controller_SelectedScaffolderCategoryPath>root/Controller</Controller_SelectedScaffolderCategoryPath>
<WebStackScaffolding_ControllerDialogWidth>600</WebStackScaffolding_ControllerDialogWidth>
<WebStackScaffolding_IsLayoutPageSelected>True</WebStackScaffolding_IsLayoutPageSelected>
<WebStackScaffolding_IsPartialViewSelected>False</WebStackScaffolding_IsPartialViewSelected>
<WebStackScaffolding_IsReferencingScriptLibrariesSelected>True</WebStackScaffolding_IsReferencingScriptLibrariesSelected>
<WebStackScaffolding_LayoutPageFile />
<WebStackScaffolding_IsAsyncSelected>False</WebStackScaffolding_IsAsyncSelected>
<NameOfLastUsedPublishProfile>C:\Projects\SkydiveLogs\Back\skydiveLogs-api\Properties\PublishProfiles\FolderProfile.pubxml</NameOfLastUsedPublishProfile>
<ShowAllFiles>false</ShowAllFiles>
<ActiveDebugProfile>skydiveLogs_api</ActiveDebugProfile>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DebuggerFlavor>ProjectDebugger</DebuggerFlavor>
</PropertyGroup>
</Project>