From 940fb7ba78219301addd42288a08294b20b023bf Mon Sep 17 00:00:00 2001 From: Pranav K Date: Mon, 2 Nov 2015 11:35:44 -0800 Subject: [PATCH] Adding Controller.Challenge and Controller.Forbid --- .../ChallengeResult.cs | 45 ++++++- .../{ForbiddenResult.cs => ForbidResult.cs} | 33 +++-- ...ons.cs => ForbidResultLoggerExtensions.cs} | 6 +- .../Controller.cs | 124 ++++++++++++++++++ ...iddenResultTest.cs => ForbidResultTest.cs} | 10 +- 5 files changed, 192 insertions(+), 26 deletions(-) rename src/Microsoft.AspNet.Mvc.Core/{ForbiddenResult.cs => ForbidResult.cs} (74%) rename src/Microsoft.AspNet.Mvc.Core/Logging/{ForbiddenResultLoggerExtensions.cs => ForbidResultLoggerExtensions.cs} (68%) rename test/Microsoft.AspNet.Mvc.Core.Test/{ForbiddenResultTest.cs => ForbidResultTest.cs} (94%) diff --git a/src/Microsoft.AspNet.Mvc.Core/ChallengeResult.cs b/src/Microsoft.AspNet.Mvc.Core/ChallengeResult.cs index bd75945fdb..4a357eadc9 100644 --- a/src/Microsoft.AspNet.Mvc.Core/ChallengeResult.cs +++ b/src/Microsoft.AspNet.Mvc.Core/ChallengeResult.cs @@ -11,43 +11,86 @@ using Microsoft.Extensions.Logging; namespace Microsoft.AspNet.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) @@ -70,7 +113,7 @@ namespace Microsoft.AspNet.Mvc { await authentication.ChallengeAsync(Properties); } - + logger.ChallengeResultExecuting(AuthenticationSchemes); } } diff --git a/src/Microsoft.AspNet.Mvc.Core/ForbiddenResult.cs b/src/Microsoft.AspNet.Mvc.Core/ForbidResult.cs similarity index 74% rename from src/Microsoft.AspNet.Mvc.Core/ForbiddenResult.cs rename to src/Microsoft.AspNet.Mvc.Core/ForbidResult.cs index b5a24eba46..b203581a1b 100644 --- a/src/Microsoft.AspNet.Mvc.Core/ForbiddenResult.cs +++ b/src/Microsoft.AspNet.Mvc.Core/ForbidResult.cs @@ -12,70 +12,69 @@ using Microsoft.Extensions.Logging; namespace Microsoft.AspNet.Mvc { /// - /// An that on execution issues a 403 forbidden response - /// if the authentication challenge is unacceptable. + /// An that on execution invokes . /// - public class ForbiddenResult : ActionResult + public class ForbidResult : ActionResult { /// - /// Initializes a new instance of . + /// Initializes a new instance of . /// - public ForbiddenResult() + public ForbidResult() : this(new string[] { }) { } /// - /// Initializes a new instance of with the + /// Initializes a new instance of with the /// specified authentication scheme. /// /// The authentication scheme to challenge. - public ForbiddenResult(string authenticationScheme) + public ForbidResult(string authenticationScheme) : this(new[] { authenticationScheme }) { } /// - /// Initializes a new instance of with the + /// Initializes a new instance of with the /// specified authentication schemes. /// /// The authentication schemes to challenge. - public ForbiddenResult(IList authenticationSchemes) + public ForbidResult(IList authenticationSchemes) : this(authenticationSchemes, properties: null) { } /// - /// Initializes a new instance of with the + /// Initializes a new instance of with the /// specified . /// /// used to perform the authentication /// challenge. - public ForbiddenResult(AuthenticationProperties properties) + public ForbidResult(AuthenticationProperties properties) : this(new string[] { }, properties) { } /// - /// Initializes a new instance of with the + /// Initializes a new instance of with the /// specified authentication scheme and . /// /// The authentication schemes to challenge. /// used to perform the authentication /// challenge. - public ForbiddenResult(string authenticationScheme, AuthenticationProperties properties) + public ForbidResult(string authenticationScheme, AuthenticationProperties properties) : this(new[] { authenticationScheme }, properties) { } /// - /// Initializes a new instance of with the + /// Initializes a new instance of with the /// specified authentication schemes and . /// /// The authentication scheme to challenge. /// used to perform the authentication /// challenge. - public ForbiddenResult(IList authenticationSchemes, AuthenticationProperties properties) + public ForbidResult(IList authenticationSchemes, AuthenticationProperties properties) { AuthenticationSchemes = authenticationSchemes; Properties = properties; @@ -100,7 +99,7 @@ namespace Microsoft.AspNet.Mvc } var loggerFactory = context.HttpContext.RequestServices.GetRequiredService(); - var logger = loggerFactory.CreateLogger(); + var logger = loggerFactory.CreateLogger(); var authentication = context.HttpContext.Authentication; @@ -116,7 +115,7 @@ namespace Microsoft.AspNet.Mvc await authentication.ForbidAsync(Properties); } - logger.ForbiddenResultExecuting(AuthenticationSchemes); + logger.ForbidResultExecuting(AuthenticationSchemes); } } } diff --git a/src/Microsoft.AspNet.Mvc.Core/Logging/ForbiddenResultLoggerExtensions.cs b/src/Microsoft.AspNet.Mvc.Core/Logging/ForbidResultLoggerExtensions.cs similarity index 68% rename from src/Microsoft.AspNet.Mvc.Core/Logging/ForbiddenResultLoggerExtensions.cs rename to src/Microsoft.AspNet.Mvc.Core/Logging/ForbidResultLoggerExtensions.cs index 40989c59a3..c5aa05ce15 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Logging/ForbiddenResultLoggerExtensions.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Logging/ForbidResultLoggerExtensions.cs @@ -8,15 +8,15 @@ using Microsoft.Extensions.Logging; namespace Microsoft.AspNet.Mvc.Logging { - internal static class ForbiddenResultLoggerExtensions + internal static class ForbidResultLoggerExtensions { private static readonly Action _resultExecuting = LoggerMessage.Define( LogLevel.Information, eventId: 1, - formatString: $"Executing {nameof(ForbiddenResult)} with authentication schemes ({{Schemes}})."); + formatString: $"Executing {nameof(ForbidResult)} with authentication schemes ({{Schemes}})."); - public static void ForbiddenResultExecuting(this ILogger logger, IList authenticationSchemes) + public static void ForbidResultExecuting(this ILogger logger, IList authenticationSchemes) { _resultExecuting(logger, authenticationSchemes.ToArray(), null); } diff --git a/src/Microsoft.AspNet.Mvc.ViewFeatures/Controller.cs b/src/Microsoft.AspNet.Mvc.ViewFeatures/Controller.cs index 5f610a30dc..4ca6389f54 100644 --- a/src/Microsoft.AspNet.Mvc.ViewFeatures/Controller.cs +++ b/src/Microsoft.AspNet.Mvc.ViewFeatures/Controller.cs @@ -2,12 +2,14 @@ // 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.IO; using System.Linq.Expressions; using System.Security.Claims; using System.Text; using System.Threading.Tasks; using Microsoft.AspNet.Http; +using Microsoft.AspNet.Http.Authentication; using Microsoft.AspNet.Mvc.Filters; using Microsoft.AspNet.Mvc.ModelBinding; using Microsoft.AspNet.Mvc.ModelBinding.Validation; @@ -1180,6 +1182,128 @@ namespace Microsoft.AspNet.Mvc return new CreatedAtRouteResult(routeName, routeValues, value); } + /// + /// Creates a . + /// + /// The created for the response. + [NonAction] + public virtual ChallengeResult Challenge() + => new ChallengeResult(); + + /// + /// Creates a with the specified authentication scheme. + /// + /// The authentication scheme to challenge. + /// The created for the response. + [NonAction] + public virtual ChallengeResult Challenge(string authenticationScheme) + => new ChallengeResult(authenticationScheme); + + /// + /// Creates a with the specified authentication schemes. + /// + /// The authentication schemes to challenge. + /// The created for the response. + [NonAction] + public virtual ChallengeResult Challenge(IList authenticationSchemes) + => new ChallengeResult(authenticationSchemes); + + /// + /// Creates a with the specified . + /// + /// used to perform the authentication + /// challenge. + /// The created for the response. + [NonAction] + public virtual ChallengeResult Challenge(AuthenticationProperties properties) + => new ChallengeResult(properties); + + /// + /// Creates a with the specified specified authentication scheme and + /// . + /// + /// The authentication scheme to challenge. + /// used to perform the authentication + /// challenge. + /// The created for the response. + [NonAction] + public virtual ChallengeResult Challenge(string authenticationScheme, AuthenticationProperties properties) + => new ChallengeResult(authenticationScheme, properties); + + /// + /// Creates a with the specified specified authentication schemes and + /// . + /// + /// The authentication schemes to challenge. + /// used to perform the authentication + /// challenge. + /// The created for the response. + [NonAction] + public virtual ChallengeResult Challenge( + IList authenticationSchemes, + AuthenticationProperties properties) + => new ChallengeResult(authenticationSchemes, properties); + + /// + /// Creates a . + /// + /// The created for the response. + [NonAction] + public virtual ForbidResult Forbid() + => new ForbidResult(); + + /// + /// Creates a with the specified authentication scheme. + /// + /// The authentication scheme to challenge. + /// The created for the response. + [NonAction] + public virtual ForbidResult Forbid(string authenticationScheme) + => new ForbidResult(authenticationScheme); + + /// + /// Creates a with the specified authentication schemes. + /// + /// The authentication schemes to challenge. + /// The created for the response. + [NonAction] + public virtual ForbidResult Forbid(IList authenticationSchemes) + => new ForbidResult(authenticationSchemes); + + /// + /// Creates a with the specified . + /// + /// used to perform the authentication + /// challenge. + /// The created for the response. + [NonAction] + public virtual ForbidResult Forbid(AuthenticationProperties properties) + => new ForbidResult(properties); + + /// + /// Creates a with the specified specified authentication scheme and + /// . + /// + /// The authentication scheme to challenge. + /// used to perform the authentication + /// challenge. + /// The created for the response. + [NonAction] + public virtual ForbidResult Forbid(string authenticationScheme, AuthenticationProperties properties) + => new ForbidResult(authenticationScheme, properties); + + /// + /// Creates a with the specified specified authentication schemes and + /// . + /// + /// The authentication schemes to challenge. + /// used to perform the authentication + /// challenge. + /// The created for the response. + [NonAction] + public virtual ForbidResult Forbid(IList authenticationSchemes, AuthenticationProperties properties) + => new ForbidResult(authenticationSchemes, properties); + /// /// Called before the action method is invoked. /// diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/ForbiddenResultTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/ForbidResultTest.cs similarity index 94% rename from test/Microsoft.AspNet.Mvc.Core.Test/ForbiddenResultTest.cs rename to test/Microsoft.AspNet.Mvc.Core.Test/ForbidResultTest.cs index a028aac4d6..f9ac598dcf 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/ForbiddenResultTest.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/ForbidResultTest.cs @@ -16,7 +16,7 @@ using Xunit; namespace Microsoft.AspNet.Mvc { - public class ForbiddenResultTest + public class ForbidResultTest { [Fact] public async Task ExecuteResultAsync_InvokesForbiddenAsyncOnAuthenticationManager() @@ -30,7 +30,7 @@ namespace Microsoft.AspNet.Mvc var httpContext = new Mock(); httpContext.Setup(c => c.RequestServices).Returns(CreateServices()); httpContext.Setup(c => c.Authentication).Returns(authenticationManager.Object); - var result = new ForbiddenResult("", null); + var result = new ForbidResult("", null); var routeData = new RouteData(); var actionContext = new ActionContext( @@ -62,7 +62,7 @@ namespace Microsoft.AspNet.Mvc var httpContext = new Mock(); httpContext.Setup(c => c.RequestServices).Returns(CreateServices()); httpContext.Setup(c => c.Authentication).Returns(authenticationManager.Object); - var result = new ForbiddenResult(new[] { "Scheme1", "Scheme2" }, authProperties); + var result = new ForbidResult(new[] { "Scheme1", "Scheme2" }, authProperties); var routeData = new RouteData(); var actionContext = new ActionContext( @@ -97,7 +97,7 @@ namespace Microsoft.AspNet.Mvc var httpContext = new Mock(); httpContext.Setup(c => c.RequestServices).Returns(CreateServices()); httpContext.Setup(c => c.Authentication).Returns(authenticationManager.Object); - var result = new ForbiddenResult(expected); + var result = new ForbidResult(expected); var routeData = new RouteData(); var actionContext = new ActionContext( @@ -126,7 +126,7 @@ namespace Microsoft.AspNet.Mvc var httpContext = new Mock(); httpContext.Setup(c => c.RequestServices).Returns(CreateServices()); httpContext.Setup(c => c.Authentication).Returns(authenticationManager.Object); - var result = new ForbiddenResult(expected) + var result = new ForbidResult(expected) { AuthenticationSchemes = new string[0] };