Last active
May 21, 2020 19:22
-
-
Save demaderios/34bb42851a6ea9583eb9eb67a2398ef1 to your computer and use it in GitHub Desktop.
Custom .Net Core policy using Func
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
public static void ConfigureAuthorization(this IServiceCollection services) | |
{ | |
services.AddAuthorization(options => | |
{ | |
options.AddPolicy("CanAccessSuperTopSecretArea", policy => | |
{ | |
context => context.User.HasClaim(claim => claim.Type == "TopSecretUser" || claim.Type == "OtherTopSecretUser" || | |
context.User.IsInRole("SupremeOverlord")) | |
} | |
} | |
} |
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
public class TopSecretController : Controller | |
{ | |
[Authorize(Policy = "CanAccessSuperTopSecretArea")] | |
public IActionResult Index() | |
{ | |
return View(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This authorization service extension (to avoid cluttering up Startup.cs) creates a policy called "CanAccessSuperTopSecretArea". A user in this policy must have the claim "TopSecretUser" OR "OtherTopSecretUser" OR is in the role "SupremeOverlord".