简体   繁体   English

在SignalR集线器内部检索UserID时出现问题

[英]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. 我在强制每个连接的客户端进行客户端更新时运行了SignalR私人聊天系统,但我想简化此流程,因此仅在特定的连接用户专门接收新消息时才对其进行更新。 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? 使用msdn上发布的“将SignalR用户映射为连接”,我进行了所有设置,它记录并更改了连接ID和用户,但是只要我想为我的Sql查询检索其关联的UserID,我就会得到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. 如上所述,当我使用HTTPContext.User.Identity.GetUserId()检索用户GUID时,查询工作正常,但是现在我更新了代码,无论我做什么都无法再获取当前连接的UsersID。 Even though the associated database functions have NO issue retrieving the current users UserName... 即使关联的数据库功能在检索当前用户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. 参数化查询'(@id nvarchar(4000))SELECT msgTo,msgFrom,msgContent,日期FRO'需要未提供的参数'@id'。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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