SignalR 授权用户未收到消息 ASP.Core 3.1
SingalR authorized user doesn't get message back ASP.Core 3.1
我正在尝试将身份用户与 SingalR 集成。至此,只要连接到hub,授权就成功了。
问题是我可以让它与群组一起工作,但尽管授权有效,但我无法直接由用户发送。我也试过电子邮件而不是用户唯一标识符,但结果是一样的。会喜欢你的意见。
谢谢
智威汤逊:
options.Events = new JwtBearerEvents
{
OnMessageReceived = context =>
{
var accessToken = context.Request.Query["access_token"];
if (!string.IsNullOrEmpty(accessToken) &&(context.Request.Path.StartsWithSegments("/feed/get")))
{
context.Token = context.Request.Query["access_token"];
}
return Task.CompletedTask;
}
};
Startup.cs
app.UseEndpoints(route =>
{
route.MapHub<ActivityHub>("/feed/Get");
});
中心
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
public class ActivityHub : Hub
{
IAuthService AuthService;
public ActivityHub(IAuthService authService)
{
AuthService = authService;
}
public override async Task OnConnectedAsync()
{
var user = await AuthService.GetUserByNameAsync(Context.User.Identity.Name);
await Clients.User(user.Id).SendAsync("receiveMessage", "Hello user"); //this doesn't get recived on client
await Groups.AddToGroupAsync(Context.ConnectionId, user.Id);
await Clients.Group(user.Id).SendAsync("receiveMessage", "Hello group!");//this works
await base.OnConnectedAsync();
}
public override async Task OnDisconnectedAsync(Exception exception)
{
await base.OnDisconnectedAsync(exception);
}
}
}
如果您想自定义用户标识符,您可以实现自己的 IUserIdProvider,如文档中所示 https://docs.microsoft.com/aspnet/core/signalr/authn-and-authz?view=aspnetcore-3.1#use-claims-to-customize-identity-handling
此外,在使用 Clients.User(...) 时,您应该使用 Context.UserIdentifier 来获取用户 ID,因为 SignalR 将使用它来区分用户。
正如我在评论部分所说,最好的方法就是使用群组来存档您的目标。只需在连接方法的单个用户组中添加一个用户。
如您所见documentation,在 SignalR 中使用单个用户组很常见。
我正在尝试将身份用户与 SingalR 集成。至此,只要连接到hub,授权就成功了。 问题是我可以让它与群组一起工作,但尽管授权有效,但我无法直接由用户发送。我也试过电子邮件而不是用户唯一标识符,但结果是一样的。会喜欢你的意见。 谢谢
智威汤逊:
options.Events = new JwtBearerEvents
{
OnMessageReceived = context =>
{
var accessToken = context.Request.Query["access_token"];
if (!string.IsNullOrEmpty(accessToken) &&(context.Request.Path.StartsWithSegments("/feed/get")))
{
context.Token = context.Request.Query["access_token"];
}
return Task.CompletedTask;
}
};
Startup.cs
app.UseEndpoints(route =>
{
route.MapHub<ActivityHub>("/feed/Get");
});
中心
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
public class ActivityHub : Hub
{
IAuthService AuthService;
public ActivityHub(IAuthService authService)
{
AuthService = authService;
}
public override async Task OnConnectedAsync()
{
var user = await AuthService.GetUserByNameAsync(Context.User.Identity.Name);
await Clients.User(user.Id).SendAsync("receiveMessage", "Hello user"); //this doesn't get recived on client
await Groups.AddToGroupAsync(Context.ConnectionId, user.Id);
await Clients.Group(user.Id).SendAsync("receiveMessage", "Hello group!");//this works
await base.OnConnectedAsync();
}
public override async Task OnDisconnectedAsync(Exception exception)
{
await base.OnDisconnectedAsync(exception);
}
}
}
如果您想自定义用户标识符,您可以实现自己的 IUserIdProvider,如文档中所示 https://docs.microsoft.com/aspnet/core/signalr/authn-and-authz?view=aspnetcore-3.1#use-claims-to-customize-identity-handling
此外,在使用 Clients.User(...) 时,您应该使用 Context.UserIdentifier 来获取用户 ID,因为 SignalR 将使用它来区分用户。
正如我在评论部分所说,最好的方法就是使用群组来存档您的目标。只需在连接方法的单个用户组中添加一个用户。
如您所见documentation,在 SignalR 中使用单个用户组很常见。