2021-05-21 18:32:25 +00:00
using Microsoft.AspNetCore.Authentication ;
using Microsoft.AspNetCore.Builder ;
using Microsoft.AspNetCore.Hosting ;
using Microsoft.EntityFrameworkCore ;
2021-07-10 02:47:40 +00:00
using MultiShop.Data ;
using MultiShop.Models ;
2021-05-21 18:32:25 +00:00
using Microsoft.Extensions.Configuration ;
using Microsoft.Extensions.DependencyInjection ;
using Microsoft.Extensions.Hosting ;
2021-07-10 02:47:40 +00:00
namespace MultiShop
2021-05-21 18:32:25 +00:00
{
public class Startup
{
2021-07-10 02:47:40 +00:00
public Startup ( IConfiguration configuration , IHostEnvironment environment )
2021-05-21 18:32:25 +00:00
{
Configuration = configuration ;
2021-07-10 02:47:40 +00:00
Environment = environment ;
2021-05-21 18:32:25 +00:00
}
public IConfiguration Configuration { get ; }
2021-07-10 02:47:40 +00:00
public IHostEnvironment Environment { get ; }
2021-05-21 18:32:25 +00:00
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices ( IServiceCollection services )
{
2021-07-10 02:47:40 +00:00
services . AddDbContext < ApplicationDbContext > ( options = >
options . UseSqlite (
Configuration . GetConnectionString ( "DefaultConnection" ) ) ) ;
2021-05-21 18:32:25 +00:00
services . AddDatabaseDeveloperPageExceptionFilter ( ) ;
services . AddDefaultIdentity < ApplicationUser > ( options = > options . SignIn . RequireConfirmedAccount = true )
. AddEntityFrameworkStores < ApplicationDbContext > ( ) ;
services . AddIdentityServer ( )
2021-07-10 02:47:40 +00:00
. AddApiAuthorization < ApplicationUser , ApplicationDbContext > (
options = > {
options . Clients . AddIdentityServerSPA ( "MultiShop" , spa = > {
2021-07-12 08:06:13 +00:00
spa . WithRedirectUri ( "/authentication/silent-login-callback.html" ) ;
2021-07-10 02:47:40 +00:00
spa . WithRedirectUri ( "/authentication/login-callback" ) ;
2021-07-12 08:06:13 +00:00
spa . WithLogoutRedirectUri ( "/authentication/logout-callback" ) ;
2021-07-10 02:47:40 +00:00
} ) ;
} ) ;
2021-05-25 23:09:06 +00:00
2021-05-21 18:32:25 +00:00
services . AddAuthentication ( )
. AddIdentityServerJwt ( ) ;
services . AddControllersWithViews ( ) ;
services . AddRazorPages ( ) ;
2021-07-10 02:47:40 +00:00
services . AddSpaStaticFiles ( configuration = >
{
configuration . RootPath = "client/dist" ;
} ) ;
2021-05-21 18:32:25 +00:00
}
// 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 . IsDevelopment ( ) )
{
app . UseDeveloperExceptionPage ( ) ;
app . UseMigrationsEndPoint ( ) ;
}
else
{
app . UseExceptionHandler ( "/Error" ) ;
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app . UseHsts ( ) ;
}
2021-07-10 05:04:54 +00:00
if ( ! env . IsDevelopment ( ) ) app . UseHttpsRedirection ( ) ;
2021-05-21 18:32:25 +00:00
app . UseStaticFiles ( ) ;
2021-07-10 05:04:54 +00:00
if ( ! env . IsDevelopment ( ) ) app . UseSpaStaticFiles ( ) ;
2021-05-21 18:32:25 +00:00
app . UseRouting ( ) ;
app . UseAuthentication ( ) ;
2021-07-10 02:47:40 +00:00
app . UseIdentityServer ( ) ;
2021-05-21 18:32:25 +00:00
app . UseAuthorization ( ) ;
app . UseEndpoints ( endpoints = >
{
2021-07-10 02:47:40 +00:00
endpoints . MapControllerRoute (
name : "default" ,
pattern : "{controller}/{action=Index}/{id?}" ) ;
2021-05-21 18:32:25 +00:00
endpoints . MapRazorPages ( ) ;
2021-07-10 02:47:40 +00:00
} ) ;
app . UseSpa ( spa = >
{
spa . Options . SourcePath = "../client" ; // "May not exist in published applications" - https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.spaservices.spaoptions.sourcepath?view=aspnetcore-5.0#Microsoft_AspNetCore_SpaServices_SpaOptions_SourcePath
if ( env . IsDevelopment ( ) )
{
spa . UseProxyToSpaDevelopmentServer ( Configuration [ "SPA:BaseUri" ] ) ;
}
2021-05-21 18:32:25 +00:00
} ) ;
}
}
}