I have upgraded dotnet core 2.2 to 3.preview 7.
So after that, I can't get custom attributes.
context.Resource
in version 2.2 was type of AuthorizationFilterContext
, but in version 3 is type of Microsoft.AspNetCore.Http.Endpoint
.
Now I couldn't get attributes from 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>();
}
}
}