Browse Source

🚧 CookieAuth implementiert

develop
Tobi 7 years ago
parent
commit
d04978fdae
No known key found for this signature in database
GPG Key ID: 187C1244EA024329
  1. 7
      Backend/EVABackend/EVABackend/Areas/Identity/Data/EVABackendIdentityContext.cs
  2. 18
      Backend/EVABackend/EVABackend/Areas/Identity/IdentityHostingStartup.cs
  3. 56
      Backend/EVABackend/EVABackend/Controllers/EVAController.cs
  4. 6
      Backend/EVABackend/EVABackend/Properties/launchSettings.json
  5. 9
      Backend/EVABackend/EVABackend/Startup.cs
  6. BIN
      Backend/EVABackend/EVABackend/eva_users.db

7
Backend/EVABackend/EVABackend/Areas/Identity/Data/EVABackendIdentityContext.cs

@ -1,9 +1,4 @@
using System; using EVABackend.Areas.Identity.Data;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EVABackend.Areas.Identity.Data;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;

18
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 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.AspNetCore.Identity.UI;
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
@ -15,13 +15,21 @@ namespace EVABackend.Areas.Identity
{ {
public void Configure(IWebHostBuilder builder) public void Configure(IWebHostBuilder builder)
{ {
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.AddDefaultIdentity<EVABackendUser>()
.AddEntityFrameworkStores<EVABackendIdentityContext>(); .AddRoles<IdentityRole>()
.AddEntityFrameworkStores<EVABackendIdentityContext>()
.AddDefaultTokenProviders();
services.ConfigureApplicationCookie(options =>
{
options.Cookie.Name = "EVABackend_Token";
});
}); });
} }
} }

56
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 Microsoft.AspNetCore.Mvc;
using System.Linq; using System.Linq;
using System.Threading.Tasks;
namespace EVABackend.Controllers namespace EVABackend.Controllers
{ {
[ApiController] [ApiController]
public class EVAController : ControllerBase public class EVAController : ControllerBase
{ {
EVAContext context = new EVAContext(); private readonly EVAContext _context;
private readonly UserManager<EVABackendUser> _userManager;
private readonly RoleManager<IdentityRole> _roleManager;
private readonly SignInManager<EVABackendUser> _signInManager;
public EVAController(UserManager<EVABackendUser> userManager, RoleManager<IdentityRole> roleManager, SignInManager<EVABackendUser> 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<ActionResult> CreateDummyData()
{
var user = new EVABackendUser
{
UserName = "Test",
Email = "info@test.de"
};
if ((await _userManager.CreateAsync(user, "123Abc!&")).Succeeded)
return Ok();
return StatusCode(500);
}
[HttpPost] [HttpPost]
[Route("login")] [Route("login")]
public ActionResult Login([FromBody] string username, string password) public async Task<ActionResult> 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] [HttpGet]
[Route("instruments")] [Route("instruments")]
[Authorize]
public ActionResult Instruments() public ActionResult Instruments()
{ {
var instruments = context.Instrumente.ToList(); var instruments = _context.Instrumente.ToList();
return Ok(instruments.ToArray()); return Ok(instruments.ToArray());
} }

6
Backend/EVABackend/EVABackend/Properties/launchSettings.json

@ -12,7 +12,7 @@
"IIS Express": { "IIS Express": {
"commandName": "IISExpress", "commandName": "IISExpress",
"launchBrowser": true, "launchBrowser": true,
"launchUrl": "api/values", "launchUrl": "login",
"environmentVariables": { "environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development" "ASPNETCORE_ENVIRONMENT": "Development"
} }
@ -20,7 +20,7 @@
"EVABackend": { "EVABackend": {
"commandName": "Project", "commandName": "Project",
"launchBrowser": true, "launchBrowser": true,
"launchUrl": "api/values", "launchUrl": "login",
"environmentVariables": { "environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development" "ASPNETCORE_ENVIRONMENT": "Development"
}, },
@ -29,7 +29,7 @@
"Docker": { "Docker": {
"commandName": "Docker", "commandName": "Docker",
"launchBrowser": true, "launchBrowser": true,
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/api/values", "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/login",
"httpPort": 50571, "httpPort": 50571,
"useSSL": true, "useSSL": true,
"sslPort": 44375 "sslPort": 44375

9
Backend/EVABackend/EVABackend/Startup.cs

@ -1,15 +1,10 @@
using System; using EVABackend.Areas.Identity.Data;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy; 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;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace EVABackend namespace EVABackend
{ {

BIN
Backend/EVABackend/EVABackend/eva_users.db

Binary file not shown.
Loading…
Cancel
Save