@@ -1511,6 +1511,58 @@ app.MapBlazorHub();
1511
1511
1512
1512
[! INCLUDE [](~ / blazor /security /includes /httpcontext .md )]
1513
1513
1514
+ ## Impersonation for Windows Authentication
1515
+
1516
+ Authenticated hub connections (<xref :Microsoft .AspNetCore .SignalR .Client .HubConnection >) are created with <xref :Microsoft .AspNetCore .Http .Connections .Client .HttpConnectionOptions .UseDefaultCredentials %2A > to indicate the use of default credentials for HTTP requests . For more information , see <xref :signalr /authn -and -authz #windows -authentication >.
1517
+
1518
+ 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 .
1519
+
1520
+ When the app is published to IIS , the app runs under the *Application Pool Identity *. The <xref :Microsoft .AspNetCore .SignalR .Client .HubConnection > connects as the IIS "user " account hosting the app , not the user accessing the page .
1521
+
1522
+ Implement *impersonation * with the <xref :Microsoft .AspNetCore .SignalR .Client .HubConnection > to use the identity of the browsing user .
1523
+
1524
+ In the following example :
1525
+
1526
+ * The user from the authentication state provider is cast to a <xref :System .Security .Principal .WindowsIdentity >.
1527
+ * The identity 's access token is passed to <xref :System .Security .Principal .WindowsIdentity .RunImpersonatedAsync %2A ?displayProperty =nameWithType > with the code that builds and starts the <xref :Microsoft .AspNetCore .SignalR .Client .HubConnection >.
1528
+
1529
+ ```csharp
1530
+ protected override async Task OnInitializedAsync ()
1531
+ {
1532
+ var authState = await AuthenticationStateProvider .GetAuthenticationStateAsync ();
1533
+
1534
+ if (authState ?.User .Identity is not null )
1535
+ {
1536
+ var user = authState .User .Identity as WindowsIdentity ;
1537
+
1538
+ if (user is not null )
1539
+ {
1540
+ await WindowsIdentity .RunImpersonatedAsync (user .AccessToken ,
1541
+ async () =>
1542
+ {
1543
+ hubConnection = new HubConnectionBuilder ()
1544
+ .WithUrl (NavManager .ToAbsoluteUri ("/hub "), config =>
1545
+ {
1546
+ config .UseDefaultCredentials = true ;
1547
+ })
1548
+ .WithAutomaticReconnect ()
1549
+ .Build ();
1550
+
1551
+ hubConnection .On <string >(" name" , userName =>
1552
+ {
1553
+ name = userName ;
1554
+ InvokeAsync (StateHasChanged );
1555
+ });
1556
+
1557
+ await hubConnection .StartAsync ();
1558
+ });
1559
+ }
1560
+ }
1561
+ }
1562
+ ```
1563
+
1564
+ In the preceding code , `NavManager ` is a <xref :Microsoft .AspNetCore .Components .NavigationManager >, and `AuthenticationStateProvider ` is an <xref :Microsoft .AspNetCore .Components .Authorization .AuthenticationStateProvider > service instance ([`AuthenticationStateProvider ` documentation ](xref :blazor /security /authentication -state )).
1565
+
1514
1566
## Additional server-side resources
1515
1567
1516
1568
* [Server -side host and deployment guidance : SignalR configuration ](xref :blazor /host -and - deploy /server #signalr -configuration )
0 commit comments