// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.AspNetCore.Http.Authentication; using Microsoft.AspNetCore.Mvc.Internal; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; namespace Microsoft.AspNetCore.Mvc { /// /// An that on execution invokes . /// public class ChallengeResult : ActionResult { /// /// Initializes a new instance of . /// public ChallengeResult() : this(new string[] { }) { } /// /// Initializes a new instance of with the /// specified authentication scheme. /// /// The authentication scheme to challenge. public ChallengeResult(string authenticationScheme) : this(new[] { authenticationScheme }) { } /// /// Initializes a new instance of with the /// specified authentication schemes. /// /// The authentication schemes to challenge. public ChallengeResult(IList authenticationSchemes) : this(authenticationSchemes, properties: null) { } /// /// Initializes a new instance of with the /// specified . /// /// used to perform the authentication /// challenge. public ChallengeResult(AuthenticationProperties properties) : this(new string[] { }, properties) { } /// /// Initializes a new instance of with the /// specified authentication scheme and . /// /// The authentication schemes to challenge. /// used to perform the authentication /// challenge. public ChallengeResult(string authenticationScheme, AuthenticationProperties properties) : this(new[] { authenticationScheme }, properties) { } /// /// Initializes a new instance of with the /// specified authentication schemes and . /// /// The authentication scheme to challenge. /// used to perform the authentication /// challenge. public ChallengeResult(IList authenticationSchemes, AuthenticationProperties properties) { AuthenticationSchemes = authenticationSchemes; Properties = properties; } /// /// Gets or sets the authentication schemes that are challenged. /// public IList AuthenticationSchemes { get; set; } /// /// Gets or sets the used to perform the authentication challenge. /// public AuthenticationProperties Properties { get; set; } /// public override async Task ExecuteResultAsync(ActionContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } var loggerFactory = context.HttpContext.RequestServices.GetRequiredService(); var logger = loggerFactory.CreateLogger(); logger.ChallengeResultExecuting(AuthenticationSchemes); var authentication = context.HttpContext.Authentication; if (AuthenticationSchemes != null && AuthenticationSchemes.Count > 0) { foreach (var scheme in AuthenticationSchemes) { await authentication.ChallengeAsync(scheme, Properties); } } else { await authentication.ChallengeAsync(Properties); } } } }