0

I have completed this tutorial on authorization in asp.net razorpages.

I have multiple models and would love to allow the Adminhandler to have access to all of the models. The tutorial is structured in a way which uses the C# interface called AuthorizationHandler.

By nature it only accepts one resource. I am not a master in C#. So what I did is i copied the code of the AuthorizationHandler class to implement my own class which accepts multiple resources.

This what i have thus far. I admit that I am not sharpest nail in the toolbox and am now stuck as what to code into the HandleAsync and HandleRequirementAsync bodies. Please forgive me if my question sounds silly but I am experienced yet in creating custom interfaces.

using System.Diagnostics; using System.Threading.Tasks;

namespace Microsoft.AspNetCore.Authorization {

public abstract class AuthorizationHandler<TRequirement, T1, T2> : IAuthorizationHandler where TRequirement : IAuthorizationRequirement
{
    protected AuthorizationHandler();

  
    [DebuggerStepThrough]
    public virtual Task HandleAsync(AuthorizationHandlerContext context);
    

    protected abstract Task HandleRequirementAsync(AuthorizationHandlerContext context, TRequirement requirement, T1 first, T2 second);
}

}

chris1234
  • 53
  • 6

2 Answers2

1

I have multiple models and would love to allow the Adminhandler to have access to all of the models. The tutorial is structured in a way which uses the C# interface called AuthorizationHandler.

This has been implemented in AuthorizationHandler , just implement the HandleAsync method, you can get all requirements you add from AuthorizationHandlerContext.

public Task HandleAsync(AuthorizationHandlerContext context)
{
    var requirements = context.PendingRequirements.ToList();

    return Task.CompletedTask;
}

For more details, refer to the doc: Use a handler for multiple requirements

mj1313
  • 7,930
  • 2
  • 12
  • 32
  • Thank you @mj1313. I had to add in a bit of my own logic in the code but it worked out. Thanks again for taking the time to read my post and answer my question. I just needed that one bit of advice and now it finally succeeded. – chris1234 Jan 12 '21 at 22:37
0
public class CustomHandler : AuthorizationHandler<CustomRequirement, Tuple<T1, T2>>
cham
  • 21
  • 3
  • 2
    Please read [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). While this code block may answer the OP's question, this answer would be much more useful if you explain how this code is different from the code in the question, what you've changed, why you've changed it and why that solves the problem without introducing others. – Saeed Zhiany Jul 26 '22 at 03:20