-
Notifications
You must be signed in to change notification settings - Fork 25.2k
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
Can't apply [Authorize] on page handler methods #18676
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
c1950d0
Can't apply [Authorize] on page handler methods
Rick-Anderson 2a33764
Can't apply [Authorize] on page handler methods
Rick-Anderson 29e77e6
add compose warning
Rick-Anderson a95a595
add compose warning
Rick-Anderson 768a1df
add compose warning
Rick-Anderson de17279
add sample
Rick-Anderson 1d0c22e
add sample
Rick-Anderson f64f8ea
add sample
Rick-Anderson 85e586e
add sample
Rick-Anderson 8bcdf1d
add sample
Rick-Anderson b05ee73
Update AuthorizeIndexPageHandlerFilter.cs
pranavkm 9f7f696
Update AuthorizeIndexPageHandlerFilter.cs
pranavkm 0e34c01
Update Index.cshtml.cs
pranavkm 6eb3e1a
Update Startup.cs
pranavkm 36e16d4
work
Rick-Anderson fc6d0d4
Merge branch 'rp/auth/page/handlers/ra' of https://github.com/aspnet/…
Rick-Anderson 413a30f
work
Rick-Anderson cc3e5f3
work
Rick-Anderson 535dfea
Update aspnetcore/security/authorization/simple.md
Rick-Anderson 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 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 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
106 changes: 106 additions & 0 deletions
106
...urity/authorization/simple/samples/3.1/PageHandlerAuth/AuthorizeIndexPageHandlerFilter.cs
This file contains 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,106 @@ | ||
using Microsoft.AspNetCore.Authentication; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Authorization.Policy; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.Filters; | ||
using System; | ||
using System.Reflection; | ||
using System.Threading.Tasks; | ||
|
||
namespace WebApplication22 | ||
{ | ||
#region snippet | ||
public class AuthorizeIndexPageHandlerFilter : IAsyncPageFilter, IOrderedFilter | ||
{ | ||
private readonly IAuthorizationPolicyProvider policyProvider; | ||
private readonly IPolicyEvaluator policyEvaluator; | ||
|
||
public AuthorizeIndexPageHandlerFilter( | ||
IAuthorizationPolicyProvider policyProvider, | ||
IPolicyEvaluator policyEvaluator) | ||
{ | ||
this.policyProvider = policyProvider; | ||
this.policyEvaluator = policyEvaluator; | ||
} | ||
|
||
// Run late in the selection pipeline | ||
public int Order => 10000; | ||
|
||
public Task OnPageHandlerExecutionAsync(PageHandlerExecutingContext context, PageHandlerExecutionDelegate next) => next(); | ||
|
||
public async Task OnPageHandlerSelectionAsync(PageHandlerSelectedContext context) | ||
{ | ||
var attribute = context.HandlerMethod?.MethodInfo?.GetCustomAttribute<AuthorizePageHandlerAttribute>(); | ||
if (attribute is null) | ||
{ | ||
return; | ||
} | ||
|
||
var policy = await AuthorizationPolicy.CombineAsync(policyProvider, new[] { attribute }); | ||
if (policy is null) | ||
{ | ||
return; | ||
} | ||
|
||
await AuthorizeAsync(context, policy); | ||
} | ||
|
||
#region AuthZ - do not change | ||
private async Task AuthorizeAsync(ActionContext actionContext, AuthorizationPolicy policy) | ||
{ | ||
var httpContext = actionContext.HttpContext; | ||
var authenticateResult = await policyEvaluator.AuthenticateAsync(policy, httpContext); | ||
var authorizeResult = await policyEvaluator.AuthorizeAsync(policy, authenticateResult, httpContext, actionContext.ActionDescriptor); | ||
if (authorizeResult.Challenged) | ||
{ | ||
if (policy.AuthenticationSchemes.Count > 0) | ||
{ | ||
foreach (var scheme in policy.AuthenticationSchemes) | ||
{ | ||
await httpContext.ChallengeAsync(scheme); | ||
} | ||
} | ||
else | ||
{ | ||
await httpContext.ChallengeAsync(); | ||
} | ||
|
||
return; | ||
} | ||
else if (authorizeResult.Forbidden) | ||
{ | ||
if (policy.AuthenticationSchemes.Count > 0) | ||
{ | ||
foreach (var scheme in policy.AuthenticationSchemes) | ||
{ | ||
await httpContext.ForbidAsync(scheme); | ||
} | ||
} | ||
else | ||
{ | ||
await httpContext.ForbidAsync(); | ||
} | ||
|
||
return; | ||
} | ||
} | ||
#endregion | ||
} | ||
#endregion | ||
} | ||
|
||
|
||
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] | ||
public class AuthorizePageHandlerAttribute : Attribute, IAuthorizeData | ||
{ | ||
public AuthorizePageHandlerAttribute(string policy = null) | ||
{ | ||
Policy = policy; | ||
} | ||
|
||
public string Policy { get; set; } | ||
|
||
public string Roles { get; set; } | ||
|
||
public string AuthenticationSchemes { get; set; } | ||
} |
5 changes: 5 additions & 0 deletions
5
...y/authorization/simple/samples/3.1/PageHandlerAuth/Pages/AuthorizePageHandlerAttribute.cs
This file contains 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,5 @@ | ||
using System; | ||
|
||
namespace WebApplication22.Pages | ||
{ | ||
} |
26 changes: 26 additions & 0 deletions
26
aspnetcore/security/authorization/simple/samples/3.1/PageHandlerAuth/Pages/Error.cshtml
This file contains 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,26 @@ | ||
@page | ||
@model ErrorModel | ||
@{ | ||
ViewData["Title"] = "Error"; | ||
} | ||
|
||
<h1 class="text-danger">Error.</h1> | ||
<h2 class="text-danger">An error occurred while processing your request.</h2> | ||
|
||
@if (Model.ShowRequestId) | ||
{ | ||
<p> | ||
<strong>Request ID:</strong> <code>@Model.RequestId</code> | ||
</p> | ||
} | ||
|
||
<h3>Development Mode</h3> | ||
<p> | ||
Swapping to the <strong>Development</strong> environment displays detailed information about the error that occurred. | ||
</p> | ||
<p> | ||
<strong>The Development environment shouldn't be enabled for deployed applications.</strong> | ||
It can result in displaying sensitive information from exceptions to end users. | ||
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong> | ||
and restarting the app. | ||
</p> |
31 changes: 31 additions & 0 deletions
31
aspnetcore/security/authorization/simple/samples/3.1/PageHandlerAuth/Pages/Error.cshtml.cs
This file contains 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,31 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.RazorPages; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace WebApplication22.Pages | ||
{ | ||
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] | ||
public class ErrorModel : PageModel | ||
{ | ||
public string RequestId { get; set; } | ||
|
||
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId); | ||
|
||
private readonly ILogger<ErrorModel> _logger; | ||
|
||
public ErrorModel(ILogger<ErrorModel> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
public void OnGet() | ||
{ | ||
RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier; | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
aspnetcore/security/authorization/simple/samples/3.1/PageHandlerAuth/Pages/Index.cshtml
This file contains 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,10 @@ | ||
@page | ||
@model IndexModel | ||
@{ | ||
ViewData["Title"] = "Home page"; | ||
} | ||
|
||
<div class="text-center"> | ||
<h1 class="display-4">Welcome</h1> | ||
<p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p> | ||
</div> |
35 changes: 35 additions & 0 deletions
35
aspnetcore/security/authorization/simple/samples/3.1/PageHandlerAuth/Pages/Index.cshtml.cs
This file contains 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,35 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.RazorPages; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace WebApplication22.Pages | ||
{ | ||
#region snippet | ||
[TypeFilter(typeof(AuthorizeIndexPageHandlerFilter))] | ||
public class IndexModel : PageModel | ||
{ | ||
private readonly ILogger<IndexModel> _logger; | ||
|
||
public IndexModel(ILogger<IndexModel> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
public void OnGet() | ||
{ | ||
|
||
} | ||
|
||
public void OnPost() | ||
{ | ||
|
||
} | ||
|
||
[AuthorizePageHandler] | ||
public void OnPostAuthorized() | ||
{ | ||
|
||
} | ||
} | ||
#endregion | ||
} |
8 changes: 8 additions & 0 deletions
8
aspnetcore/security/authorization/simple/samples/3.1/PageHandlerAuth/Pages/Privacy.cshtml
This file contains 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,8 @@ | ||
@page | ||
@model PrivacyModel | ||
@{ | ||
ViewData["Title"] = "Privacy Policy"; | ||
} | ||
<h1>@ViewData["Title"]</h1> | ||
|
||
<p>Use this page to detail your site's privacy policy.</p> |
24 changes: 24 additions & 0 deletions
24
aspnetcore/security/authorization/simple/samples/3.1/PageHandlerAuth/Pages/Privacy.cshtml.cs
This file contains 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,24 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.RazorPages; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace WebApplication22.Pages | ||
{ | ||
public class PrivacyModel : PageModel | ||
{ | ||
private readonly ILogger<PrivacyModel> _logger; | ||
|
||
public PrivacyModel(ILogger<PrivacyModel> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
public void OnGet() | ||
{ | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...ore/security/authorization/simple/samples/3.1/PageHandlerAuth/Pages/Shared/_Layout.cshtml
This file contains 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,50 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>@ViewData["Title"] - WebApplication22</title> | ||
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" /> | ||
<link rel="stylesheet" href="~/css/site.css" /> | ||
</head> | ||
<body> | ||
<header> | ||
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3"> | ||
<div class="container"> | ||
<a class="navbar-brand" asp-area="" asp-page="/Index">WebApplication22</a> | ||
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent" | ||
aria-expanded="false" aria-label="Toggle navigation"> | ||
<span class="navbar-toggler-icon"></span> | ||
</button> | ||
<div class="navbar-collapse collapse d-sm-inline-flex flex-sm-row-reverse"> | ||
<ul class="navbar-nav flex-grow-1"> | ||
<li class="nav-item"> | ||
<a class="nav-link text-dark" asp-area="" asp-page="/Index">Home</a> | ||
</li> | ||
<li class="nav-item"> | ||
<a class="nav-link text-dark" asp-area="" asp-page="/Privacy">Privacy</a> | ||
</li> | ||
</ul> | ||
</div> | ||
</div> | ||
</nav> | ||
</header> | ||
<div class="container"> | ||
<main role="main" class="pb-3"> | ||
@RenderBody() | ||
</main> | ||
</div> | ||
|
||
<footer class="border-top footer text-muted"> | ||
<div class="container"> | ||
© 2020 - WebApplication22 - <a asp-area="" asp-page="/Privacy">Privacy</a> | ||
</div> | ||
</footer> | ||
|
||
<script src="~/lib/jquery/dist/jquery.min.js"></script> | ||
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script> | ||
<script src="~/js/site.js" asp-append-version="true"></script> | ||
|
||
@RenderSection("Scripts", required: false) | ||
</body> | ||
</html> |
2 changes: 2 additions & 0 deletions
2
...rization/simple/samples/3.1/PageHandlerAuth/Pages/Shared/_ValidationScriptsPartial.cshtml
This file contains 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,2 @@ | ||
<script src="~/lib/jquery-validation/dist/jquery.validate.min.js"></script> | ||
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script> |
3 changes: 3 additions & 0 deletions
3
...tcore/security/authorization/simple/samples/3.1/PageHandlerAuth/Pages/_ViewImports.cshtml
This file contains 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,3 @@ | ||
@using WebApplication22 | ||
@namespace WebApplication22.Pages | ||
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers |
3 changes: 3 additions & 0 deletions
3
aspnetcore/security/authorization/simple/samples/3.1/PageHandlerAuth/Pages/_ViewStart.cshtml
This file contains 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,3 @@ | ||
@{ | ||
Layout = "_Layout"; | ||
} |
26 changes: 26 additions & 0 deletions
26
aspnetcore/security/authorization/simple/samples/3.1/PageHandlerAuth/Program.cs
This file contains 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,26 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace WebApplication22 | ||
{ | ||
public class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
CreateHostBuilder(args).Build().Run(); | ||
} | ||
|
||
public static IHostBuilder CreateHostBuilder(string[] args) => | ||
Host.CreateDefaultBuilder(args) | ||
.ConfigureWebHostDefaults(webBuilder => | ||
{ | ||
webBuilder.UseStartup<Startup>(); | ||
}); | ||
} | ||
} |
Oops, something went wrong.
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.
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 edited this to be more inline with what was shared in the issue (based on the initial commit). Could you move this to a new file? If you're able to try it out, that would be great. I edited this on GitHub, so I'm not sure if all this compiles \ works correctly.