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

Commit

Permalink
Added Permanent property to configure RequireHttpsAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
khellang committed May 12, 2016
1 parent fb5d92b commit 8d6a565
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Microsoft.AspNetCore.Mvc.Core/RequireHttpsAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ namespace Microsoft.AspNetCore.Mvc
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
public class RequireHttpsAttribute : Attribute, IAuthorizationFilter, IOrderedFilter
{
/// <summary>
/// Specifies whether a permanent redirect, <c>301 Moved Permanently</c>,
/// should be used instead of a temporary redirect, <c>302 Found</c>.
/// </summary>
public bool Permanent { get; set; }

/// <inheritdoc />
public int Order { get; set; }

Expand Down Expand Up @@ -84,7 +90,7 @@ protected virtual void HandleNonHttpsRequest(AuthorizationFilterContext filterCo
request.QueryString.ToUriComponent());

// redirect to HTTPS version of page
filterContext.Result = new RedirectResult(newUrl, permanent: false);
filterContext.Result = new RedirectResult(newUrl, Permanent);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,27 @@ public void OnAuthorization_RedirectsToHttpsEndpoint_ForCustomSslPort(
Assert.Equal(expectedUrl, result.Url);
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public void OnAuthorization_RedirectsToHttpsEndpoint_WithSpecifiedStatusCode(bool permanent)
{
var requestContext = new DefaultHttpContext();
requestContext.RequestServices = CreateServices();
requestContext.Request.Scheme = "http";
requestContext.Request.Method = "GET";

var authContext = CreateAuthorizationContext(requestContext);
var attr = new RequireHttpsAttribute { Permanent = permanent };

// Act
attr.OnAuthorization(authContext);

// Assert
var result = Assert.IsType<RedirectResult>(authContext.Result);
Assert.Equal(permanent, result.Permanent);
}

private class CustomRequireHttpsAttribute : RequireHttpsAttribute
{
protected override void HandleNonHttpsRequest(AuthorizationFilterContext filterContext)
Expand Down

0 comments on commit 8d6a565

Please sign in to comment.