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 <brecon@microsoft.com>

* Update aspnetcore/signalr/authn-and-authz.md

Co-Authored-By: Scott Addie <10702007+scottaddie@users.noreply.github.com>

* update ms.date metadata value
pull/12361/head
Henrik Walker Moe 2019-05-09 23:30:36 +02:00 committed by Scott Addie
parent cda77c4c6f
commit 73a20435a2
1 changed files with 27 additions and 1 deletions

View File

@ -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<ChatHub>("/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.