From 00eb05a28286486e99c3b488f7f924e1fd7e1e03 Mon Sep 17 00:00:00 2001 From: MarcEricMartel <74071476+MarcEricMartel@users.noreply.github.com> Date: Tue, 1 Nov 2022 08:05:32 -0700 Subject: [PATCH] =?UTF-8?q?Probl=C3=A8me=20de=20redirect=20de=20l'API=20co?= =?UTF-8?q?rrig=C3=A9.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- GrossesMitaines/GrossesMitainesAPI/Program.cs | 22 +++++++++++++++++-- .../grosses-mitaines-ui/src/pages/Home.js | 2 +- .../grosses-mitaines-ui/src/pages/Login.js | 7 +++--- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/GrossesMitaines/GrossesMitainesAPI/Program.cs b/GrossesMitaines/GrossesMitainesAPI/Program.cs index 2a16f38..58230ea 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Program.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Program.cs @@ -23,8 +23,27 @@ builder.Services.AddCors(options => { }); builder.Services.AddControllers(); +builder.Services.AddAuthorization(); +builder.Services.AddAuthentication().AddIdentityCookies(); + +// Source: https://github.com/dotnet/aspnetcore/issues/9039 +builder.Services.ConfigureApplicationCookie(o => { + o.Events = new CookieAuthenticationEvents() { + OnRedirectToLogin = (ctx) => { + if (ctx.Request.Path.StartsWithSegments("/api") && ctx.Response.StatusCode == 200) { + ctx.Response.StatusCode = 401; + } + return Task.CompletedTask; + }, + OnRedirectToAccessDenied = (ctx) => { + if (ctx.Request.Path.StartsWithSegments("/api") && ctx.Response.StatusCode == 200) { + ctx.Response.StatusCode = 403; + } + return Task.CompletedTask; + } + }; +}); -builder.Services.AddAuthentication("Cookies").AddIdentityCookies(); builder.Services.AddIdentityCore() .AddRoles() .AddEntityFrameworkStores() @@ -52,7 +71,6 @@ if (app.Environment.IsDevelopment()) { app.UseHttpsRedirection(); app.UseCors(MyAllowSpecificOrigins); - app.UseAuthorization(); app.UseAuthentication(); diff --git a/GrossesMitaines/grosses-mitaines-ui/src/pages/Home.js b/GrossesMitaines/grosses-mitaines-ui/src/pages/Home.js index 723057e..035305c 100644 --- a/GrossesMitaines/grosses-mitaines-ui/src/pages/Home.js +++ b/GrossesMitaines/grosses-mitaines-ui/src/pages/Home.js @@ -29,7 +29,7 @@ const Home = () => {

(à Ginette et Monique)

home
-

Des produits bien commodes faits par des aînées d'expérience.

+

Des produits bien commodes faits par des aînées d'expérience. {document.cookie}

{ const [username, setUsername] = useState(""); const [password, setPassword] = useState(""); + const [kookees, setCookies] = useState(""); const handleLogin = async (e) => { e.preventDefault(); - const response = await fetch(`https://localhost:7292/api/Login?rememberme=true`, { + const response = await fetch(`https://localhost:7292/api/Login?rememberMe=true`, { method: 'POST', headers: { 'Accept': 'application/json', @@ -17,13 +18,13 @@ const Login = () => { }, body: JSON.stringify({ username, password }) }) - - // Partie de display d'erreur ou de redirection (faudrait checker pour se faire un state de connexion). + // Partie de display d'erreur ou de redirection (faudrait checker pour se faire un state de connexion avec un cookie pour react). } return (
+

{kookees}

Connexion