0
    [HttpPost]
    [Authorization(permissions: new string[] { "default" })]
    public async Task<IActionResult> Create([FromBody] UserModel userModel)
    {
       
    }
public class AuthorizationAttribute : AuthorizeAttribute, IAsyncAuthorizationFilter
    {
        
        private readonly string[] _permissions;
        private readonly IHttpContextAccessor _httpContextAccessor;

        public AuthorizationAttribute(params string[] permissions)
        {            
            _permissions = permissions;
        }

        public async Task OnAuthorizationAsync(AuthorizationFilterContext context)
        {
            if (_permissions == null || _permissions.Length == 0)
            {
                return;
            }
            //How do I access userModel here
            
        }
        
    }

Basically I would like to access [frombody]userModel in my authorization class so that we can authorize.

phuzi
  • 12,078
  • 3
  • 26
  • 50
amit agarwal
  • 63
  • 2
  • 17
  • What you are trying to do doesn't feel right. Why would authorization be contingent on the request content like this? – phuzi Aug 01 '22 at 10:39
  • We have an Id which is passed through FromBody model class and we would like to authorize based on it. – amit agarwal Aug 01 '22 at 10:43
  • I believe model binding happens after auhorization so you there won't be anything meaningful assigned to the model anyway. You will have to inspect the request body directly. – phuzi Aug 01 '22 at 11:13
  • 1
    check this https://stackoverflow.com/questions/40494913/how-to-read-request-body-in-an-asp-net-core-webapi-controller – negin motalebi Aug 01 '22 at 11:27

0 Answers0