From 8614d18522441543e08c37c68121fed1fa8d6ae7 Mon Sep 17 00:00:00 2001 From: ivarlovlie Date: Sun, 9 Aug 2020 15:51:33 +0200 Subject: auth user --- src/server/Startup.cs | 45 +++++++++++++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 14 deletions(-) (limited to 'src/server/Startup.cs') diff --git a/src/server/Startup.cs b/src/server/Startup.cs index f55a761..891965b 100644 --- a/src/server/Startup.cs +++ b/src/server/Startup.cs @@ -1,3 +1,4 @@ +using System.IO; using Dough.IdentityServer; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; @@ -7,6 +8,9 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Dough.Models; using Dough.Models.Database; +using Dough.Services; +using IdentityServer4.Configuration; +using Microsoft.AspNetCore.DataProtection; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; @@ -20,7 +24,7 @@ namespace Dough } public IConfiguration Configuration { get; } - + private const string DefaultCorsPolicy = "DefaultCorsPolicy"; private string GetConnectionStringFromEnvironment() @@ -35,7 +39,6 @@ namespace Dough public void ConfigureServices(IServiceCollection services) { - services.AddCors(options => { options.AddPolicy(DefaultCorsPolicy, builder => @@ -48,25 +51,34 @@ namespace Dough }); }); + var dataprotectionkeyPath = Path.Combine(Directory.GetCurrentDirectory(), "AppData", "dpkeys"); + services.AddDataProtection().PersistKeysToFileSystem(new DirectoryInfo(dataprotectionkeyPath)); + services.AddHealthChecks() .AddDbContextCheck(); - services.AddDbContext(options => { - options.UseMySql(GetConnectionStringFromEnvironment()); - }); - - services.Configure(options => + services.AddDbContext(options => { - options.SuppressModelStateInvalidFilter = true; - options.SuppressInferBindingSourcesForParameters = true; + options.UseMySql(GetConnectionStringFromEnvironment()); }); - var builder = services.AddIdentityServer() + + services.AddIdentityServer(options => + { + options.UserInteraction = new UserInteractionOptions + { + LoginUrl = "/login", + ErrorUrl = "/error", + }; + }) .AddInMemoryIdentityResources(Config.IdentityResources) .AddInMemoryApiScopes(Config.ApiScopes) + .AddDeveloperSigningCredential() .AddInMemoryClients(Config.Clients); - + services.AddSingleton(); + services.AddControllers(); + services.AddRazorPages().AddRazorRuntimeCompilation(); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) @@ -78,9 +90,14 @@ namespace Dough app.UseCors(DefaultCorsPolicy); app.UseHealthChecks("/health"); app.UseStatusCodePages(); - app.UseAuthentication(); + app.UseIdentityServer(); app.UseAuthorization(); - app.UseEndpoints(endpoints => { endpoints.MapControllers().RequireAuthorization(); }); + app.UseEndpoints(endpoints => + { + endpoints.MapRazorPages(); + endpoints.MapControllers() + .RequireAuthorization(); + }); } } -} +} \ No newline at end of file -- cgit v1.3