如何获取 dotnet core 3 的路由属性?

2024-01-11

我已将 dotnet core 2.2 升级到 3.preview 7。
所以在那之后,我无法获得自定义属性。
context.Resource在 2.2 版本中是类型AuthorizationFilterContext,但在版本 3 中是类型Microsoft.AspNetCore.Http.Endpoint.

现在我无法从端点获取属性。

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;

namespace Gamma.Core.Security
{
    public abstract class AttributeAuthorizationHandler<TRequirement,     TAttribute>
    : AuthorizationHandler<TRequirement> where TRequirement
    : IAuthorizationRequirement where TAttribute : Attribute
    {
        Microsoft.AspNetCore.Http.IHttpContextAccessor _httpContextAccessor = null;
        public AttributeAuthorizationHandler(Microsoft.AspNetCore.Http.IHttpContextAccessor httpContextAccessor)
        {
            _httpContextAccessor = httpContextAccessor;
        }
        protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, TRequirement requirement)
        {
            var attributes = new List<TAttribute>();

            var action = (context.Resource as AuthorizationFilterContext)?.ActionDescriptor as ControllerActionDescriptor;
            if (context.Resource is Microsoft.AspNetCore.Http.Endpoint endpoint)
            {
                //endpoint.
            }

            if (action != null)
            {
                attributes.AddRange(GetAttributes(action.MethodInfo));
            }

            return HandleRequirementAsync(context, requirement, attributes);
        }

        protected abstract Task HandleRequirementAsync(AuthorizationHandlerContext context, TRequirement requirement, IEnumerable<TAttribute> attributes);

        private static IEnumerable<TAttribute> GetAttributes(MemberInfo memberInfo)
        {
            return memberInfo.GetCustomAttributes(typeof(TAttribute), false).Cast<TAttribute>();
        }
    }
}

我能够从 .NET Core 3.1 获取自定义属性AuthorizationHandlerContext using ControllerActionDescriptor in 布莱恩的回答 https://stackoverflow.com/a/58504298/12858798.

private IEnumerable<TAttribute> GetAttributes<TAttribute>(AuthorizationHandlerContext authContext)
{
    if (authContext.Resource is RouteEndpoint routeEndpoint)
    {
        var actionDescriptor = routeEndpoint.Metadata.OfType<ControllerActionDescriptor>().SingleOrDefault();
        var attributes = actionDescriptor?.MethodInfo.GetCustomAttributes(typeof(TAttribute), false).Cast<TAttribute>();
        return attributes;
    }
    
    return null;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何获取 dotnet core 3 的路由属性? 的相关文章

随机推荐

  • 如何在 Pygame 中插入滑块?

    我目前正在 Python 上进行物理模拟 使用 Pygame 模拟室内的气体云 我的问题是我无法在代码中插入工作滑块来更改参数的值 我有一个运行模拟的 运行时 循环 当我想在其中插入工作滑块时 模拟就会停止 我无法让模拟和滑块同时工作 下面
  • 为什么正则语言的补语仍然是正则语言?

    根据我的教科书 只要L1是正则语言 L1 A L1的补集就是正则语言 A 不是还包括上下文无关语言 上下文相关语言和递归可枚举语言吗 A L1 也将包括所有这些 不是吗 那怎么可能有规律呢 在有限状态机的表示下 我理解为什么补码仍然是常规语
  • 向静态 Azure 网站添加身份验证

    我们有一个 Azure 网站 托管一个静态站点 仅一些 HTML CSS Javascript 然后通过 AJAX 调用与我们的 Azure 移动服务进行通信 我们想向该站点添加一些非常简单的身份验证 只需静态用户名 密码就足够了 请推荐最
  • 如何在 scipy.optimize 中使用 fmin_cg 获得正确的尺寸

    我一直在尝试使用 fmin cg 来最小化逻辑回归的成本函数 xopt fmin cg costFn fprime grad x0 initial theta args X y m maxiter 400 disp True full ou
  • Java foreach 对原语的迭代顺序是否精确定义?

    示例代码 int a new int 0 1 2 3 int result 0 for int i a result i 循环是否保证迭代a 0 a 1 a 2 a 3 以该顺序 我坚信答案是肯定的 但是这一页 http java sun
  • JLabel 仅显示 initComponents() 是否被删除

    MainFrame java JFrame public MainFrame initComponents Letters pl new Letters this this setContentPane pl this setTitle P
  • 无法使用反应钩子读取未定义的属性“映射”

    我试图使用 fortnite api 来显示当前的物品商店 但我收到了如标题所示的错误 当我控制台日志结果时 它显示一个数组 但我无法映射它 为什么 import React useState useEffect from react im
  • 在互操作 C# 期间延迟 Excel 计算

    我必须从 C 程序将数百个单元格输入到 Excel 工作表中 每次我设置一个单元格或一个范围时 Excel都会缓慢响应 大概是在我添加的每个输入之间更新各种输出 有没有办法禁用我的 C 程序的计算 并在完成设置单元格并准备好读取输出后重新启
  • LINQ 有条件添加联接

    我有一个 LINQ 查询 我试图从 2 个表返回数据 但我加入的表是有条件的 这就是我想做的 if teamType A var query from foo in context People join foo2 in context P
  • 在运行时请求管理员权限

    如何在运行时请求管理员权限 以便提示用户允许或禁止 如果您希望它响应按钮 例如 像任务管理器中的 显示所有用户的进程 您必须使用以下方法重新启动您的应用程序 ShellExecute 0 L runas exepath 0 0 SW SHO
  • 如何使用 Visual Studio 为 .net Framework 4.7.2 项目创建 SDK 样式项目?

    我运行的是最新版本的VS2019 16 6 1 然而 Windows 类库的新项目向导仍然创建旧样式的项目 我想用这个方法研究这个问题 https stackoverflow com questions 62251125 target v4
  • 如何使我的本地主机可通过互联网访问

    我在本地计算机上的 JBoss 上托管了一个应用程序 我也可以通过 localhost 或通过我的 IP 访问 现在我希望其他用户可以通过我的 IP 通过互联网访问同一个应用程序 您能让我知道如何配置 JBoss 来处理这些请求吗 http
  • 无法从 WinApi 箱调用 CryptDecrypt,因为它找不到模块

    In the 文档 https docs rs winapi x86 64 pc windows msvc winapi um wincrypt fn CryptDecrypt html它说该函数位于winapi um wincrypt C
  • 如何将 tr 附加到表格顶部

    我如何将新的 tr 附加到表格顶部而不是其他 tr 下 Example table width 100 tr td something td td else here td tr tr td something2 td td else he
  • scala中基于相邻元素之间差异的拆分列表

    我们如何根据相邻元素之间的差异在 scala 中拆分列表 例如 给定 List 1 3 6 10 12 14 和差值 3 该函数将返回 List List 1 3 List 6 List 10 12 14 我们可以使用foldLeft 来做
  • 如何为 Android 中单击的图像按钮制作边框?

    我正在使用 Imagebuttons 在我的 android 项目中显示一些图标
  • 值不能为 null 或为空。\r\n参数名称: name

    当我进入 cshtml 页面时出现此错误 System Web Mvc dll 中发生 System ArgumentException 类型的异常 但未在用户代码中处理 在我的 cshtml 代码中 div class form grou
  • 基于“附近”行值聚合

    我有一个非常混乱的数据框 网络抓取 不幸的是其中有许多双重甚至三重条目 大多数数据框如下所示 gt df1 lt data frame var1 c a a b b c c d d var2 c right a NA right b NA
  • 更新 SQL 中的多列

    有没有办法像使用插入语句一样更新 SQL Server 中的多个列 就像是 Update table1 set a b c d e f g h i j k t2 a t2 b t2 c t2 d t2 e t2 f t2 g t2 h t2
  • 如何获取 dotnet core 3 的路由属性?

    我已将 dotnet core 2 2 升级到 3 preview 7 所以在那之后 我无法获得自定义属性 context Resource在 2 2 版本中是类型AuthorizationFilterContext 但在版本 3 中是类型