Skip to content

Commit e76a9db

Browse files
authored
Impersonation for Windows Authentication (#34621)
1 parent 8f707ec commit e76a9db

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

aspnetcore/blazor/fundamentals/signalr.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,6 +1511,58 @@ app.MapBlazorHub();
15111511

15121512
[!INCLUDE[](~/blazor/security/includes/httpcontext.md)]
15131513

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+
15141566
## Additional server-side resources
15151567

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

0 commit comments

Comments
 (0)