简体   繁体   中英

Issue retrieving UserID inside SignalR hub

I had my SignalR private chat system working when it was forcing client updates for every client connected but I wanted to streamline this so only update specific connected users when they specifically receive new messages. Using the posted "Mapping SignalR users to connections" on msdn I set everything up and it logs and changes connection IDs and users but as soon as I want to retrieve their assoicated UserID for my Sql query I get Null?

As stated above, the query worked fine when I was using HTTPContext.User.Identity.GetUserId() to retrieve the users GUID but now I have updated the code I can no longer get the currently connected UsersID no matter what I do. Even though the associated database functions have NO issue retrieving the current users UserName...

public override Task OnConnected()
{
var name = Context.User.Identity.Name;
who = Context.User.Identity.Name;
UserID = Context.User.Identity.GetUserId();
.....
}

string query = "SELECT msgTo, msgFrom, msgContent, date FROM [dbo].[messages] WHERE msgTo = @id";
connection.Open();
using (SqlCommand command = new SqlCommand(query, connection))
{
command.Parameters.AddWithValue("@id", UserID);
try
{
  ...
}

The parameterized query '(@id nvarchar(4000))SELECT msgTo, msgFrom, msgContent, date FRO' expects the parameter '@id', which was not supplied.

我设法通过发送每个请求的查询字符串来修复它($ .connection.hub.qs = {userId:document.getElementById(“ usrid”)。value};),然后在集线器更新功能中检索它

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM