Browse Source

🚧 Unauthorized implementiert; Bugfixes

develop
Tobi 7 years ago
parent
commit
9fe38b53cb
No known key found for this signature in database
GPG Key ID: 187C1244EA024329
  1. 10
      Backend/EVABackend/EVABackend/Areas/Identity/IdentityHostingStartup.cs
  2. 37
      Backend/EVABackend/EVABackend/Controllers/EVAController.cs
  3. 6
      Backend/EVABackend/EVABackend/Startup.cs
  4. BIN
      Backend/EVABackend/EVABackend/eva_users.db

10
Backend/EVABackend/EVABackend/Areas/Identity/IdentityHostingStartup.cs

@ -1,12 +1,10 @@
using EVABackend.Areas.Identity.Data; using EVABackend.Areas.Identity.Data;
using EVABackend.Models; using EVABackend.Models;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using System;
[assembly: HostingStartup(typeof(EVABackend.Areas.Identity.IdentityHostingStartup))] [assembly: HostingStartup(typeof(EVABackend.Areas.Identity.IdentityHostingStartup))]
namespace EVABackend.Areas.Identity namespace EVABackend.Areas.Identity
@ -18,10 +16,9 @@ namespace EVABackend.Areas.Identity
builder.ConfigureServices((context, services) => builder.ConfigureServices((context, services) =>
{ {
services.AddDbContext<EVABackendIdentityContext>(options => services.AddDbContext<EVABackendIdentityContext>(options =>
options.UseSqlite( options.UseSqlite(context.Configuration.GetConnectionString("EVABackendIdentityContextConnection")));
context.Configuration.GetConnectionString("EVABackendIdentityContextConnection")));
services.AddDefaultIdentity<EVABackendUser>() services.AddIdentity<EVABackendUser, IdentityRole>()
.AddRoles<IdentityRole>() .AddRoles<IdentityRole>()
.AddEntityFrameworkStores<EVABackendIdentityContext>() .AddEntityFrameworkStores<EVABackendIdentityContext>()
.AddDefaultTokenProviders(); .AddDefaultTokenProviders();
@ -29,6 +26,9 @@ namespace EVABackend.Areas.Identity
services.ConfigureApplicationCookie(options => services.ConfigureApplicationCookie(options =>
{ {
options.Cookie.Name = "EVABackend_Token"; options.Cookie.Name = "EVABackend_Token";
options.LoginPath = $"/unauthorized";
options.LogoutPath = $"/unauthorized";
options.AccessDeniedPath = $"/unauthorized";
}); });
}); });
} }

37
Backend/EVABackend/EVABackend/Controllers/EVAController.cs

@ -32,7 +32,27 @@ namespace EVABackend.Controllers
[AllowAnonymous] [AllowAnonymous]
public ActionResult Login() public ActionResult Login()
{ {
return Ok(new { status = "Nicht unterstützt" }); return StatusCode(404, new { status = "Nicht unerstützt" });
}
[HttpGet]
[Route("unauthorized")]
[AllowAnonymous]
public ActionResult GetUnauthorized()
{
return StatusCode(401, new { status = "401 - Unauthorized" });
}
#if DEBUG
[HttpGet]
[Route("login_test")]
[AllowAnonymous]
public async Task<ActionResult> LoginTest()
{
var user = await _userManager.FindByNameAsync("Test");
await _signInManager.SignInAsync(user, true);
return Ok(new { status = "Eingeloggt als Test" });
} }
[HttpGet] [HttpGet]
@ -42,7 +62,6 @@ namespace EVABackend.Controllers
{ {
if (_userManager.FindByNameAsync("Test") == null) if (_userManager.FindByNameAsync("Test") == null)
{ {
var user = new EVABackendUser var user = new EVABackendUser
{ {
UserName = "Test", UserName = "Test",
@ -66,6 +85,7 @@ namespace EVABackend.Controllers
return Ok(); return Ok();
} }
#endif
[HttpPost] [HttpPost]
[Route("login")] [Route("login")]
@ -78,15 +98,18 @@ namespace EVABackend.Controllers
return Ok(); return Ok();
} }
return Unauthorized(); return GetUnauthorized();
} }
[HttpPost] [HttpPost]
#if DEBUG
[HttpGet]
#endif
[Route("logout")] [Route("logout")]
[Authorize] [Authorize]
public async Task<ActionResult> Logout() public async Task<ActionResult> Logout()
{ {
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); await _signInManager.SignOutAsync();
return Ok(); return Ok();
} }
@ -146,7 +169,7 @@ namespace EVABackend.Controllers
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
antrag.Instrumente = instrumente.Select(i => new AntragInstrument { AntragId = antrag.Id, InstrumentId = i.Id }).ToList(); antrag.Instrumente = instrumente.Select(i => new AntragInstrument { AntragId = antrag.Id, InstrumentId = i.Id }).ToList();
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
return Ok(); return Ok();
@ -172,7 +195,7 @@ namespace EVABackend.Controllers
var model = rooms.Select(r => new var model = rooms.Select(r => new
{ {
RaumID = r.Id, RaumID = r.Id,
RaumName= r.Name, RaumName = r.Name,
Instrumente = r.Instrumente.Select(i => new Instrumente = r.Instrumente.Select(i => new
{ {
InstrumentID = i.InstrumentId, InstrumentID = i.InstrumentId,
@ -188,7 +211,7 @@ namespace EVABackend.Controllers
[Authorize(Roles = "Verwaltung")] [Authorize(Roles = "Verwaltung")]
public async Task<ActionResult> CreateRooms(CreateRooms model) public async Task<ActionResult> CreateRooms(CreateRooms model)
{ {
throw new System.NotImplementedException();
} }
} }
} }

6
Backend/EVABackend/EVABackend/Startup.cs

@ -1,7 +1,5 @@
using EVABackend.Areas.Identity.Data; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
@ -33,11 +31,11 @@ namespace EVABackend
else else
{ {
app.UseHsts(); app.UseHsts();
app.UseHttpsRedirection();
} }
app.UseStaticFiles(); app.UseStaticFiles();
app.UseAuthentication(); app.UseAuthentication();
app.UseHttpsRedirection();
app.UseMvc(); app.UseMvc();
} }
} }

BIN
Backend/EVABackend/EVABackend/eva_users.db

Binary file not shown.
Loading…
Cancel
Save