Search code examples
c#authorizationasp.net-core-3.0tag-helpers

Reference to type 'IAuthorizeData' claims it is defined in 'Microsoft.AspNetCore.Authorization', but it could not be found


I have an Asp.Net Core 2.2 application. When I migrate to Asp.Net Core 3.0, I'm getting the below error:

Reference to type 'IAuthorizeData' claims it is defined in 'Microsoft.AspNetCore.Authorization', but it could not be found

I'm using TagHelperSamples.Authorization nuget package to optionally render portion of HTML based on user rights and roles.

I'm using the asp-authorize tag helpers from the above nuget package to show menu if user is authorized as shown below:

<div asp-authorize class="collapse navbar-collapse" id="navbarCollapse">

These tag helpers is creating the error on building the project.

I tried adding @using Microsoft.AspNetCore.Authorization in _ViewImports.cshtml but that doesn't work.

Any helps/suggestions on how to fix this or any workarounds?


Solution

  • The TagHelperSamples.Authorization does not fit for asp.net core 3.0 yet,refer to

    https://github.com/dpaquette/TagHelperSamples/issues/77

    You could just remove the package and manually create your authorize tag helper class based on source code in asp.net core 3.0 project:

    using Microsoft.AspNetCore.Authorization;
    using Microsoft.AspNetCore.Authorization.Policy;
    using Microsoft.AspNetCore.Http;
    using Microsoft.AspNetCore.Razor.TagHelpers;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    
    namespace Asp3identityCore.TagHelpers
    {
        [HtmlTargetElement(Attributes = "asp-authorize")]
        [HtmlTargetElement(Attributes = "asp-authorize,asp-policy")]
        [HtmlTargetElement(Attributes = "asp-authorize,asp-roles")]
        [HtmlTargetElement(Attributes = "asp-authorize,asp-authentication-schemes")]
        public class AuthorizationPolicyTagHelper : TagHelper, IAuthorizeData
        {
            private readonly IAuthorizationPolicyProvider _policyProvider;
            private readonly IPolicyEvaluator _policyEvaluator;
            private readonly IHttpContextAccessor _httpContextAccessor;
    
            public AuthorizationPolicyTagHelper(IHttpContextAccessor httpContextAccessor, IAuthorizationPolicyProvider policyProvider, IPolicyEvaluator policyEvaluator)
            {
                _httpContextAccessor = httpContextAccessor;
                _policyProvider = policyProvider;
                _policyEvaluator = policyEvaluator;
            }
    
            /// <summary>
            /// Gets or sets the policy name that determines access to the HTML block.
            /// </summary>
            [HtmlAttributeName("asp-policy")]
            public string Policy { get; set; }
    
            /// <summary>
            /// Gets or sets a comma delimited list of roles that are allowed to access the HTML  block.
            /// </summary>
            [HtmlAttributeName("asp-roles")]
            public string Roles { get; set; }
    
            /// <summary>
            /// Gets or sets a comma delimited list of schemes from which user information is constructed.
            /// </summary>
            [HtmlAttributeName("asp-authentication-schemes")]
            public string AuthenticationSchemes { get; set; }
    
            public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
            {
                var policy = await AuthorizationPolicy.CombineAsync(_policyProvider, new[] { this });
    
                var authenticateResult = await _policyEvaluator.AuthenticateAsync(policy, _httpContextAccessor.HttpContext);
    
                var authorizeResult = await _policyEvaluator.AuthorizeAsync(policy, authenticateResult, _httpContextAccessor.HttpContext, null);
    
                if (!authorizeResult.Succeeded)
                {
                    output.SuppressOutput();
                }
            }
        }
    }