add-aspnet-identity #26

Merged
jleicher merged 5 commits from add-aspnet-identity into master 2020-12-11 17:59:36 +01:00
4 changed files with 39 additions and 29 deletions
Showing only changes of commit f4c564748a - Show all commits

View File

@ -1,6 +1,7 @@
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using HN.Infrastructure; using HN.Infrastructure;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Website.Models; using Website.Models;
@ -18,6 +19,7 @@ namespace Website.Controllers
_signInManager = signInManager; _signInManager = signInManager;
} }
[AllowAnonymous]
public IActionResult Register() public IActionResult Register()
{ {
return View(); return View();
@ -25,6 +27,7 @@ namespace Website.Controllers
[HttpPost] [HttpPost]
[ValidateAntiForgeryToken] [ValidateAntiForgeryToken]
[AllowAnonymous]
public async Task<IActionResult> Register(RegisterViewModel command) public async Task<IActionResult> Register(RegisterViewModel command)
{ {
if (!ModelState.IsValid) if (!ModelState.IsValid)
@ -46,6 +49,7 @@ namespace Website.Controllers
return RedirectToAction(nameof(Login)); return RedirectToAction(nameof(Login));
} }
[AllowAnonymous]
public IActionResult Login() public IActionResult Login()
{ {
return View(); return View();
@ -53,6 +57,7 @@ namespace Website.Controllers
[HttpPost] [HttpPost]
[ValidateAntiForgeryToken] [ValidateAntiForgeryToken]
[AllowAnonymous]
public async Task<IActionResult> Login(LoginViewModel command) public async Task<IActionResult> Login(LoginViewModel command)
{ {
if (!ModelState.IsValid) if (!ModelState.IsValid)

View File

@ -1,37 +1,35 @@
using System; using System.Diagnostics;
using System.Collections.Generic; using Microsoft.AspNetCore.Authorization;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Website.Models; using Website.Models;
namespace Website.Controllers namespace Website.Controllers
{ {
public class HomeController : Controller [AllowAnonymous]
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{ {
private readonly ILogger<HomeController> _logger; _logger = logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public IActionResult Index()
{
return View();
}
public IActionResult Privacy()
{
return View();
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
} }
public IActionResult Index()
{
return View();
}
public IActionResult Privacy()
{
return View();
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
} }

View File

@ -5,6 +5,7 @@ using System.Threading.Tasks;
using System; using System;
using HN.Domain; using HN.Domain;
using Website.Models; using Website.Models;
using Microsoft.AspNetCore.Authorization;
namespace Website.Controllers namespace Website.Controllers
{ {
@ -18,12 +19,14 @@ namespace Website.Controllers
} }
[HttpGet] [HttpGet]
[AllowAnonymous]
public async Task<IActionResult> Index() public async Task<IActionResult> Index()
{ {
return View(await _bus.Send(new ListLinksQuery())); return View(await _bus.Send(new ListLinksQuery()));
} }
[HttpGet("{controller}/{id:guid}")] [HttpGet("{controller}/{id:guid}")]
[AllowAnonymous]
public async Task<IActionResult> Show(Guid id) public async Task<IActionResult> Show(Guid id)
{ {
var link = await _bus.Send(new GetLinkQuery(id)); var link = await _bus.Send(new GetLinkQuery(id));

View File

@ -6,6 +6,7 @@ using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc.Authorization;
using Microsoft.AspNetCore.Routing; using Microsoft.AspNetCore.Routing;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
@ -55,7 +56,10 @@ namespace Website
o.LogoutPath = "/accounts/logout"; o.LogoutPath = "/accounts/logout";
}); });
services.AddControllersWithViews(); services.AddControllersWithViews(o =>
{
o.Filters.Add(new AuthorizeFilter()); // Nécessite l'authentification par défaut
});
} }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.