React to http challenge changes
This commit is contained in:
parent
9daf6b48a1
commit
bee20973c7
|
|
@ -47,7 +47,17 @@ namespace Microsoft.AspNet.Mvc
|
||||||
public override void ExecuteResult([NotNull] ActionContext context)
|
public override void ExecuteResult([NotNull] ActionContext context)
|
||||||
{
|
{
|
||||||
var response = context.HttpContext.Response;
|
var response = context.HttpContext.Response;
|
||||||
response.Challenge(Properties, AuthenticationSchemes);
|
if (AuthenticationSchemes.Count > 0)
|
||||||
|
{
|
||||||
|
foreach (var scheme in AuthenticationSchemes)
|
||||||
|
{
|
||||||
|
response.Challenge(Properties, scheme);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
response.Challenge(Properties);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Microsoft.AspNet.Http;
|
using Microsoft.AspNet.Http;
|
||||||
|
using Microsoft.AspNet.Http.Authentication;
|
||||||
using Microsoft.AspNet.Routing;
|
using Microsoft.AspNet.Routing;
|
||||||
using Moq;
|
using Moq;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
@ -13,6 +14,29 @@ namespace Microsoft.AspNet.Mvc.Core.Test.ActionResults
|
||||||
{
|
{
|
||||||
[Fact]
|
[Fact]
|
||||||
public void ChallengeResult_Execute()
|
public void ChallengeResult_Execute()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var result = new ChallengeResult("", null);
|
||||||
|
var httpContext = new Mock<HttpContext>();
|
||||||
|
var httpResponse = new Mock<HttpResponse>();
|
||||||
|
httpContext.Setup(o => o.Response).Returns(httpResponse.Object);
|
||||||
|
|
||||||
|
var routeData = new RouteData();
|
||||||
|
routeData.Routers.Add(Mock.Of<IRouter>());
|
||||||
|
|
||||||
|
var actionContext = new ActionContext(httpContext.Object,
|
||||||
|
routeData,
|
||||||
|
new ActionDescriptor());
|
||||||
|
|
||||||
|
// Act
|
||||||
|
result.ExecuteResult(actionContext);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
httpResponse.Verify(c => c.Challenge(null, ""), Times.Exactly(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void ChallengeResult_ExecuteNoSchemes()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var result = new ChallengeResult(new string[] { }, null);
|
var result = new ChallengeResult(new string[] { }, null);
|
||||||
|
|
@ -31,7 +55,7 @@ namespace Microsoft.AspNet.Mvc.Core.Test.ActionResults
|
||||||
result.ExecuteResult(actionContext);
|
result.ExecuteResult(actionContext);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
httpResponse.Verify(c => c.Challenge(null, (IEnumerable<string>)new string[] { }), Times.Exactly(1));
|
httpResponse.Verify(c => c.Challenge((AuthenticationProperties)null), Times.Exactly(1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -7,6 +7,7 @@ using System.Security.Claims;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNet.Authorization;
|
using Microsoft.AspNet.Authorization;
|
||||||
using Microsoft.AspNet.Http;
|
using Microsoft.AspNet.Http;
|
||||||
|
using Microsoft.AspNet.Http.Authentication;
|
||||||
using Microsoft.AspNet.Routing;
|
using Microsoft.AspNet.Routing;
|
||||||
using Microsoft.AspNet.WebUtilities;
|
using Microsoft.AspNet.WebUtilities;
|
||||||
using Microsoft.Framework.DependencyInjection;
|
using Microsoft.Framework.DependencyInjection;
|
||||||
|
|
@ -278,6 +279,26 @@ namespace Microsoft.AspNet.Mvc.Test
|
||||||
Assert.NotNull(authorizationContext.Result);
|
Assert.NotNull(authorizationContext.Result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Invoke_CanFilterToOnlyBearerScheme()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var authorizeFilter = new AuthorizeFilter(new AuthorizationPolicyBuilder("Bearer")
|
||||||
|
.RequireClaim("Permission", "CanViewPage")
|
||||||
|
.Build());
|
||||||
|
var authorizationContext = GetAuthorizationContext(services =>
|
||||||
|
{
|
||||||
|
services.AddAuthorization();
|
||||||
|
services.AddTransient<IAuthorizationHandler, DenyAnonymousAuthorizationHandler>();
|
||||||
|
});
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await authorizeFilter.OnAuthorizationAsync(authorizationContext);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.NotNull(authorizationContext.Result);
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Invoke_EmptyPolicyWillFail()
|
public async Task Invoke_EmptyPolicyWillFail()
|
||||||
{
|
{
|
||||||
|
|
@ -298,7 +319,7 @@ namespace Microsoft.AspNet.Mvc.Test
|
||||||
|
|
||||||
private AuthorizationContext GetAuthorizationContext(Action<ServiceCollection> registerServices, bool anonymous = false)
|
private AuthorizationContext GetAuthorizationContext(Action<ServiceCollection> registerServices, bool anonymous = false)
|
||||||
{
|
{
|
||||||
var validUser = new ClaimsPrincipal(
|
var basicPrincipal = new ClaimsPrincipal(
|
||||||
new ClaimsIdentity(
|
new ClaimsIdentity(
|
||||||
new Claim[] {
|
new Claim[] {
|
||||||
new Claim("Permission", "CanViewPage"),
|
new Claim("Permission", "CanViewPage"),
|
||||||
|
|
@ -307,13 +328,18 @@ namespace Microsoft.AspNet.Mvc.Test
|
||||||
new Claim(ClaimTypes.NameIdentifier, "John")},
|
new Claim(ClaimTypes.NameIdentifier, "John")},
|
||||||
"Basic"));
|
"Basic"));
|
||||||
|
|
||||||
validUser.AddIdentity(
|
var validUser = basicPrincipal;
|
||||||
new ClaimsIdentity(
|
|
||||||
|
var bearerIdentity = new ClaimsIdentity(
|
||||||
new Claim[] {
|
new Claim[] {
|
||||||
new Claim("Permission", "CupBearer"),
|
new Claim("Permission", "CupBearer"),
|
||||||
new Claim(ClaimTypes.Role, "Token"),
|
new Claim(ClaimTypes.Role, "Token"),
|
||||||
new Claim(ClaimTypes.NameIdentifier, "John Bear")},
|
new Claim(ClaimTypes.NameIdentifier, "John Bear")},
|
||||||
"Bearer"));
|
"Bearer");
|
||||||
|
|
||||||
|
var bearerPrincipal = new ClaimsPrincipal(bearerIdentity);
|
||||||
|
|
||||||
|
validUser.AddIdentity(bearerIdentity);
|
||||||
|
|
||||||
// ServiceProvider
|
// ServiceProvider
|
||||||
var serviceCollection = new ServiceCollection();
|
var serviceCollection = new ServiceCollection();
|
||||||
|
|
@ -332,6 +358,8 @@ namespace Microsoft.AspNet.Mvc.Test
|
||||||
httpContext.Object.User = validUser;
|
httpContext.Object.User = validUser;
|
||||||
}
|
}
|
||||||
httpContext.SetupGet(c => c.RequestServices).Returns(serviceProvider);
|
httpContext.SetupGet(c => c.RequestServices).Returns(serviceProvider);
|
||||||
|
httpContext.Setup(c => c.AuthenticateAsync("Bearer")).ReturnsAsync(new AuthenticationResult(bearerPrincipal, new AuthenticationProperties(), new AuthenticationDescription()));
|
||||||
|
httpContext.Setup(c => c.AuthenticateAsync("Basic")).ReturnsAsync(new AuthenticationResult(basicPrincipal, new AuthenticationProperties(), new AuthenticationDescription()));
|
||||||
|
|
||||||
// AuthorizationContext
|
// AuthorizationContext
|
||||||
var actionContext = new ActionContext(
|
var actionContext = new ActionContext(
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue