From c816e482ddc788839332232b3d8021738ce23798 Mon Sep 17 00:00:00 2001 From: Savorboard Date: Thu, 3 Mar 2022 19:07:43 +0800 Subject: [PATCH] Place the authentication result into cookie to avoid re-authentication. --- src/DotNetCore.CAP.Dashboard/CAP.BuilderExtension.cs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/DotNetCore.CAP.Dashboard/CAP.BuilderExtension.cs b/src/DotNetCore.CAP.Dashboard/CAP.BuilderExtension.cs index 3308e2a..9b215aa 100644 --- a/src/DotNetCore.CAP.Dashboard/CAP.BuilderExtension.cs +++ b/src/DotNetCore.CAP.Dashboard/CAP.BuilderExtension.cs @@ -8,12 +8,14 @@ using DotNetCore.CAP.Dashboard; using DotNetCore.CAP.Dashboard.GatewayProxy; using DotNetCore.CAP.Dashboard.NodeDiscovery; using Microsoft.AspNetCore.Authentication; +using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Routing; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Internal; +using Microsoft.Extensions.Options; // ReSharper disable once CheckNamespace namespace DotNetCore.CAP @@ -121,13 +123,18 @@ namespace DotNetCore.CAP return false; } - if (options.UseAuth) + if (isAuthenticated == false && options.UseAuth) { var result = await context.AuthenticateAsync(options.DefaultAuthenticationScheme); if (result.Succeeded && result.Principal != null) { - context.User = result.Principal; + //If a cookie scheme is configured, the authentication result will be placed in a cookie to avoid re-authentication + var defaultOptions = context.RequestServices.GetService>(); + if (defaultOptions != null && defaultOptions.Value.DefaultScheme == CookieAuthenticationDefaults.AuthenticationScheme) + { + await context.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, result.Principal); + } } else {