Skip to content

Commit 95b2159

Browse files
committed
Updates
1 parent e826f34 commit 95b2159

File tree

1 file changed

+30
-44
lines changed

1 file changed

+30
-44
lines changed

aspnetcore/blazor/fundamentals/signalr.md

Lines changed: 30 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1513,32 +1513,9 @@ app.MapBlazorHub();
15131513

15141514
## Impersonation for Windows Authentication
15151515

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:
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>.
15171517

1518-
```csharp
1519-
protected override async Task OnInitializedAsync()
1520-
{
1521-
hubConnection = new HubConnectionBuilder()
1522-
.WithUrl(NavigationManager.ToAbsoluteUri("/hub"), config =>
1523-
{
1524-
config.UseDefaultCredentials = true;
1525-
})
1526-
.WithAutomaticReconnect()
1527-
.Build();
1528-
1529-
hubConnection.On<string>("name", userName =>
1530-
{
1531-
name = userName;
1532-
InvokeAsync(StateHasChanged);
1533-
});
1534-
1535-
await hubConnection.StartAsync();
1536-
}
1537-
```
1538-
1539-
For more information, see <xref:signalr/authn-and-authz#windows-authentication>.
1540-
1541-
The preceding code is sufficient 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.
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.
15421519

15431520
When the app is published to IIS, the app runs under the *Application Pool Identity*. The hub connects as the IIS "user" account hosting the app, not the user accessing the page.
15441521

@@ -1552,31 +1529,40 @@ In the following example:
15521529
```csharp
15531530
protected override async Task OnInitializedAsync()
15541531
{
1555-
var user = (WindowsIdentity)
1556-
(await AuthenticationStateProvider.GetAuthenticationStateAsync())
1557-
.User.Identity;
1532+
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
15581533

1559-
await WindowsIdentity.RunImpersonatedAsync(user.AccessToken, async () =>
1534+
if (authState?.User.Identity is not null)
15601535
{
1561-
hubConnection = new HubConnectionBuilder()
1562-
.WithUrl(NavigationManager.ToAbsoluteUri("/hub"), config =>
1563-
{
1564-
config.UseDefaultCredentials = true;
1565-
})
1566-
.WithAutomaticReconnect()
1567-
.Build();
1568-
1569-
hubConnection.On<string>("name", userName =>
1570-
{
1571-
name = userName;
1572-
InvokeAsync(StateHasChanged);
1573-
});
1536+
var user = authState.User.Identity as WindowsIdentity;
15741537

1575-
await hubConnection.StartAsync();
1576-
});
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+
}
15771561
}
15781562
```
15791563

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+
15801566
## Additional server-side resources
15811567

15821568
* [Server-side host and deployment guidance: SignalR configuration](xref:blazor/host-and-deploy/server#signalr-configuration)

0 commit comments

Comments
 (0)