Let's say I have this Hub in an ASP.NET backend.
public class ChatHub : Hub
{
public async Task SendMessage(string user, string message)
=> await Clients.All.SendAsync("ReceiveMessage", user, message);
}
I know that I can protect this SendMessage method with the attribute Authorize like this.
public class ChatHub : Hub
{
[Authorize]
public async Task SendMessage(string user, string message)
=> await Clients.All.SendAsync("ReceiveMessage", user, message);
}
That way only Authorized users can call this endpoint.
But are still able to connect to ChatHub and listen to "ReceiveMessage".
In an Angular frontend app I am using the signalR.HubConnection from '@microsoft/signalr' to listen to events like this.
this.hubConnection.on('ReceiveMessage', (data: SomeData) => {
this.SomeDataSub.next(data);
});
My question is, How can I ensure that users can only listen to enpoints they are allowed to?
So, in case someone listens to "ReceiveMessage" they will get: "you aren't allowed to listen to this event."