I have a uwp client application which needs to do following 3 scenarios for chat feature.
- chat with everyone connected to the signal R hub ( public )
- chat with MyGroup only : each user will be part of a group.
- private chat ( 1v1 chat with other users )
I have following hub code where I somehow manage to do public and group feature.
public class ChatHub : Hub
{
public Task SendPrivateTweet(string user, string message)
{
return Clients.User(user).SendAsync("ReceiveTweet", user, message);
}
public async Task SendTweet(string user, string message)
{
await Clients.All.SendAsync("ReceiveTweet", user, message);
}
public async Task SendTweetGroup(string groupName, string user, string message)
{
await Clients.Group(groupName).SendAsync("ReceiveTweet", user, message);
}
public async Task AddToGroup(string user, string groupName)
{
await Groups.AddToGroupAsync(Context.ConnectionId, groupName);
await Clients.Group(groupName).SendAsync("EnteredOrLeft", $"{user} has joined the group {groupName}.");
}
public async Task RemoveFromGroup(string user, string groupName)
{
await Groups.RemoveFromGroupAsync(Context.ConnectionId, groupName);
await Clients.Group(groupName).SendAsync("EnteredOrLeft", $"{user} has left the group {groupName}.");
}
}
I have a db with users info in it and each user has a group name linked to it based on that I add each connection to their respective group.
await hubConnection.InvokeAsync("AddToGroup", user.Name, user.GroupName);
I have to do this because I do not have users logged into signalR so I cannot match their userIds with the one in my db, and that is exactly why I cannot send private message to a specific user because for that the context must have user info which it doesn't at the moment, according to docs there is some complex token bearer stuff going on and I don't know the api to popup Microsoft login for the user ( which would be ideal ).
Is there any other way how I can retain the user ids in the hub so that I can easily send a private message from client by sending the receiver user id?
I mean, can I assign a user Id from each connection at the time of connecting to the hub? which can be later on used for querying within the hub?
I can see how to do identity in a web app but can't find tutorial for doing the same in a native app like uwp: https://docs.microsoft.com/en-us/aspnet/core/security/authentication/identity?view=aspnetcore-2.2&tabs=visual-studio
Context.User.Identity.Name
you want? I saw this from here: docs.microsoft.com/en-us/aspnet/signalr/overview/security/… – Bite