SignalR Custom Authorize Attribute

249 Views Asked by At

I am trying to implement custom Authorize attribute for .net 5 SignalR component. When I use the attribute, It is being ignored and doesn't get into the custom Authorize class.

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
    public class IdentitySignalRAuthorizeAttribute : Microsoft.AspNet.SignalR.AuthorizeAttribute
    {
        private Role[] _roles;
        public IdentitySignalRAuthorizeAttribute(params Role[] roles)
        {
            _roles = roles;
        }

        public override bool AuthorizeHubConnection(HubDescriptor hubDescriptor, IRequest request)
        {
            return base.AuthorizeHubConnection(hubDescriptor, request);
        }

        public override bool AuthorizeHubMethodInvocation(IHubIncomingInvokerContext hubIncomingInvokerContext, bool appliesToMethod)
        {
            return base.AuthorizeHubMethodInvocation(hubIncomingInvokerContext, appliesToMethod);
        }


    }

  public enum Role
    {
        Admin,
        User
    }
       Usage:
        [IdentitySignalRAuthorize(Role.User)]        
        public async Task InitConnectionAsync(...)
0

There are 0 best solutions below