diff --git a/samples/Mvc.Client/Controllers/AuthenticationController.cs b/samples/Mvc.Client/Controllers/AuthenticationController.cs index db6677435..77d8db67c 100644 --- a/samples/Mvc.Client/Controllers/AuthenticationController.cs +++ b/samples/Mvc.Client/Controllers/AuthenticationController.cs @@ -4,49 +4,46 @@ * for more information concerning the license and the contributors participating to this project. */ -using System.Threading.Tasks; -using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Mvc; using Mvc.Client.Extensions; -namespace Mvc.Client.Controllers +namespace Mvc.Client.Controllers; + +public class AuthenticationController : Controller { - public class AuthenticationController : Controller - { - [HttpGet("~/signin")] - public async Task SignIn() => View("SignIn", await HttpContext.GetExternalProvidersAsync()); + [HttpGet("~/signin")] + public async Task SignIn() => View("SignIn", await HttpContext.GetExternalProvidersAsync()); - [HttpPost("~/signin")] - public async Task SignIn([FromForm] string provider) + [HttpPost("~/signin")] + public async Task SignIn([FromForm] string provider) + { + // Note: the "provider" parameter corresponds to the external + // authentication provider choosen by the user agent. + if (string.IsNullOrWhiteSpace(provider)) { - // Note: the "provider" parameter corresponds to the external - // authentication provider choosen by the user agent. - if (string.IsNullOrWhiteSpace(provider)) - { - return BadRequest(); - } - - if (!await HttpContext.IsProviderSupportedAsync(provider)) - { - return BadRequest(); - } - - // Instruct the middleware corresponding to the requested external identity - // provider to redirect the user agent to its own authorization endpoint. - // Note: the authenticationScheme parameter must match the value configured in Startup.cs - return Challenge(new AuthenticationProperties { RedirectUri = "/" }, provider); + return BadRequest(); } - [HttpGet("~/signout")] - [HttpPost("~/signout")] - public IActionResult SignOutCurrentUser() + if (!await HttpContext.IsProviderSupportedAsync(provider)) { - // Instruct the cookies middleware to delete the local cookie created - // when the user agent is redirected from the external identity provider - // after a successful authentication flow (e.g Google or Facebook). - return SignOut(new AuthenticationProperties { RedirectUri = "/" }, - CookieAuthenticationDefaults.AuthenticationScheme); + return BadRequest(); } + + // Instruct the middleware corresponding to the requested external identity + // provider to redirect the user agent to its own authorization endpoint. + // Note: the authenticationScheme parameter must match the value configured in Startup.cs + return Challenge(new AuthenticationProperties { RedirectUri = "/" }, provider); + } + + [HttpGet("~/signout")] + [HttpPost("~/signout")] + public IActionResult SignOutCurrentUser() + { + // Instruct the cookies middleware to delete the local cookie created + // when the user agent is redirected from the external identity provider + // after a successful authentication flow (e.g Google or Facebook). + return SignOut(new AuthenticationProperties { RedirectUri = "/" }, + CookieAuthenticationDefaults.AuthenticationScheme); } } diff --git a/samples/Mvc.Client/Controllers/HomeController.cs b/samples/Mvc.Client/Controllers/HomeController.cs index 9ed360052..1832f1942 100644 --- a/samples/Mvc.Client/Controllers/HomeController.cs +++ b/samples/Mvc.Client/Controllers/HomeController.cs @@ -6,11 +6,10 @@ using Microsoft.AspNetCore.Mvc; -namespace Mvc.Client.Controllers +namespace Mvc.Client.Controllers; + +public class HomeController : Controller { - public class HomeController : Controller - { - [HttpGet("~/")] - public ActionResult Index() => View(); - } + [HttpGet("~/")] + public ActionResult Index() => View(); } diff --git a/samples/Mvc.Client/Extensions/HttpContextExtensions.cs b/samples/Mvc.Client/Extensions/HttpContextExtensions.cs index aad15dc71..44575d850 100644 --- a/samples/Mvc.Client/Extensions/HttpContextExtensions.cs +++ b/samples/Mvc.Client/Extensions/HttpContextExtensions.cs @@ -4,41 +4,33 @@ * for more information concerning the license and the contributors participating to this project. */ -using System; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Authentication; -using Microsoft.AspNetCore.Http; -using Microsoft.Extensions.DependencyInjection; +namespace Mvc.Client.Extensions; -namespace Mvc.Client.Extensions +public static class HttpContextExtensions { - public static class HttpContextExtensions + public static async Task GetExternalProvidersAsync(this HttpContext context) { - public static async Task GetExternalProvidersAsync(this HttpContext context) + if (context == null) { - if (context == null) - { - throw new ArgumentNullException(nameof(context)); - } + throw new ArgumentNullException(nameof(context)); + } - var schemes = context.RequestServices.GetRequiredService(); + var schemes = context.RequestServices.GetRequiredService(); - return (from scheme in await schemes.GetAllSchemesAsync() - where !string.IsNullOrEmpty(scheme.DisplayName) - select scheme).ToArray(); - } + return (from scheme in await schemes.GetAllSchemesAsync() + where !string.IsNullOrEmpty(scheme.DisplayName) + select scheme).ToArray(); + } - public static async Task IsProviderSupportedAsync(this HttpContext context, string provider) + public static async Task IsProviderSupportedAsync(this HttpContext context, string provider) + { + if (context == null) { - if (context == null) - { - throw new ArgumentNullException(nameof(context)); - } - - return (from scheme in await context.GetExternalProvidersAsync() - where string.Equals(scheme.Name, provider, StringComparison.OrdinalIgnoreCase) - select scheme).Any(); + throw new ArgumentNullException(nameof(context)); } + + return (from scheme in await context.GetExternalProvidersAsync() + where string.Equals(scheme.Name, provider, StringComparison.OrdinalIgnoreCase) + select scheme).Any(); } } diff --git a/samples/Mvc.Client/Mvc.Client.csproj b/samples/Mvc.Client/Mvc.Client.csproj index fe08796e4..fa2895ba7 100644 --- a/samples/Mvc.Client/Mvc.Client.csproj +++ b/samples/Mvc.Client/Mvc.Client.csproj @@ -11,6 +11,7 @@ + diff --git a/samples/Mvc.Client/Program.cs b/samples/Mvc.Client/Program.cs index a8ef55bd6..2a231c15a 100644 --- a/samples/Mvc.Client/Program.cs +++ b/samples/Mvc.Client/Program.cs @@ -4,19 +4,15 @@ * for more information concerning the license and the contributors participating to this project. */ -using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Hosting; +namespace Mvc.Client; -namespace Mvc.Client +public static class Program { - public static class Program - { - public static void Main(string[] args) => - CreateHostBuilder(args).Build().Run(); + public static void Main(string[] args) => + CreateHostBuilder(args).Build().Run(); - public static IHostBuilder CreateHostBuilder(string[] args) => - Host.CreateDefaultBuilder(args) - .ConfigureWebHostDefaults( - (webBuilder) => webBuilder.UseStartup()); - } + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults( + (webBuilder) => webBuilder.UseStartup()); } diff --git a/samples/Mvc.Client/Startup.cs b/samples/Mvc.Client/Startup.cs index 82f1f4506..2b5d3c453 100644 --- a/samples/Mvc.Client/Startup.cs +++ b/samples/Mvc.Client/Startup.cs @@ -5,11 +5,6 @@ */ using Microsoft.AspNetCore.Authentication.Cookies; -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; using Microsoft.IdentityModel.Logging; namespace Mvc.Client