diff --git a/aspnetcore/blazor/fundamentals/signalr.md b/aspnetcore/blazor/fundamentals/signalr.md index 04d41fd265a0..40fcfde1b905 100644 --- a/aspnetcore/blazor/fundamentals/signalr.md +++ b/aspnetcore/blazor/fundamentals/signalr.md @@ -1511,6 +1511,58 @@ app.MapBlazorHub(); [!INCLUDE[](~/blazor/security/includes/httpcontext.md)] +## Impersonation for Windows Authentication + +Authenticated hub connections () are created with to indicate the use of default credentials for HTTP requests. For more information, see . + +When the app is running in IIS Express as the signed-in user under Windows Authentication, which is likely the user's personal or work account, the default credentials are those of the signed-in user. + +When the app is published to IIS, the app runs under the *Application Pool Identity*. The connects as the IIS "user" account hosting the app, not the user accessing the page. + +Implement *impersonation* with the to use the identity of the browsing user. + +In the following example: + +* The user from the authentication state provider is cast to a . +* The identity's access token is passed to with the code that builds and starts the . + +```csharp +protected override async Task OnInitializedAsync() +{ + var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync(); + + if (authState?.User.Identity is not null) + { + var user = authState.User.Identity as WindowsIdentity; + + if (user is not null) + { + await WindowsIdentity.RunImpersonatedAsync(user.AccessToken, + async () => + { + hubConnection = new HubConnectionBuilder() + .WithUrl(NavManager.ToAbsoluteUri("/hub"), config => + { + config.UseDefaultCredentials = true; + }) + .WithAutomaticReconnect() + .Build(); + + hubConnection.On("name", userName => + { + name = userName; + InvokeAsync(StateHasChanged); + }); + + await hubConnection.StartAsync(); + }); + } + } +} +``` + +In the preceding code, `NavManager` is a , and `AuthenticationStateProvider` is an service instance ([`AuthenticationStateProvider` documentation](xref:blazor/security/authentication-state)). + ## Additional server-side resources * [Server-side host and deployment guidance: SignalR configuration](xref:blazor/host-and-deploy/server#signalr-configuration)