Introducing ChallengeResult to call Response.Challenge()

This commit is contained in:
sornaks 2014-05-01 13:27:46 -07:00
parent b29ecea4b5
commit 6b836e9e77
5 changed files with 91 additions and 1 deletions

View File

@ -43,6 +43,11 @@ namespace MvcSample.Web
return Index(age, userName);
}
public ActionResult ChallengeUser(int age = 20, string userName = "SampleUser")
{
return new ChallengeResult();
}
[Authorize("Permission", "CanViewPage")]
public ActionResult NotGrantedClaim(int age = 20, string userName = "SampleUser")
{

View File

@ -0,0 +1,52 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using Microsoft.AspNet.Http.Security;
namespace Microsoft.AspNet.Mvc
{
public class ChallengeResult : ActionResult
{
public ChallengeResult()
: this(new string[] { })
{
}
public ChallengeResult(string authenticationType)
: this(new[] { authenticationType })
{
}
public ChallengeResult(IList<string> authenticationTypes)
: this(authenticationTypes, properties: null)
{
}
public ChallengeResult(AuthenticationProperties properties)
: this(new string[] { }, properties)
{
}
public ChallengeResult(string authenticationType, AuthenticationProperties properties)
: this(new[] { authenticationType }, properties)
{
}
public ChallengeResult(IList<string> authenticationTypes, AuthenticationProperties properties)
{
AuthenticationTypes = authenticationTypes;
Properties = properties;
}
public IList<string> AuthenticationTypes { get; set; }
public AuthenticationProperties Properties { get; set; }
public override void ExecuteResult([NotNull] ActionContext context)
{
var response = context.HttpContext.Response;
response.Challenge(AuthenticationTypes, Properties);
}
}
}

View File

@ -32,6 +32,7 @@
<Compile Include="ActionNameAttribute.cs" />
<Compile Include="ActionResult.cs" />
<Compile Include="ActionResultHelper.cs" />
<Compile Include="ActionResults\ChallengeResult.cs" />
<Compile Include="ActionResults\ContentResult.cs" />
<Compile Include="ActionResults\EmptyResult.cs" />
<Compile Include="ActionResults\HttpStatusCodeResult.cs" />
@ -233,4 +234,4 @@
<Compile Include="ViewDataDictionaryOfT.cs" />
</ItemGroup>
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>
</Project>

View File

@ -0,0 +1,31 @@
using System.Collections.Generic;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Routing;
using Moq;
using Xunit;
namespace Microsoft.AspNet.Mvc.Core.Test.ActionResults
{
public class ChallengeResultTest
{
[Fact]
public void ChallengeResult_Execute()
{
// Arrange
var result = new ChallengeResult(new string[] { }, null);
var httpContext = new Mock<HttpContext>();
var httpResponse = new Mock<HttpResponse>();
httpContext.Setup(o => o.Response).Returns(httpResponse.Object);
var actionContext = new ActionContext(httpContext.Object,
Mock.Of<IRouter>(),
new Dictionary<string, object>(),
new ActionDescriptor());
// Act
result.ExecuteResult(actionContext);
// Assert
httpResponse.Verify(c => c.Challenge(new string[] { }, null), Times.Exactly(1));
}
}
}

View File

@ -24,6 +24,7 @@
<Compile Include="ActionAttributeTests.cs" />
<Compile Include="ActionExecutorTests.cs" />
<Compile Include="ActionResults\ObjectContentResultTests.cs" />
<Compile Include="ActionResults\ChallengeResultTest.cs" />
<Compile Include="ActionResults\RedirectToActionResultTest.cs" />
<Compile Include="ActionResults\RedirectToRouteResultTest.cs" />
<Compile Include="ActionResults\RedirectResultTest.cs" />