Authentication and authorization in ASP.NET Core SignalR | Microsoft Learn | Auth in Hub methods

PHOTO EMBED

Sun Jul 02 2023 19:45:00 GMT+0000 (Coordinated Universal Time)

Saved by @cameron_v_r

[Authorize("MyAuthorizationPolicy")]
public class ChatPolicyHub : Hub
{
    public override async Task OnConnectedAsync()
    {
        await Clients.All.SendAsync("ReceiveSystemMessage", 
                                    $"{Context.UserIdentifier} joined.");
        await base.OnConnectedAsync();
    }
    // Code removed for brevity.
content_copyCOPY

The constructor arguments and properties of the [Authorize] attribute can be used to restrict access to only users matching specific authorization policies. For example, with the custom authorization policy called MyAuthorizationPolicy, only users matching that policy can access the hub using the following code:

https://learn.microsoft.com/en-us/aspnet/core/signalr/authn-and-authz?view