|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Linq; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Microsoft.AspNetCore.Authentication; |
| 9 | +using Microsoft.Extensions.DependencyInjection; |
| 10 | +using Microsoft.Extensions.Logging; |
| 11 | + |
| 12 | +namespace Microsoft.AspNetCore.Http.Result |
| 13 | +{ |
| 14 | + /// <summary> |
| 15 | + /// An <see cref="IResult"/> that on execution invokes <see cref="M:HttpContext.ChallengeAsync"/>. |
| 16 | + /// </summary> |
| 17 | + internal sealed partial class ChallengeResult : IResult |
| 18 | + { |
| 19 | + /// <summary> |
| 20 | + /// Initializes a new instance of <see cref="ChallengeResult"/>. |
| 21 | + /// </summary> |
| 22 | + public ChallengeResult() |
| 23 | + : this(Array.Empty<string>()) |
| 24 | + { |
| 25 | + } |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Initializes a new instance of <see cref="ChallengeResult"/> with the |
| 29 | + /// specified authentication scheme. |
| 30 | + /// </summary> |
| 31 | + /// <param name="authenticationScheme">The authentication scheme to challenge.</param> |
| 32 | + public ChallengeResult(string authenticationScheme) |
| 33 | + : this(new[] { authenticationScheme }) |
| 34 | + { |
| 35 | + } |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Initializes a new instance of <see cref="ChallengeResult"/> with the |
| 39 | + /// specified authentication schemes. |
| 40 | + /// </summary> |
| 41 | + /// <param name="authenticationSchemes">The authentication schemes to challenge.</param> |
| 42 | + public ChallengeResult(IList<string> authenticationSchemes) |
| 43 | + : this(authenticationSchemes, properties: null) |
| 44 | + { |
| 45 | + } |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Initializes a new instance of <see cref="ChallengeResult"/> with the |
| 49 | + /// specified <paramref name="properties"/>. |
| 50 | + /// </summary> |
| 51 | + /// <param name="properties"><see cref="AuthenticationProperties"/> used to perform the authentication |
| 52 | + /// challenge.</param> |
| 53 | + public ChallengeResult(AuthenticationProperties? properties) |
| 54 | + : this(Array.Empty<string>(), properties) |
| 55 | + { |
| 56 | + } |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// Initializes a new instance of <see cref="ChallengeResult"/> with the |
| 60 | + /// specified authentication scheme and <paramref name="properties"/>. |
| 61 | + /// </summary> |
| 62 | + /// <param name="authenticationScheme">The authentication schemes to challenge.</param> |
| 63 | + /// <param name="properties"><see cref="AuthenticationProperties"/> used to perform the authentication |
| 64 | + /// challenge.</param> |
| 65 | + public ChallengeResult(string authenticationScheme, AuthenticationProperties? properties) |
| 66 | + : this(new[] { authenticationScheme }, properties) |
| 67 | + { |
| 68 | + } |
| 69 | + |
| 70 | + /// <summary> |
| 71 | + /// Initializes a new instance of <see cref="ChallengeResult"/> with the |
| 72 | + /// specified authentication schemes and <paramref name="properties"/>. |
| 73 | + /// </summary> |
| 74 | + /// <param name="authenticationSchemes">The authentication scheme to challenge.</param> |
| 75 | + /// <param name="properties"><see cref="AuthenticationProperties"/> used to perform the authentication |
| 76 | + /// challenge.</param> |
| 77 | + public ChallengeResult(IList<string> authenticationSchemes, AuthenticationProperties? properties) |
| 78 | + { |
| 79 | + AuthenticationSchemes = authenticationSchemes; |
| 80 | + Properties = properties; |
| 81 | + } |
| 82 | + |
| 83 | + public IList<string> AuthenticationSchemes { get; init; } = Array.Empty<string>(); |
| 84 | + |
| 85 | + public AuthenticationProperties? Properties { get; init; } |
| 86 | + |
| 87 | + public async Task ExecuteAsync(HttpContext httpContext) |
| 88 | + { |
| 89 | + var logger = httpContext.RequestServices.GetRequiredService<ILogger<ChallengeResult>>(); |
| 90 | + |
| 91 | + Log.ChallengeResultExecuting(logger, AuthenticationSchemes); |
| 92 | + |
| 93 | + if (AuthenticationSchemes != null && AuthenticationSchemes.Count > 0) |
| 94 | + { |
| 95 | + foreach (var scheme in AuthenticationSchemes) |
| 96 | + { |
| 97 | + await httpContext.ChallengeAsync(scheme, Properties); |
| 98 | + } |
| 99 | + } |
| 100 | + else |
| 101 | + { |
| 102 | + await httpContext.ChallengeAsync(Properties); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + private static partial class Log |
| 107 | + { |
| 108 | + public static void ChallengeResultExecuting(ILogger logger, IList<string> authenticationSchemes) |
| 109 | + { |
| 110 | + if (logger.IsEnabled(LogLevel.Information)) |
| 111 | + { |
| 112 | + ChallengeResultExecuting(logger, authenticationSchemes.ToArray()); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + [LoggerMessage(1, LogLevel.Information, "Executing ChallengeResult with authentication schemes ({Schemes}).", EventName = "ChallengeResultExecuting", SkipEnabledCheck = true)] |
| 117 | + private static partial void ChallengeResultExecuting(ILogger logger, string[] schemes); |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments