Streaming and Authentication and Authorization in SignalR
SignalR
8 Articles
In this article, let's learn about Streaming and Authentication and Authentication in SignalR.
Note: If you have not done so already, I recommend you read the article on Exception Handling and Logging in SignalR.
Table of Contents
Introduction
In our previous article we about Exception Handling and Logging and how to configure them. Today in this article, lets learn about how to do streaming and touch base on authentication and authorization in SignalR.
Streaming
Streaming with SignalR is pretty specialized. If we're talking about using WebSockets for connections, the messages flying back and forth don't weigh much in terms of overhead. Since the connection's already set up, the biggest chunk of overhead is likely in serializing and deserializing, which happens pretty quickly. So if you're passing things like real time data, the regular SignalR transport should do the trick just fine.
But streaming isn't the best fit for everything, especially not for binary data like audio and video. SignalR uses a JSON-based transport, even if it's using MessagePack, which means JSON gets turned into binary. So, not the smoothest ride for audio and video.
Code Sample - SignalR Streaming
Authentication & Authorization
The cool thing is that the SignalR hub can make use of everything that's in ASP.NET Core already. We can just put the Authorize attribute above ILoveDotNetHub and now users that don't have a valid credentials can't access it. SignalR supports both JWT and Cookie authentication. Here is the example for JWT authentication.
Code Sample - SignalR Authentication and Authorization
Authorization doesn't work any differently with SignalR than it does with any other ASP.NET Core application. You can use Context.User.Claims and get the claims and work on them.
Summary
In this article, we learnt about Streaming and Authentication and Authorization in SignalR. In the next article, we will learn about Design Considerations and Deployment & Scaling in SignalR.