第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何獲取 dotnet core 3 的路由屬性?

如何獲取 dotnet core 3 的路由屬性?

C#
滄海一幻覺 2023-07-23 16:47:57
我已將 dotnet core 2.2 升級(jí)到 3.preview 7。因此之后,我無法獲取自定義屬性。context.Resource在版本 2.2 中是類型 of AuthorizationFilterContext,但在版本 3 中是類型 of Microsoft.AspNetCore.Http.Endpoint。現(xiàn)在我無法從端點(diǎn)獲取屬性。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>();        }    }}
查看完整描述

4 回答

?
狐的傳說

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超3個(gè)贊

我能夠通過在AuthorizationHandlerContext中使用來獲取 .NET Core 3.1 中的自定義屬性。ControllerActionDescriptor


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;

}




查看完整回答
反對(duì) 回復(fù) 2023-07-23
?
千巷貓影

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超7個(gè)贊

從 .net 5 開始,上下文是HttpContext

GetEndPoint方法擴(kuò)展HttpContext

protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, TRequirement requirement)

{

? ? if(context is HttpContext httContext){

? ? ? ? var endpoint = context.GetEndPoint();

? ? } else {

? ? ? ? throw ... // let's see how things will move with next version of .net?

? ? }

}


查看完整回答
反對(duì) 回復(fù) 2023-07-23
?
元芳怎么了

TA貢獻(xiàn)1798條經(jīng)驗(yàn) 獲得超7個(gè)贊

有一個(gè)部分“在處理程序中訪問 MVC 請(qǐng)求上下文”,但這是錯(cuò)誤的。

然而,在“反饋”下,他們的 2 個(gè)問題看起來與最有希望的答案相關(guān),即將下面的代碼添加到AuthorizationHandler.

這可以訪問controllerActionDescriptor,但正如你所看到的,我已經(jīng)查看了各種屬性,但沒有一個(gè)屬性給我當(dāng)前的路由數(shù)據(jù):

var controllerActionDescriptor = routeEndpoint.Metadata

? ? .OfType<ControllerActionDescriptor>()

? ? .SingleOrDefault();


if (controllerActionDescriptor != null)

{

? ? var a = controllerActionDescriptor.AttributeRouteInfo;

? ? var p = controllerActionDescriptor.Parameters;

? ? var ep = controllerActionDescriptor.EndpointMetadata;

? ? var r = controllerActionDescriptor.RouteValues;

}


查看完整回答
反對(duì) 回復(fù) 2023-07-23
?
慕萊塢森

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超4個(gè)贊

我找到了解決方案,在ConfigureServices中將IHttpContextAccessor注冊(cè)到IOC中


services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

然后在AttributeAuthorizationHandler中獲取


public class AccountLoginAuthorizeHandler : AttributeAuthorizationHandler<AccountLoginAuthorizationRequirement, AccountLoginAttribute>

{

    private readonly IHttpContextAccessor _httpContextAccessor;

    public PermissionAuthorizeHandler(IHttpContextAccessor httpContextAccessor, IZaabeeRedisClient redisClient,

    IOptions<LoginConfig> loginConfig)

    {

        _httpContextAccessor = httpContextAccessor ?? throw new ArgumentNullException(nameof(httpContextAccessor));

    }

    protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, AccountLoginAuthorizationRequirement requirement, IEnumerable<AccountLoginAttribute> attributes)

    {

        if (context.Resource is AuthorizationFilterContext filterContext)

        {

            var httpContext = _httpContextAccessor.HttpContext;

            //Do Something

        }

        else

            //Do Something


        context.Succeed(requirement);

        return Task.CompletedTask;

    }

}


查看完整回答
反對(duì) 回復(fù) 2023-07-23
  • 4 回答
  • 0 關(guān)注
  • 287 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)