引用'IAuthorizeData'类型声称它在'Microsoft.AspNetCore.Authorization'中定义,但找不到]]

问题描述 投票:0回答:1

我有一个Asp.Net Core 2.2应用程序。当我迁移到Asp.Net Core 3.0时,出现以下错误:

参考类型'IAuthorizeData'的声明是在'Microsoft.AspNetCore.Authorization',但找不到]

我正在使用TagHelperSamples.Authorization nuget包根据用户权限和角色来选择呈现HTML的一部分。

我正在使用上述nuget包中的asp-authorize标签帮助程序来显示菜单,如果用户得到如下所示的授权:

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

这些标签帮助程序在构建项目时产生错误。

我尝试在@using Microsoft.AspNetCore.Authorization中添加_ViewImports.cshtml,但这不起作用。

有关如何解决此问题或任何变通办法的任何帮助/建议?

我有一个Asp.Net Core 2.2应用程序。当我迁移到Asp.Net Core 3.0时,出现以下错误:对类型“ IAuthorizeData”的引用声称它是在“ Microsoft.AspNetCore ....]中定义的。

TagHelperSamples.Authorization还不适合asp.net core 3.0,请参阅
https://github.com/dpaquette/TagHelperSamples/issues/77
您可以删除该包,并基于asp.net core 3.0项目中的source code手动创建您的授权标签帮助程序类:

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(); } } } }

c# authorization asp.net-core-3.0 tag-helpers
1个回答
0
投票
您可以删除该包,并基于asp.net core 3.0项目中的source code手动创建您的授权标签帮助程序类:
© www.soinside.com 2019 - 2024. All rights reserved.