From 6b836e9e7734ec9d38e4a93a9e7a40b6d34e4849 Mon Sep 17 00:00:00 2001 From: sornaks Date: Thu, 1 May 2014 13:27:46 -0700 Subject: [PATCH] Introducing ChallengeResult to call Response.Challenge() --- samples/MvcSample.Web/FiltersController.cs | 5 ++ .../ActionResults/ChallengeResult.cs | 52 +++++++++++++++++++ .../Microsoft.AspNet.Mvc.Core.kproj | 3 +- .../ActionResults/ChallengeResultTest.cs | 31 +++++++++++ .../Microsoft.AspNet.Mvc.Core.Test.kproj | 1 + 5 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 src/Microsoft.AspNet.Mvc.Core/ActionResults/ChallengeResult.cs create mode 100644 test/Microsoft.AspNet.Mvc.Core.Test/ActionResults/ChallengeResultTest.cs diff --git a/samples/MvcSample.Web/FiltersController.cs b/samples/MvcSample.Web/FiltersController.cs index eb835f67df..3548ff303a 100644 --- a/samples/MvcSample.Web/FiltersController.cs +++ b/samples/MvcSample.Web/FiltersController.cs @@ -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") { diff --git a/src/Microsoft.AspNet.Mvc.Core/ActionResults/ChallengeResult.cs b/src/Microsoft.AspNet.Mvc.Core/ActionResults/ChallengeResult.cs new file mode 100644 index 0000000000..a4ae477e5b --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.Core/ActionResults/ChallengeResult.cs @@ -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 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 authenticationTypes, AuthenticationProperties properties) + { + AuthenticationTypes = authenticationTypes; + Properties = properties; + } + + public IList AuthenticationTypes { get; set; } + + public AuthenticationProperties Properties { get; set; } + + public override void ExecuteResult([NotNull] ActionContext context) + { + var response = context.HttpContext.Response; + response.Challenge(AuthenticationTypes, Properties); + } + } +} diff --git a/src/Microsoft.AspNet.Mvc.Core/Microsoft.AspNet.Mvc.Core.kproj b/src/Microsoft.AspNet.Mvc.Core/Microsoft.AspNet.Mvc.Core.kproj index eac12b055f..e2a77b035b 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Microsoft.AspNet.Mvc.Core.kproj +++ b/src/Microsoft.AspNet.Mvc.Core/Microsoft.AspNet.Mvc.Core.kproj @@ -32,6 +32,7 @@ + @@ -233,4 +234,4 @@ - \ No newline at end of file + diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/ActionResults/ChallengeResultTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/ActionResults/ChallengeResultTest.cs new file mode 100644 index 0000000000..0828288d2d --- /dev/null +++ b/test/Microsoft.AspNet.Mvc.Core.Test/ActionResults/ChallengeResultTest.cs @@ -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(); + var httpResponse = new Mock(); + httpContext.Setup(o => o.Response).Returns(httpResponse.Object); + var actionContext = new ActionContext(httpContext.Object, + Mock.Of(), + new Dictionary(), + new ActionDescriptor()); + + // Act + result.ExecuteResult(actionContext); + + // Assert + httpResponse.Verify(c => c.Challenge(new string[] { }, null), Times.Exactly(1)); + } + } +} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/Microsoft.AspNet.Mvc.Core.Test.kproj b/test/Microsoft.AspNet.Mvc.Core.Test/Microsoft.AspNet.Mvc.Core.Test.kproj index 8480330b3c..f651e32649 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/Microsoft.AspNet.Mvc.Core.Test.kproj +++ b/test/Microsoft.AspNet.Mvc.Core.Test/Microsoft.AspNet.Mvc.Core.Test.kproj @@ -24,6 +24,7 @@ +