-
Notifications
You must be signed in to change notification settings - Fork 446
fix issue with incorrect user detection when Invoking for User #747
Changes from 10 commits
07c4e79
3408e09
4572645
f534b4e
0da1a24
fe6c28a
3270d9e
9445232
b93e024
aee0337
1548fe8
8913166
cbeb3c1
012766f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ public class DefaultHubLifetimeManager<THub> : HubLifetimeManager<THub> | |
{ | ||
private long _nextInvocationId = 0; | ||
private readonly HubConnectionList _connections = new HubConnectionList(); | ||
|
||
public override Task AddGroupAsync(string connectionId, string groupName) | ||
{ | ||
if (connectionId == null) | ||
|
@@ -138,17 +138,14 @@ public override Task InvokeGroupAsync(string groupName, string methodName, objec | |
|
||
public override Task InvokeUserAsync(string userId, string methodName, object[] args) | ||
{ | ||
return InvokeAllWhere(methodName, args, connection => | ||
{ | ||
return string.Equals(connection.User.Identity.Name, userId, StringComparison.Ordinal); | ||
}); | ||
return InvokeAllWhere(methodName, args, connection => | ||
string.Equals(connection.UserIdentifier, userId, StringComparison.Ordinal)); | ||
} | ||
|
||
public override Task OnConnectedAsync(HubConnectionContext connection) | ||
{ | ||
// Set the hub groups feature | ||
connection.Features.Set<IHubGroupsFeature>(new HubGroupsFeature()); | ||
|
||
_connections.Add(connection); | ||
return Task.CompletedTask; | ||
} | ||
|
@@ -166,7 +163,7 @@ private async Task WriteAsync(HubConnectionContext connection, HubMessage hubMes | |
if (connection.Output.TryWrite(hubMessage)) | ||
{ | ||
break; | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: remove space |
||
} | ||
} | ||
|
||
|
@@ -194,4 +191,4 @@ private class HubGroupsFeature : IHubGroupsFeature | |
public HashSet<string> Groups { get; } = new HashSet<string>(StringComparer.OrdinalIgnoreCase); | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Security.Claims; | ||
|
||
namespace Microsoft.AspNetCore.SignalR.Core | ||
{ | ||
public class DefaultUserIdProvider : IUserIdProvider | ||
{ | ||
public string GetUserId(HubConnectionContext connection) | ||
{ | ||
return connection.User.FindFirst(ClaimTypes.NameIdentifier)?.Value; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
using System.Threading.Tasks; | ||
using System.Threading.Tasks.Channels; | ||
using Microsoft.AspNetCore.Http.Features; | ||
using Microsoft.AspNetCore.SignalR.Core; | ||
using Microsoft.AspNetCore.SignalR.Features; | ||
using Microsoft.AspNetCore.SignalR.Internal; | ||
using Microsoft.AspNetCore.SignalR.Internal.Protocol; | ||
|
@@ -25,12 +26,15 @@ public class HubConnectionContext | |
private readonly ConnectionContext _connectionContext; | ||
private readonly CancellationTokenSource _connectionAbortedTokenSource = new CancellationTokenSource(); | ||
private readonly TaskCompletionSource<object> _abortCompletedTcs = new TaskCompletionSource<object>(); | ||
private readonly IUserIdProvider _userIdProvider; | ||
private string _userIdCache = null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Super nit: Just call this |
||
|
||
public HubConnectionContext(WritableChannel<HubMessage> output, ConnectionContext connectionContext) | ||
public HubConnectionContext(WritableChannel<HubMessage> output, ConnectionContext connectionContext, IUserIdProvider userIdProvider) | ||
{ | ||
_output = output; | ||
_connectionContext = connectionContext; | ||
ConnectionAbortedToken = _connectionAbortedTokenSource.Token; | ||
_userIdProvider = userIdProvider; | ||
} | ||
|
||
private IHubFeature HubFeature => Features.Get<IHubFeature>(); | ||
|
@@ -67,6 +71,18 @@ public virtual void Abort() | |
Task.Factory.StartNew(_abortedCallback, this); | ||
} | ||
|
||
public string UserIdentifier | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not comfortable with the lazy evaluation of this. I think it should be set by the HubEndPoint after negotiate. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, I don't really like the idea of having public setter for that sort of things. Since I think that for end-user connection context should be something "immutable". Don't you think so? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree with that too, make it an internal setter. We'll need to see what mocking this object looks like though. |
||
{ | ||
get | ||
{ | ||
if (String.IsNullOrEmpty(_userIdCache)) | ||
{ | ||
_userIdCache = _userIdProvider.GetUserId(this); | ||
} | ||
return _userIdCache; | ||
} | ||
} | ||
|
||
internal void Abort(Exception exception) | ||
{ | ||
AbortException = ExceptionDispatchInfo.Capture(exception); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
namespace Microsoft.AspNetCore.SignalR.Core | ||
{ | ||
public interface IUserIdProvider | ||
{ | ||
string GetUserId(HubConnectionContext connection); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -245,9 +245,9 @@ public override Task OnConnectedAsync(HubConnectionContext connection) | |
previousConnectionTask = WriteAsync(connection, message); | ||
}); | ||
|
||
if (connection.User.Identity.IsAuthenticated) | ||
if (!String.IsNullOrEmpty(connection.UserIdentifier)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: lowercase s in string |
||
{ | ||
var userChannel = _channelNamePrefix + ".user." + connection.User.Identity.Name; | ||
var userChannel = _channelNamePrefix + ".user." + connection.UserIdentifier; | ||
redisSubscriptions.Add(userChannel); | ||
|
||
var previousUserTask = Task.CompletedTask; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: remove empty space