// 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.Authentication; using Microsoft.AspNetCore.Mvc.Core; using Microsoft.AspNetCore.Mvc.Internal; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; namespace Microsoft.AspNetCore.Mvc { /// /// An that on execution invokes . /// public class SignOutResult : ActionResult { /// /// Initializes a new instance of with the default sign out scheme. /// public SignOutResult() : this(Array.Empty()) { } /// /// Initializes a new instance of with the /// specified authentication scheme. /// /// The authentication scheme to use when signing out the user. public SignOutResult(string authenticationScheme) : this(new[] { authenticationScheme }) { } /// /// Initializes a new instance of with the /// specified authentication schemes. /// /// The authentication schemes to use when signing out the user. public SignOutResult(IList authenticationSchemes) : this(authenticationSchemes, properties: null) { } /// /// Initializes a new instance of with the /// specified authentication scheme and . /// /// The authentication schemes to use when signing out the user. /// used to perform the sign-out operation. public SignOutResult(string authenticationScheme, AuthenticationProperties properties) : this(new[] { authenticationScheme }, properties) { } /// /// Initializes a new instance of with the /// specified authentication schemes and . /// /// The authentication scheme to use when signing out the user. /// used to perform the sign-out operation. public SignOutResult(IList authenticationSchemes, AuthenticationProperties properties) { if (authenticationSchemes == null) { throw new ArgumentNullException(nameof(authenticationSchemes)); } 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 sign-out operation. /// public AuthenticationProperties Properties { get; set; } /// public override async Task ExecuteResultAsync(ActionContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (AuthenticationSchemes == null) { throw new InvalidOperationException( Resources.FormatPropertyOfTypeCannotBeNull( /* property: */ nameof(AuthenticationSchemes), /* type: */ nameof(SignOutResult))); } var loggerFactory = context.HttpContext.RequestServices.GetRequiredService(); var logger = loggerFactory.CreateLogger(); logger.SignOutResultExecuting(AuthenticationSchemes); if (AuthenticationSchemes.Count == 0) { await context.HttpContext.SignOutAsync(Properties); } else { for (var i = 0; i < AuthenticationSchemes.Count; i++) { await context.HttpContext.SignOutAsync(AuthenticationSchemes[i], Properties); } } } } }