Skip to content

Commit

Permalink
Use file-scoped namespaces
Browse files Browse the repository at this point in the history
Use file-scoped namespaces in the sample app.
  • Loading branch information
martincostello committed Sep 25, 2021
1 parent 9559742 commit 6b94a82
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 83 deletions.
63 changes: 30 additions & 33 deletions samples/Mvc.Client/Controllers/AuthenticationController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IActionResult> SignIn() => View("SignIn", await HttpContext.GetExternalProvidersAsync());
[HttpGet("~/signin")]
public async Task<IActionResult> SignIn() => View("SignIn", await HttpContext.GetExternalProvidersAsync());

[HttpPost("~/signin")]
public async Task<IActionResult> SignIn([FromForm] string provider)
[HttpPost("~/signin")]
public async Task<IActionResult> 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);
}
}
11 changes: 5 additions & 6 deletions samples/Mvc.Client/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
46 changes: 19 additions & 27 deletions samples/Mvc.Client/Extensions/HttpContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AuthenticationScheme[]> GetExternalProvidersAsync(this HttpContext context)
{
public static async Task<AuthenticationScheme[]> 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<IAuthenticationSchemeProvider>();
var schemes = context.RequestServices.GetRequiredService<IAuthenticationSchemeProvider>();

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<bool> IsProviderSupportedAsync(this HttpContext context, string provider)
public static async Task<bool> 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();
}
}
1 change: 1 addition & 0 deletions samples/Mvc.Client/Mvc.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="JetBrains.Annotations" PrivateAssets="All" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.Google" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.Twitter" />
</ItemGroup>
Expand Down
20 changes: 8 additions & 12 deletions samples/Mvc.Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Startup>());
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(
(webBuilder) => webBuilder.UseStartup<Startup>());
}
5 changes: 0 additions & 5 deletions samples/Mvc.Client/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6b94a82

Please sign in to comment.