Last active
February 13, 2017 18:58
-
-
Save DejanMilicic/678b49b4465047d0a27a0335111bfd92 to your computer and use it in GitHub Desktop.
Tag Helper : required-claims
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
using System.Linq; | |
using Microsoft.AspNetCore.Http; | |
using Microsoft.AspNetCore.Razor.TagHelpers; | |
namespace Infrastructure.MVC | |
{ | |
[HtmlTargetElement(Attributes = "required-claim")] | |
public class RequiredClaimsTagHelper : TagHelper | |
{ | |
private readonly IHttpContextAccessor httpContextAccessor; | |
public RequiredClaimsTagHelper(IHttpContextAccessor httpContextAccessor) | |
{ | |
this.httpContextAccessor = httpContextAccessor; | |
} | |
[HtmlAttributeName("required-claim")] | |
public string RequiredClaim { get; set; } | |
public override void Process(TagHelperContext context, TagHelperOutput output) | |
{ | |
if (null == httpContextAccessor.HttpContext.User.Claims.FirstOrDefault(claim => claim.Type == this.RequiredClaim)) | |
{ | |
output.SuppressOutput(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use it on any html element to hide it if current user does not have required claim
<a required-claim="admin" asp-controller="Home" asp-action="About">About</a>
Concerns