-
Notifications
You must be signed in to change notification settings - Fork 10.4k
No-op Authorization middleware for Razor Pages #7028
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/Mvc/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorPagesWithEndpointRoutingTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Net; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace Microsoft.AspNetCore.Mvc.FunctionalTests | ||
{ | ||
public class RazorPagesWithEndpointRoutingTest : IClassFixture<MvcTestFixture<RazorPagesWebSite.StartupWithEndpointRouting>> | ||
{ | ||
public RazorPagesWithEndpointRoutingTest(MvcTestFixture<RazorPagesWebSite.StartupWithEndpointRouting> fixture) | ||
{ | ||
Client = fixture.CreateDefaultClient(); | ||
} | ||
|
||
public HttpClient Client { get; } | ||
|
||
[Fact] | ||
public async Task Authorize_AppliedUsingConvention_Works() | ||
{ | ||
// Act | ||
var response = await Client.GetAsync("/Conventions/AuthFolder"); | ||
|
||
// Assert | ||
await response.AssertStatusCodeAsync(HttpStatusCode.Redirect); | ||
Assert.Equal("/Login?ReturnUrl=%2FConventions%2FAuthFolder", response.Headers.Location.PathAndQuery); | ||
} | ||
|
||
[Fact] | ||
public async Task Authorize_AppliedUsingConvention_CanByOverridenByAllowAnonymousAppliedToModel() | ||
{ | ||
// Act | ||
var response = await Client.GetAsync("/Conventions/AuthFolder/AnonymousViaModel"); | ||
|
||
// Assert | ||
await response.AssertStatusCodeAsync(HttpStatusCode.OK); | ||
|
||
var content = await response.Content.ReadAsStringAsync(); | ||
Assert.Equal("Hello from Anonymous", content.Trim()); | ||
} | ||
|
||
[Fact] | ||
public async Task Authorize_AppliedUsingAttributeOnModel_Works() | ||
{ | ||
// Act | ||
var response = await Client.GetAsync("/ModelWithAuthFilter"); | ||
|
||
// Assert | ||
await response.AssertStatusCodeAsync(HttpStatusCode.Redirect); | ||
Assert.Equal("/Login?ReturnUrl=%2FModelWithAuthFilter", response.Headers.Location.PathAndQuery); | ||
} | ||
|
||
[Fact] | ||
public async Task Authorize_WithEndpointRouting_WorksForControllers() | ||
{ | ||
// Act | ||
var response = await Client.GetAsync("/AuthorizedAction/Index"); | ||
|
||
// Assert | ||
await response.AssertStatusCodeAsync(HttpStatusCode.Redirect); | ||
Assert.Equal("/Login?ReturnUrl=%2FAuthorizedAction%2FIndex", response.Headers.Location.PathAndQuery); | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/Mvc/test/WebSites/RazorPagesWebSite/Controllers/AuthorizedActionController.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace RazorPagesWebSite.Controllers | ||
{ | ||
[Route("[controller]/[action]")] | ||
[Authorize] | ||
public class AuthorizedActionController : Controller | ||
{ | ||
public IActionResult Index() => Ok(); | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
36 changes: 36 additions & 0 deletions
36
src/Mvc/test/WebSites/RazorPagesWebSite/StartupWithEndpointRouting.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using Microsoft.AspNetCore.Authentication.Cookies; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace RazorPagesWebSite | ||
{ | ||
public class StartupWithEndpointRouting | ||
{ | ||
public void ConfigureServices(IServiceCollection services) | ||
{ | ||
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) | ||
.AddCookie(options => options.LoginPath = "/Login"); | ||
|
||
services.AddMvc() | ||
.AddRazorPagesOptions(options => | ||
{ | ||
options.Conventions.AuthorizeFolder("/Admin"); | ||
}) | ||
.SetCompatibilityVersion(CompatibilityVersion.Latest); | ||
} | ||
|
||
public void Configure(IApplicationBuilder app) | ||
{ | ||
app.UseRouting(routes => | ||
{ | ||
routes.MapApplication(); | ||
}); | ||
|
||
app.UseAuthorization(); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess what I expected to see was for
AuthorizeFilter
(MVC) to ignoreAuthorizationMiddlewareInvokedKey
- so that we always run authorization twice. This should be a little bit slower, but it's is close to what we shipped in preview1 that didn't have this bug.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Summing up offline conversation: Authenticate \ Authorize has side effects and I wanted to avoid running it twice if we could help it. This does work correctly for the particular scenario that is broken. I've added an additional test with controllers to make sure that works correctly too so we've covered all the bases.