Ich weiß, dass Sie in Controllern [Authorize("policyName")]
ohne Probleme schreiben können, aber gibt es eine Möglichkeit, eine Richtlinie in einer Ansicht zu verwenden? Ich möchte User.IsInRole(...)
nicht jedes Mal verwenden, wenn ich HTML-Code autorisieren möchte.
Bearbeiten:
Hier ist ein Code
Startup.cs - Grundsatzerklärung
services.AddAuthorization(options =>
{
options.AddPolicy("testPolicy", policy =>
{
policy.RequireAuthenticatedUser()
.RequireRole("RoleOne", "RoleTwo", "RoleThree")
.RequireClaim(ClaimTypes.Email);
});
});
Admin Controller
[Authorize("testPolicy")]
public class AdminController : Controller
{
public IActionResult Index()
{
return View();
}
}
Navbar HTML
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li><a asp-controller="Home" asp-action="Index">Home</a></li>
<!-- I want to implement my policy here. -->
@if (User.IsInRole("..."))
{
<li><a asp-controller="Admin" asp-action="Index">Admin</a></li>
}
</ul>
@await Html.PartialAsync("_LoginPartial")
</div>
</div>
Ich habe diesen Link gefunden, der hilfreich sein kann: https://docs.asp.net/en/latest/security/authorization/views.html
Beispiele von dieser Seite:
@if (await AuthorizationService.AuthorizeAsync(User, "PolicyName"))
{
<p>This paragraph is displayed because you fulfilled PolicyName.</p>
}
In einigen Fällen ist die Ressource Ihr Ansichtsmodell, und Sie können AuthorizeAsync genauso aufrufen, wie Sie es bei der ressourcenbasierten Autorisierung überprüfen würden.
@if (await AuthorizationService.AuthorizeAsync(User, Model, Operations.Edit))
{
<p><a class="btn btn-default" role="button"
href="@Url.Action("Edit", "Document", new {id= Model.Id})">Edit</a></p>
}
Am Ende habe ich einen Tag-Helfer erstellt, um das Element, mit dem es verknüpft ist, bedingt auszublenden.
[HtmlTargetElement(Attributes = "policy")]
public class PolicyTagHelper : TagHelper
{
private readonly IAuthorizationService _authService;
private readonly ClaimsPrincipal _principal;
public PolicyTagHelper(IAuthorizationService authService, IHttpContextAccessor httpContextAccessor)
{
_authService = authService;
_principal = httpContextAccessor.HttpContext.User;
}
public string Policy { get; set; }
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
// if (!await _authService.AuthorizeAsync(_principal, Policy)) ASP.NET Core 1.x
if (!(await _authService.AuthorizeAsync(_principal, Policy)).Succeeded)
output.SuppressOutput();
}
}
Verwendungszweck
<li policy="testPolicy"><a asp-controller="Admin" asp-action="Index">Admin</a></li>
Dies ist eine der großen Verbesserungen in ASP Core, wenn Sie die Identität allen Seiten in der Startdatei hinzufügen können:
@if (User.IsInRole("Admin"))
{
<p>
<a asp-action="Create" asp-controller="MyController">Create New</a>
</p>
}
In Startup.cs:
services.AddIdentity<ApplicationUser, IdentityRole>()
EDIT: Ok Ich habe den Beitrag falsch gelesen, das wusstest du schon :) - lass ihn trotzdem, wenn jemand ihn benutzen kann.