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

View File

@@ -9,25 +9,27 @@ using LaDOSE.Api.Services;
using LaDOSE.Entity;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
namespace LaDOSE.Api.Controllers
{
[Authorize]
[ApiController]
[Produces("application/json")]
[Route("[controller]")]
public class UsersController : ControllerBase
{
private IUserService _userService;
private readonly IConfiguration _configuration;
public UsersController(
IUserService userService
IUserService userService,
IConfiguration configuration
)
{
_userService = userService;
_configuration = configuration;
}
[AllowAnonymous]
@@ -56,7 +58,7 @@ namespace LaDOSE.Api.Controllers
return BadRequest(new { message = "Username or password is incorrect" });
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
{
Subject = new ClaimsIdentity(new Claim[]

View File

@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
@@ -9,7 +9,7 @@
</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" />
</ItemGroup>
@@ -17,4 +17,10 @@
<ProjectReference Include="..\LaDOSE.Entity\LaDOSE.Entity.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="localhost.pfx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View File

@@ -2,6 +2,8 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
@@ -12,13 +14,30 @@ namespace LaDOSE.Api
{
public class Program
{
public static IConfiguration config { get; private set; }
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) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
public static IWebHostBuilder CreateWebHostBuilder(X509Certificate2 certificate, string[] args)
{
return WebHost.CreateDefaultBuilder(args)
.UseConfiguration(config)
.UseKestrel(options => options.Listen(IPAddress.Loopback,int.Parse(config["Port"]),config=> config.UseHttps(certificate)))
.UseStartup<Startup>();
}
}
}

View File

@@ -9,7 +9,7 @@ using LaDOSE.Entity;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
//using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
@@ -36,7 +36,7 @@ namespace LaDOSE.Api
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddMvc();
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
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 =>
{
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
@@ -95,17 +95,14 @@ namespace LaDOSE.Api
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseCors(x => x
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
app.UseHttpsRedirection();
//app.UseHttpsRedirection();
app.UseAuthentication();
app.UseMvc();
}

View File

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