question

6666666 avatar image
0 Votes"
6666666 asked ZhiLv-MSFT edited

How to extend the class to add a property UserId?

I am using Signalr and there is a class:

 public class Device:Hub<T>
 {
    
 }

I want to add a property UserId in the class. so I want to ask how to add a property to the class Hub so I can use it?

dotnet-aspnet-core-webapidotnet-aspnet-signalr
· 6
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @6666666,

Can you explain the uses of UserId? Do you mean you want to add it in the Device class?

If you mean the login user's UserId, you can access it via Context. And, if you want to share it in the Device's method, you could define a static variable or use it as a parameter.

0 Votes 0 ·

I want to use UserId in the method.

for example:

 public async Task SendMessage(string message)
 {
     var user = await _context.users.FindAsync(`UserId`);
     await Clients.Caller.SendMessage(user.UserName,message);
 }
0 Votes 0 ·

Hi @6666666,

Whether the UserId is the current user's id? and you want to get current user information and send to the Caller? If it is not the current user's id, it is better to send the user id as a parameter.

0 Votes 0 ·
Show more comments

1 Answer

ZhiLv-MSFT avatar image
0 Votes"
ZhiLv-MSFT answered ZhiLv-MSFT edited

Hi @6666666,

You can get the user id via the following code:

var userId = Context.UserIdentifier;

Or find the user via DbContext and the username, code like this:

     var user = _dbContext.Users.Where(c => c.UserName == username).FirstOrDefault();
     if(user != null)
     {
         var id = user.Id;
     }

The detail sample code as below: (In my sample, the user id is a Guid value, and it is auto generated when insert new user)

 [Authorize]
 public class MyChatHub : Hub
 {
     private static Dictionary<string, List<string>> NtoIdMappingTable = new Dictionary<string, List<string>>();
     private readonly ApplicationDbContext _dbContext;
     public MyChatHub(ApplicationDbContext applicationDbContext)
     {
         _dbContext = applicationDbContext;
     }
     public override async Task OnConnectedAsync()
     {
         var username = Context.User.Identity.Name;
         var userId = Context.UserIdentifier;

         var user = _dbContext.Users.Where(c => c.UserName == username).FirstOrDefault();
         if(user != null)
         {
             var id = user.Id;
         }

         List<string> userIds;

         //store the userid to the list.
         if (!NtoIdMappingTable.TryGetValue(username, out userIds))
         {
             userIds = new List<string>();
             userIds.Add(userId);

             NtoIdMappingTable.Add(username, userIds);
         }
         else
         {
             userIds.Add(userId);
         }

         await base.OnConnectedAsync(); 
     }

The debug screenshot:

134518-2.gif


If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Best regards,
Dillion






2.gif (249.2 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.