From d04978fdaed1fa13c7f4a688ed1911cadea1dba3 Mon Sep 17 00:00:00 2001 From: Tobi Date: Wed, 19 Jun 2019 12:30:32 +0200 Subject: [PATCH] :construction: CookieAuth implementiert --- .../Identity/Data/EVABackendIdentityContext.cs | 7 +-- .../Areas/Identity/IdentityHostingStartup.cs | 18 +++++-- .../EVABackend/Controllers/EVAController.cs | 56 +++++++++++++++++++-- .../EVABackend/Properties/launchSettings.json | 6 +-- Backend/EVABackend/EVABackend/Startup.cs | 9 +--- Backend/EVABackend/EVABackend/eva_users.db | Bin 102400 -> 102400 bytes 6 files changed, 70 insertions(+), 26 deletions(-) diff --git a/Backend/EVABackend/EVABackend/Areas/Identity/Data/EVABackendIdentityContext.cs b/Backend/EVABackend/EVABackend/Areas/Identity/Data/EVABackendIdentityContext.cs index 58342c9..8a3b572 100644 --- a/Backend/EVABackend/EVABackend/Areas/Identity/Data/EVABackendIdentityContext.cs +++ b/Backend/EVABackend/EVABackend/Areas/Identity/Data/EVABackendIdentityContext.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using EVABackend.Areas.Identity.Data; -using Microsoft.AspNetCore.Identity; +using EVABackend.Areas.Identity.Data; using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; diff --git a/Backend/EVABackend/EVABackend/Areas/Identity/IdentityHostingStartup.cs b/Backend/EVABackend/EVABackend/Areas/Identity/IdentityHostingStartup.cs index d1a9c56..4610acd 100644 --- a/Backend/EVABackend/EVABackend/Areas/Identity/IdentityHostingStartup.cs +++ b/Backend/EVABackend/EVABackend/Areas/Identity/IdentityHostingStartup.cs @@ -1,12 +1,12 @@ -using System; -using EVABackend.Areas.Identity.Data; +using EVABackend.Areas.Identity.Data; using EVABackend.Models; +using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Identity; -using Microsoft.AspNetCore.Identity.UI; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using System; [assembly: HostingStartup(typeof(EVABackend.Areas.Identity.IdentityHostingStartup))] namespace EVABackend.Areas.Identity @@ -15,13 +15,21 @@ namespace EVABackend.Areas.Identity { public void Configure(IWebHostBuilder builder) { - builder.ConfigureServices((context, services) => { + builder.ConfigureServices((context, services) => + { services.AddDbContext(options => options.UseSqlite( context.Configuration.GetConnectionString("EVABackendIdentityContextConnection"))); services.AddDefaultIdentity() - .AddEntityFrameworkStores(); + .AddRoles() + .AddEntityFrameworkStores() + .AddDefaultTokenProviders(); + + services.ConfigureApplicationCookie(options => + { + options.Cookie.Name = "EVABackend_Token"; + }); }); } } diff --git a/Backend/EVABackend/EVABackend/Controllers/EVAController.cs b/Backend/EVABackend/EVABackend/Controllers/EVAController.cs index 90b4089..9c70e96 100644 --- a/Backend/EVABackend/EVABackend/Controllers/EVAController.cs +++ b/Backend/EVABackend/EVABackend/Controllers/EVAController.cs @@ -1,26 +1,72 @@ -using EVABackend.Models; +using EVABackend.Areas.Identity.Data; +using EVABackend.Models; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using System.Linq; +using System.Threading.Tasks; namespace EVABackend.Controllers { [ApiController] public class EVAController : ControllerBase { - EVAContext context = new EVAContext(); + private readonly EVAContext _context; + private readonly UserManager _userManager; + private readonly RoleManager _roleManager; + private readonly SignInManager _signInManager; + + public EVAController(UserManager userManager, RoleManager roleManager, SignInManager signInManager) + { + _userManager = userManager; + _roleManager = roleManager; + _signInManager = signInManager; + + _context = new EVAContext(); + } + + [HttpGet] + [Route("login")] + public ActionResult Login() + { + return Ok(new { status = "Nicht unterstützt" }); + } + + [HttpGet] + [Route("create_dummy_data")] + public async Task CreateDummyData() + { + var user = new EVABackendUser + { + UserName = "Test", + Email = "info@test.de" + }; + + if ((await _userManager.CreateAsync(user, "123Abc!&")).Succeeded) + return Ok(); + + return StatusCode(500); + } [HttpPost] [Route("login")] - public ActionResult Login([FromBody] string username, string password) + public async Task Login([FromForm] string username, [FromForm] string password) { - return Ok(new { success = true }); + var result = await _signInManager.PasswordSignInAsync(username, password, false, false); + if (result.Succeeded) + { + return Ok(); + } + + return Unauthorized(); } [HttpGet] [Route("instruments")] + [Authorize] public ActionResult Instruments() { - var instruments = context.Instrumente.ToList(); + var instruments = _context.Instrumente.ToList(); return Ok(instruments.ToArray()); } diff --git a/Backend/EVABackend/EVABackend/Properties/launchSettings.json b/Backend/EVABackend/EVABackend/Properties/launchSettings.json index 6f53cc1..30a47b4 100644 --- a/Backend/EVABackend/EVABackend/Properties/launchSettings.json +++ b/Backend/EVABackend/EVABackend/Properties/launchSettings.json @@ -12,7 +12,7 @@ "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, - "launchUrl": "api/values", + "launchUrl": "login", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } @@ -20,7 +20,7 @@ "EVABackend": { "commandName": "Project", "launchBrowser": true, - "launchUrl": "api/values", + "launchUrl": "login", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" }, @@ -29,7 +29,7 @@ "Docker": { "commandName": "Docker", "launchBrowser": true, - "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/api/values", + "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/login", "httpPort": 50571, "useSSL": true, "sslPort": 44375 diff --git a/Backend/EVABackend/EVABackend/Startup.cs b/Backend/EVABackend/EVABackend/Startup.cs index eb581f2..f68d70a 100644 --- a/Backend/EVABackend/EVABackend/Startup.cs +++ b/Backend/EVABackend/EVABackend/Startup.cs @@ -1,15 +1,10 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; +using EVABackend.Areas.Identity.Data; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.HttpsPolicy; +using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; namespace EVABackend { diff --git a/Backend/EVABackend/EVABackend/eva_users.db b/Backend/EVABackend/EVABackend/eva_users.db index c1fce3ad3ab26e024f999b592e50135f6f93179c..060977427a1f04ad6d5c4590e1033324b473f3be 100644 GIT binary patch delta 417 zcma)&y-vbV7>3VD6D%g?lz~M}oQN@`csTS}I+zq_1AO)z(GZ+zd08UFZ~q>_VZ$w$+9ISUjz{v^h9+yWM(&jB^w>CM~aT`Q=NiS;=cO zt+&!Vdp)>y$`#j0rz>N3U=4=-o4)Px&iOg%lyi<(ELShPdR@0PN@^9`A$6_V*E?04 zght2%lJixD4Pl(0aW%kstWv^oIXp=A<(pp#?Ci~r~8y(Kh%juwux waPYzuD1tW=D(?_O$rQn^09q{*_zUeLDHZ=4$oB3Uptab4BY8(k#y4600yj)?m;e9( delta 31 ncmZozz}B#UZGto-(?l6(My8DkOZ*qJ7$`Ke6l`WO_#+PhoGS_9