Skip to content

Commit ca6365c

Browse files
committed
Use internal class instead of interface
1 parent 35943fc commit ca6365c

File tree

6 files changed

+15
-24
lines changed

6 files changed

+15
-24
lines changed

src/Microsoft.Azure.SignalR/HubHost/ServiceLifetimeManager.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ public ServiceLifetimeManager(
5353

5454
public override Task OnConnectedAsync(HubConnectionContext connection)
5555
{
56-
var userIdFeature = connection.Features.Get<IServiceUserIdFeature>();
56+
var userIdFeature = connection.Features.Get<ServiceUserIdFeature>();
5757
if (userIdFeature != null)
5858
{
5959
connection.UserIdentifier = userIdFeature.UserId;
60-
connection.Features.Set<IServiceUserIdFeature>(null);
60+
connection.Features.Set<ServiceUserIdFeature>(null);
6161
}
6262
return base.OnConnectedAsync(connection);
6363
}

src/Microsoft.Azure.SignalR/Internals/IServiceUserNameFeature.cs

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/Microsoft.Azure.SignalR/Internals/ServiceUserIdFeature.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
// Copyright (c) Microsoft. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4+
using Microsoft.AspNetCore.SignalR;
5+
46
namespace Microsoft.Azure.SignalR
57
{
6-
internal class ServiceUserIdFeature : IServiceUserIdFeature
8+
/// <summary>
9+
/// 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.
10+
/// 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.
11+
/// https://github.com/dotnet/aspnetcore/blob/v6.0.9/src/SignalR/server/Core/src/HubConnectionHandler.cs#L132-L141
12+
/// </summary>
13+
internal class ServiceUserIdFeature
714
{
815
public string UserId { get; }
916

src/Microsoft.Azure.SignalR/ServerConnections/ClientConnectionContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ private FeatureCollection BuildFeatures(OpenConnectionMessage serviceMessage)
251251
var userIdClaim = serviceMessage.Claims.FirstOrDefault(c => c.Type == Constants.ClaimType.UserId);
252252
if (userIdClaim != default)
253253
{
254-
features.Set<IServiceUserIdFeature>(new ServiceUserIdFeature(userIdClaim.Value));
254+
features.Set(new ServiceUserIdFeature(userIdClaim.Value));
255255
}
256256
return features;
257257
}

test/Microsoft.Azure.SignalR.Tests/ClientConnectionContextFacts.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public void SetUserIdFeatureTest()
1414
{
1515
var claims = new Claim[] { new(Constants.ClaimType.UserId, "testUser") };
1616
var connection = new ClientConnectionContext(new("connectionId", claims));
17-
var feature = connection.Features.Get<IServiceUserIdFeature>();
17+
var feature = connection.Features.Get<ServiceUserIdFeature>();
1818
Assert.NotNull(feature);
1919
Assert.Equal("testUser", feature.UserId);
2020
}
@@ -23,7 +23,7 @@ public void SetUserIdFeatureTest()
2323
public void DoNotSetUserIdFeatureWithoutUserIdClaimTest()
2424
{
2525
var connection = new ClientConnectionContext(new("connectionId", Array.Empty<Claim>()));
26-
var feature = connection.Features.Get<IServiceUserIdFeature>();
26+
var feature = connection.Features.Get<ServiceUserIdFeature>();
2727
Assert.Null(feature);
2828
}
2929
}

test/Microsoft.Azure.SignalR.Tests/ServiceLifetimeManagerFacts.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ public async void TestSendConnectionAsyncisOverwrittenWhenClientConnectionExiste
229229
public async void SetUserIdTest()
230230
{
231231
var connectionContext = new TestConnectionContext();
232-
connectionContext.Features.Set<IServiceUserIdFeature>(new ServiceUserIdFeature("testUser"));
232+
connectionContext.Features.Set(new ServiceUserIdFeature("testUser"));
233233

234234
var hubConnectionContext = new HubConnectionContext(connectionContext, new(), NullLoggerFactory.Instance);
235235
var serviceLifetimeManager = MockLifetimeManager(new TestServiceConnectionManager<TestHub>());
@@ -248,6 +248,7 @@ public async void DoNotSetUserIdWithoutFeatureTest()
248248
await serviceLifetimeManager.OnConnectedAsync(hubConnectionContext);
249249

250250
Assert.Null(hubConnectionContext.UserIdentifier);
251+
Assert.Null(hubConnectionContext.Features.Get<ServiceUserIdFeature>());
251252
}
252253

253254
private HubLifetimeManager<TestHub> MockLifetimeManager(IServiceConnectionManager<TestHub> serviceConnectionManager, IClientConnectionManager clientConnectionManager = null, IBlazorDetector blazorDetector = null)

0 commit comments

Comments
 (0)