2018-10-04 21:13:32 +02:00
using System ;
using System.Collections.Generic ;
using System.Linq ;
2018-10-05 01:51:23 +02:00
using System.Text ;
2018-10-04 21:13:32 +02:00
using System.Threading.Tasks ;
2018-10-05 00:18:37 +02:00
using LaDOSE.Api.Context ;
2018-10-05 01:51:23 +02:00
using LaDOSE.Api.Services ;
using LaDOSE.Entity ;
using Microsoft.AspNetCore.Authentication.JwtBearer ;
2018-10-04 21:13:32 +02:00
using Microsoft.AspNetCore.Builder ;
using Microsoft.AspNetCore.Hosting ;
2018-10-06 02:05:00 +02:00
//using Microsoft.AspNetCore.HttpsPolicy;
2018-10-05 01:51:23 +02:00
using Microsoft.AspNetCore.Identity ;
2018-10-04 21:13:32 +02:00
using Microsoft.AspNetCore.Mvc ;
2018-10-05 00:18:37 +02:00
using Microsoft.EntityFrameworkCore ;
2018-10-04 21:13:32 +02:00
using Microsoft.Extensions.Configuration ;
using Microsoft.Extensions.DependencyInjection ;
using Microsoft.Extensions.Logging ;
using Microsoft.Extensions.Options ;
2018-10-05 01:51:23 +02:00
using Microsoft.IdentityModel.Tokens ;
2018-10-05 00:18:37 +02:00
using Pomelo.EntityFrameworkCore.MySql ;
using Pomelo.EntityFrameworkCore.MySql.Infrastructure ;
2018-10-04 21:13:32 +02:00
namespace LaDOSE.Api
{
public class Startup
{
public Startup ( IConfiguration configuration )
{
Configuration = configuration ;
}
public IConfiguration Configuration { get ; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices ( IServiceCollection services )
{
2018-10-06 02:55:29 +02:00
var MySqlServer = this . Configuration [ "MySql:Server" ] ;
var MySqlDatabase = this . Configuration [ "MySql:Database" ] ;
var MySqlUser = this . Configuration [ "MySql:User" ] ;
var MySqlPassword = this . Configuration [ "MySql:Password" ] ;
2018-10-05 01:51:23 +02:00
services . AddCors ( ) ;
2018-10-06 02:05:00 +02:00
services . AddMvc ( ) ;
2018-10-05 00:18:37 +02:00
services . AddDbContextPool < LaDOSEDbContext > ( // replace "YourDbContext" with the class name of your DbContext
2018-10-06 02:55:29 +02:00
options = > options . UseMySql ( $"Server={MySqlServer};Database={MySqlDatabase};{MySqlUser}=root;Password={MySqlPassword};" , // replace with your Connection String
2018-10-05 00:18:37 +02:00
mysqlOptions = >
{
mysqlOptions . ServerVersion ( new Version ( 10 , 1 , 16 ) , ServerType . MariaDb ) ; // replace with your Server Version and Type
}
) ) ;
2018-10-05 01:51:23 +02:00
2018-10-06 02:05:00 +02:00
var key = Encoding . ASCII . GetBytes ( this . Configuration [ "JWTTokenSecret" ] ) ;
2018-10-05 01:51:23 +02:00
services . AddAuthentication ( x = >
{
x . DefaultAuthenticateScheme = JwtBearerDefaults . AuthenticationScheme ;
x . DefaultChallengeScheme = JwtBearerDefaults . AuthenticationScheme ;
} )
. AddJwtBearer ( x = >
{
x . Events = new JwtBearerEvents
{
OnTokenValidated = context = >
{
var userService = context . HttpContext . RequestServices . GetRequiredService < IUserService > ( ) ;
var userId = int . Parse ( context . Principal . Identity . Name ) ;
var user = userService . GetById ( userId ) ;
if ( user = = null )
{
// return unauthorized if user no longer exists
context . Fail ( "Unauthorized" ) ;
}
return Task . CompletedTask ;
}
} ;
x . RequireHttpsMetadata = false ;
x . SaveToken = true ;
x . TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true ,
IssuerSigningKey = new SymmetricSecurityKey ( key ) ,
ValidateIssuer = false ,
ValidateAudience = false
} ;
} ) ;
// configure DI for application services
services . AddScoped < IUserService , UserService > ( ) ;
2018-10-04 21:13:32 +02:00
}
2018-10-05 00:18:37 +02:00
2018-10-04 21:13:32 +02:00
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
2018-10-05 01:51:23 +02:00
public void Configure ( IApplicationBuilder app , IHostingEnvironment env , ILoggerFactory loggerFactory )
2018-10-04 21:13:32 +02:00
{
2018-10-05 01:51:23 +02:00
loggerFactory . AddConsole ( Configuration . GetSection ( "Logging" ) ) ;
loggerFactory . AddDebug ( ) ;
2018-10-04 21:13:32 +02:00
if ( env . IsDevelopment ( ) )
{
app . UseDeveloperExceptionPage ( ) ;
}
2018-10-06 02:05:00 +02:00
2018-10-05 01:51:23 +02:00
app . UseCors ( x = > x
. AllowAnyOrigin ( )
. AllowAnyMethod ( )
. AllowAnyHeader ( )
. AllowCredentials ( ) ) ;
2018-10-04 21:13:32 +02:00
2018-10-06 02:05:00 +02:00
//app.UseHttpsRedirection();
2018-10-05 01:51:23 +02:00
app . UseAuthentication ( ) ;
2018-10-04 21:13:32 +02:00
app . UseMvc ( ) ;
}
}
}