Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Commit

Permalink
Changes per PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
pranavkm committed Oct 30, 2015
1 parent fe02908 commit 376afbe
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
15 changes: 12 additions & 3 deletions src/Microsoft.AspNet.Mvc.Core/ForbiddenResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public ForbiddenResult()
/// Initializes a new instance of <see cref="ForbiddenResult"/> with the
/// specified authentication scheme.
/// </summary>
/// <param name="authenticationScheme">The authentication scheme to challenge.</param>
public ForbiddenResult(string authenticationScheme)
: this(new[] { authenticationScheme })
{
Expand All @@ -38,6 +39,7 @@ public ForbiddenResult(string authenticationScheme)
/// Initializes a new instance of <see cref="ForbiddenResult"/> with the
/// specified authentication schemes.
/// </summary>
/// <param name="authenticationScheme">The authentication schemes to challenge.</param>
public ForbiddenResult(IList<string> authenticationSchemes)
: this(authenticationSchemes, properties: null)
{
Expand All @@ -47,6 +49,8 @@ public ForbiddenResult(IList<string> authenticationSchemes)
/// Initializes a new instance of <see cref="ForbiddenResult"/> with the
/// specified <paramref name="properties"/>.
/// </summary>
/// <param name="properties"><see cref="AuthenticationProperties"/> used to perform the authentication
/// challenge.</param>
public ForbiddenResult(AuthenticationProperties properties)
: this(new string[] { }, properties)
{
Expand All @@ -56,6 +60,9 @@ public ForbiddenResult(AuthenticationProperties properties)
/// Initializes a new instance of <see cref="ForbiddenResult"/> with the
/// specified authentication scheme and <paramref name="properties"/>.
/// </summary>
/// <param name="authenticationScheme">The authentication schemes to challenge.</param>
/// <param name="properties"><see cref="AuthenticationProperties"/> used to perform the authentication
/// challenge.</param>
public ForbiddenResult(string authenticationScheme, AuthenticationProperties properties)
: this(new[] { authenticationScheme }, properties)
{
Expand All @@ -65,20 +72,22 @@ public ForbiddenResult(string authenticationScheme, AuthenticationProperties pro
/// Initializes a new instance of <see cref="ForbiddenResult"/> with the
/// specified authentication schemes and <paramref name="properties"/>.
/// </summary>
/// <param name="authenticationScheme">The authentication scheme to challenge.</param>
/// <param name="properties"><see cref="AuthenticationProperties"/> used to perform the authentication
/// challenge.</param>
public ForbiddenResult(IList<string> authenticationSchemes, AuthenticationProperties properties)
{
AuthenticationSchemes = authenticationSchemes;
Properties = properties;
}

/// <summary>
/// The list of authentication components that should handle the authentication challenge
/// invoked by this instance of <see cref="ForbiddenResult"/>.
/// Gets or sets the authentication schemes that are challenged.
/// </summary>
public IList<string> AuthenticationSchemes { get; set; }

/// <summary>
/// <see cref="AuthenticationProperties"/> used to perform authentication.
/// Gets or sets the <see cref="AuthenticationProperties"/> used to perform the authentication challenge.
/// </summary>
public AuthenticationProperties Properties { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNet.Mvc.Core;
using Microsoft.Extensions.Logging;

namespace Microsoft.AspNet.Mvc.Logging
{
/// <summary>
/// Extensions methods for logging <see cref="ForbiddenResult"/> instances.
/// </summary>
public static class ForbiddenResultLoggerExtensions
internal static class ForbiddenResultLoggerExtensions
{
private static readonly Action<ILogger, string[], Exception> _resultExecuting =
LoggerMessage.Define<string[]>(
Expand All @@ -21,6 +17,8 @@ public static class ForbiddenResultLoggerExtensions
formatString: $"Executing {nameof(ForbiddenResult)} with authentication schemes ({{Schemes}}).");

public static void ForbiddenResultExecuting(this ILogger logger, IList<string> authenticationSchemes)
=> _resultExecuting(logger, authenticationSchemes.ToArray(), null);
{
_resultExecuting(logger, authenticationSchemes.ToArray(), null);
}
}
}
33 changes: 33 additions & 0 deletions test/Microsoft.AspNet.Mvc.Core.Test/ForbiddenResultTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public async Task ExecuteResultAsync_InvokesForbiddenAsyncOnAllConfiguredSchemes
};

[Theory]
[MemberData(nameof(ExecuteResultAsync_InvokesForbiddenAsyncWithAuthPropertiesData))]
public async Task ExecuteResultAsync_InvokesForbiddenAsyncWithAuthProperties(AuthenticationProperties expected)
{
// Arrange
Expand All @@ -111,6 +112,38 @@ public async Task ExecuteResultAsync_InvokesForbiddenAsyncWithAuthProperties(Aut
authenticationManager.Verify();
}

[Theory]
[MemberData(nameof(ExecuteResultAsync_InvokesForbiddenAsyncWithAuthPropertiesData))]
public async Task ExecuteResultAsync_InvokesForbiddenAsyncWithAuthProperties_WhenAuthenticationSchemesIsEmpty(
AuthenticationProperties expected)
{
// Arrange
var authenticationManager = new Mock<AuthenticationManager>();
authenticationManager
.Setup(c => c.ForbidAsync(expected))
.Returns(TaskCache.CompletedTask)
.Verifiable();
var httpContext = new Mock<HttpContext>();
httpContext.Setup(c => c.RequestServices).Returns(CreateServices());
httpContext.Setup(c => c.Authentication).Returns(authenticationManager.Object);
var result = new ForbiddenResult(expected)
{
AuthenticationSchemes = new string[0]
};
var routeData = new RouteData();

var actionContext = new ActionContext(
httpContext.Object,
routeData,
new ActionDescriptor());

// Act
await result.ExecuteResultAsync(actionContext);

// Assert
authenticationManager.Verify();
}

private static IServiceProvider CreateServices()
{
return new ServiceCollection()
Expand Down

0 comments on commit 376afbe

Please sign in to comment.