-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix
HubConnectionContext.UserIdentifier
is null when negotiation wi…
…th Management SDK When clients negotiatie with Management SDK and connect to SignalR server, IUserIdProvider might not work as the user Id is set directly in the Management SDK. To make HubConnectionContext.UserIdentifier have the valid value in this case, we should set it before the server can access it. HubLifetimeManager{THub}.OnConnectedAsync(HubConnectionContext) is the only chance we can set the value. However, we cannot access the Constants.ClaimType.UserId as ASRS system claims're trimmed there. HubConnectionContext.Features is the place where we can store the user Id. The following code is our injection point. https://github.com/dotnet/aspnetcore/blob/v6.0.9/src/SignalR/server/Core/src/HubConnectionHandler.cs#L132-L141
- Loading branch information
Showing
6 changed files
with
100 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/Microsoft.Azure.SignalR/Internals/IServiceUserNameFeature.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using Microsoft.AspNetCore.SignalR; | ||
|
||
namespace Microsoft.Azure.SignalR | ||
{ | ||
/// <summary> | ||
/// When clients negotiate with Management SDK and connect to SignalR server, the <see cref="IUserIdProvider"/> might not work as the user Id is set directly in the Management SDK. | ||
/// To make <see cref="HubConnectionContext.UserIdentifier"/> have the valid value in this case, we should set it before the server can access it. <see cref="HubLifetimeManager{THub}.OnConnectedAsync(HubConnectionContext)"/> is the only chance we can set the value. However, we cannot access the <see cref="Constants.ClaimType.UserId"/> as ASRS system claims're trimmed there. <see cref="HubConnectionContext.Features"/> is the place where we can store the user Id. | ||
/// https://github.com/dotnet/aspnetcore/blob/v6.0.9/src/SignalR/server/Core/src/HubConnectionHandler.cs#L132-L141 | ||
/// </summary> | ||
internal interface IServiceUserIdFeature | ||
{ | ||
string UserId { get; } | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/Microsoft.Azure.SignalR/Internals/ServiceUserIdFeature.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace Microsoft.Azure.SignalR | ||
{ | ||
internal class ServiceUserIdFeature : IServiceUserIdFeature | ||
{ | ||
public string UserId { get; } | ||
|
||
public ServiceUserIdFeature(string userId) | ||
{ | ||
UserId = userId; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
test/Microsoft.Azure.SignalR.Tests/ClientConnectionContextFacts.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Security.Claims; | ||
using Xunit; | ||
|
||
namespace Microsoft.Azure.SignalR.Tests | ||
{ | ||
public class ClientConnectionContextFacts | ||
{ | ||
[Fact] | ||
public void SetUserIdFeatureTest() | ||
{ | ||
var claims = new Claim[] { new(Constants.ClaimType.UserId, "testUser") }; | ||
var connection = new ClientConnectionContext(new("connectionId", claims)); | ||
var feature = connection.Features.Get<IServiceUserIdFeature>(); | ||
Assert.NotNull(feature); | ||
Assert.Equal("testUser", feature.UserId); | ||
} | ||
|
||
[Fact] | ||
public void DoNotSetUserIdFeatureWithoutUserIdClaimTest() | ||
{ | ||
var connection = new ClientConnectionContext(new("connectionId", Array.Empty<Claim>())); | ||
var feature = connection.Features.Get<IServiceUserIdFeature>(); | ||
Assert.Null(feature); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters