DotNetCore 2.1 -> 2.0

Certificate
Kettel instead of IISExpress
This commit is contained in:
2018-10-06 02:05:00 +02:00
parent bb6b5ee4dd
commit a44cd1321a
11 changed files with 57 additions and 26 deletions

View File

@@ -11,7 +11,7 @@ namespace LaDOSE.Api.Controllers
{ {
[Authorize] [Authorize]
[Route("api/[controller]")] [Route("api/[controller]")]
[ApiController] [Produces("application/json")]
public class GameController : ControllerBase public class GameController : ControllerBase
{ {
@@ -23,7 +23,7 @@ namespace LaDOSE.Api.Controllers
} }
// GET api/Config // GET api/Config
[HttpGet] [HttpGet]
public ActionResult<IEnumerable<Game>> Get() public List<Game> Get()
{ {
return _db.Game.ToList(); return _db.Game.ToList();

View File

@@ -9,25 +9,27 @@ using LaDOSE.Api.Services;
using LaDOSE.Entity; using LaDOSE.Entity;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens; using Microsoft.IdentityModel.Tokens;
namespace LaDOSE.Api.Controllers namespace LaDOSE.Api.Controllers
{ {
[Authorize] [Authorize]
[ApiController] [Produces("application/json")]
[Route("[controller]")] [Route("[controller]")]
public class UsersController : ControllerBase public class UsersController : ControllerBase
{ {
private IUserService _userService; private IUserService _userService;
private readonly IConfiguration _configuration;
public UsersController( public UsersController(
IUserService userService IUserService userService,
IConfiguration configuration
) )
{ {
_userService = userService; _userService = userService;
_configuration = configuration;
} }
[AllowAnonymous] [AllowAnonymous]
@@ -56,7 +58,7 @@ namespace LaDOSE.Api.Controllers
return BadRequest(new { message = "Username or password is incorrect" }); return BadRequest(new { message = "Username or password is incorrect" });
var tokenHandler = new JwtSecurityTokenHandler(); var tokenHandler = new JwtSecurityTokenHandler();
var key = Encoding.ASCII.GetBytes("this is my custom Secret key for authnetication"); var key = Encoding.ASCII.GetBytes(this._configuration["JWTTokenSecret"]);
var tokenDescriptor = new SecurityTokenDescriptor var tokenDescriptor = new SecurityTokenDescriptor
{ {
Subject = new ClaimsIdentity(new Claim[] Subject = new ClaimsIdentity(new Claim[]

View File

@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web"> <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework> <TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
@@ -9,7 +9,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" /> <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.9" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="2.1.2" /> <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="2.1.2" />
</ItemGroup> </ItemGroup>
@@ -17,4 +17,10 @@
<ProjectReference Include="..\LaDOSE.Entity\LaDOSE.Entity.csproj" /> <ProjectReference Include="..\LaDOSE.Entity\LaDOSE.Entity.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Update="localhost.pfx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project> </Project>

View File

@@ -2,6 +2,8 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore; using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
@@ -12,13 +14,30 @@ namespace LaDOSE.Api
{ {
public class Program public class Program
{ {
public static IConfiguration config { get; private set; }
public static void Main(string[] args) public static void Main(string[] args)
{ {
CreateWebHostBuilder(args).Build().Run();
config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appSettings.json", optional: true, reloadOnChange: true)
.Build();
var certificateSettings = config.GetSection("certificateSettings");
string certificateFileName = certificateSettings.GetValue<string>("filename");
string certificatePassword = certificateSettings.GetValue<string>("password");
X509Certificate2 certificate = new X509Certificate2(certificateFileName, certificatePassword);
CreateWebHostBuilder(certificate,args).Build().Run();
} }
public static IWebHostBuilder CreateWebHostBuilder(string[] args) => public static IWebHostBuilder CreateWebHostBuilder(X509Certificate2 certificate, string[] args)
WebHost.CreateDefaultBuilder(args) {
return WebHost.CreateDefaultBuilder(args)
.UseConfiguration(config)
.UseKestrel(options => options.Listen(IPAddress.Loopback,int.Parse(config["Port"]),config=> config.UseHttps(certificate)))
.UseStartup<Startup>(); .UseStartup<Startup>();
} }
}
} }

View File

@@ -9,7 +9,7 @@ using LaDOSE.Entity;
using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy; //using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@@ -36,7 +36,7 @@ namespace LaDOSE.Api
public void ConfigureServices(IServiceCollection services) public void ConfigureServices(IServiceCollection services)
{ {
services.AddCors(); services.AddCors();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); services.AddMvc();
services.AddDbContextPool<LaDOSEDbContext>( // replace "YourDbContext" with the class name of your DbContext services.AddDbContextPool<LaDOSEDbContext>( // replace "YourDbContext" with the class name of your DbContext
options => options.UseMySql("Server=localhost;Database=ladose;User=root;Password=;", // replace with your Connection String options => options.UseMySql("Server=localhost;Database=ladose;User=root;Password=;", // replace with your Connection String
mysqlOptions => mysqlOptions =>
@@ -45,7 +45,7 @@ namespace LaDOSE.Api
} }
)); ));
var key = Encoding.ASCII.GetBytes("this is my custom Secret key for authnetication"); var key = Encoding.ASCII.GetBytes(this.Configuration["JWTTokenSecret"]);
services.AddAuthentication(x => services.AddAuthentication(x =>
{ {
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
@@ -95,17 +95,14 @@ namespace LaDOSE.Api
{ {
app.UseDeveloperExceptionPage(); app.UseDeveloperExceptionPage();
} }
else
{
app.UseHsts();
}
app.UseCors(x => x app.UseCors(x => x
.AllowAnyOrigin() .AllowAnyOrigin()
.AllowAnyMethod() .AllowAnyMethod()
.AllowAnyHeader() .AllowAnyHeader()
.AllowCredentials()); .AllowCredentials());
app.UseHttpsRedirection(); //app.UseHttpsRedirection();
app.UseAuthentication(); app.UseAuthentication();
app.UseMvc(); app.UseMvc();
} }

View File

@@ -4,5 +4,11 @@
"Default": "Warning" "Default": "Warning"
} }
}, },
"AllowedHosts": "*" "certificateSettings": {
"fileName": "localhost.pfx",
"password": "YourSecurePassword"
},
"AllowedHosts": "*",
"Port": 5000,
"JWTTokenSecret": "here goes the custom Secret key for authnetication"
} }

View File

@@ -2,7 +2,7 @@
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework> <TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@@ -24,6 +24,7 @@ namespace LaDOSE.DiscordBot
static async Task MainAsync(string[] args) static async Task MainAsync(string[] args)
{ {
Console.WriteLine(Directory.GetCurrentDirectory());
var builder = new ConfigurationBuilder() var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory()) .SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("settings.json", optional: true, reloadOnChange: true).Build(); .AddJsonFile("settings.json", optional: true, reloadOnChange: true).Build();

View File

@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework> <TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup> </PropertyGroup>
</Project> </Project>

View File

@@ -7,7 +7,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LaDOSE.DiscordBot", "LaDOSE
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LaDOSE.Api", "LaDOSE.Api\LaDOSE.Api.csproj", "{4D7ED4CC-F78F-4860-B8CE-DA01DCACCBB9}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LaDOSE.Api", "LaDOSE.Api\LaDOSE.Api.csproj", "{4D7ED4CC-F78F-4860-B8CE-DA01DCACCBB9}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LaDOSE.Entity", "LaDOSE.Entity\LaDOSE.Entity.csproj", "{B32A4AD5-9A4B-4D12-AAD5-55541F170E2A}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LaDOSE.Entity", "LaDOSE.Entity\LaDOSE.Entity.csproj", "{B32A4AD5-9A4B-4D12-AAD5-55541F170E2A}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution

Binary file not shown.