< Summary

Information
Class: ProjectTemplate.Web.Controllers.ExternalController
Assembly: ProjectTemplate.Web
File(s): /home/runner/work/NetCoreApplicationTemplate/NetCoreApplicationTemplate/src/ProjectTemplate.Web/Controllers/ExternalController.cs
Line coverage
94%
Covered lines: 16
Uncovered lines: 1
Coverable lines: 17
Total lines: 62
Line coverage: 94.1%
Branch coverage
80%
Covered branches: 8
Total branches: 10
Branch coverage: 80%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
Challenge()80%101093.33%

File(s)

/home/runner/work/NetCoreApplicationTemplate/NetCoreApplicationTemplate/src/ProjectTemplate.Web/Controllers/ExternalController.cs

#LineLine coverage
 1using Microsoft.AspNetCore.Authentication;
 2using Microsoft.AspNetCore.Authentication.Cookies;
 3using Microsoft.AspNetCore.Authorization;
 4using Microsoft.AspNetCore.Mvc;
 5
 6namespace ProjectTemplate.Web.Controllers;
 7
 8/// <summary>
 9/// Provides controller actions for initiating external authentication challenges using configured authentication
 10/// schemes.
 11/// </summary>
 12/// <remarks>This controller is intended for use with external authentication providers such as OAuth or OpenID
 13/// Connect. It ensures that only local return URLs are accepted to mitigate open redirect vulnerabilities. The
 14/// controller should be used in scenarios where users need to authenticate via third-party identity
 15/// providers.</remarks>
 16/// <param name="schemeProvider">The authentication scheme provider used to retrieve available external authentication s
 817public sealed class ExternalController(IAuthenticationSchemeProvider schemeProvider) : Controller
 18{
 819    private readonly IAuthenticationSchemeProvider _schemeProvider = schemeProvider;
 20
 21    /// <summary>
 22    /// Initiates an external authentication challenge using the specified provider.
 23    /// </summary>
 24    /// <remarks>This method is typically used to start an OAuth or other external login flow. Only local
 25    /// return URLs are permitted to prevent open redirect vulnerabilities.</remarks>
 26    /// <param name="provider">The name of the external authentication provider to use. Cannot be null, empty, or whites
 27    /// <param name="returnUrl">The URL to redirect the user to after successful authentication. If null or empty, defau
 28    /// root ('/'). Must be a local URL.</param>
 29    /// <returns>An IActionResult that initiates the external authentication challenge or returns a BadRequest result if
 30    /// input is invalid.</returns>
 31    [HttpGet("/External/Challenge")]
 32    [AllowAnonymous]
 33    public async Task<IActionResult> Challenge(string provider, string? returnUrl = null)
 34    {
 835        if (string.IsNullOrWhiteSpace(provider))
 36        {
 037            return BadRequest();
 38        }
 39
 840        string safeReturnUrl = string.IsNullOrWhiteSpace(returnUrl) ? "/" : returnUrl;
 41
 842        if (!Url.IsLocalUrl(safeReturnUrl))
 43        {
 244            return BadRequest();
 45        }
 46
 647        AuthenticationScheme? scheme = await _schemeProvider.GetSchemeAsync(provider);
 48
 649        if (scheme is null ||
 650            string.Equals(scheme.Name, CookieAuthenticationDefaults.AuthenticationScheme, StringComparison.Ordinal))
 51        {
 452            return BadRequest();
 53        }
 54
 255        AuthenticationProperties properties = new()
 256        {
 257            RedirectUri = safeReturnUrl
 258        };
 59
 260        return Challenge(properties, scheme.Name);
 861    }
 62}