From 73a20435a24389efb6a103ca3b55b83a8f2d7cfe Mon Sep 17 00:00:00 2001 From: Henrik Walker Moe Date: Thu, 9 May 2019 23:30:36 +0200 Subject: [PATCH] Middleware ordering for signalr and authentication (#12339) * Adds example of configuring SignalR with authentication * Adds note explicitly mentioning ordering of middleware * Update aspnetcore/signalr/authn-and-authz.md Co-Authored-By: BrennanConroy * Update aspnetcore/signalr/authn-and-authz.md Co-Authored-By: Scott Addie <10702007+scottaddie@users.noreply.github.com> * update ms.date metadata value --- aspnetcore/signalr/authn-and-authz.md | 28 ++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/aspnetcore/signalr/authn-and-authz.md b/aspnetcore/signalr/authn-and-authz.md index 5b7b2e4612..3e81462f8b 100644 --- a/aspnetcore/signalr/authn-and-authz.md +++ b/aspnetcore/signalr/authn-and-authz.md @@ -5,7 +5,7 @@ description: Learn how to use authentication and authorization in ASP.NET Core S monikerRange: '>= aspnetcore-2.1' ms.author: bradyg ms.custom: mvc -ms.date: 01/31/2019 +ms.date: 05/09/2019 uid: signalr/authn-and-authz --- @@ -19,6 +19,32 @@ By [Andrew Stanton-Nurse](https://twitter.com/anurse) SignalR can be used with [ASP.NET Core authentication](xref:security/authentication/identity) to associate a user with each connection. In a hub, authentication data can be accessed from the [`HubConnectionContext.User`](/dotnet/api/microsoft.aspnetcore.signalr.hubconnectioncontext.user) property. Authentication allows the hub to call methods on all connections associated with a user (See [Manage users and groups in SignalR](xref:signalr/groups) for more information). Multiple connections may be associated with a single user. +The following is an example of `Startup.Configure` which uses SignalR and ASP.NET Core authentication: + +```csharp +public void Configure(IApplicationBuilder app) +{ + ... + + app.UseStaticFiles(); + + app.UseAuthentication(); + + app.UseSignalR(hubs => + { + hubs.MapHub("/chat"); + }); + + app.UseMvc(routes => + { + routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}"); + }); +} +``` + +> [!NOTE] +> The order in which you register the SignalR and ASP.NET Core authentication middleware matters. Always call `UseAuthentication` before `UseSignalR` so that SignalR has a user on the `HttpContext`. + ### Cookie authentication In a browser-based app, cookie authentication allows your existing user credentials to automatically flow to SignalR connections. When using the browser client, no additional configuration is needed. If the user is logged in to your app, the SignalR connection automatically inherits this authentication.