最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - SignalR - Send message to user using UserID Provider - Stack Overflow

programmeradmin0浏览0评论

Using SignalR, I believe I should be able to send messages to specific connected users by using UserID Provider

Does anyone have an example of how this would be implemented? I've searched and searched and can not find any examples. I would need to target a javascript client.

The use case is, users to my site will have an account. They may be logged in from multiple devices / browsers. When some event happens, I will want to send them a message.

Using SignalR, I believe I should be able to send messages to specific connected users by using UserID Provider

Does anyone have an example of how this would be implemented? I've searched and searched and can not find any examples. I would need to target a javascript client.

The use case is, users to my site will have an account. They may be logged in from multiple devices / browsers. When some event happens, I will want to send them a message.

Share Improve this question asked Jan 11, 2014 at 22:04 hatcylhatcyl 2,3522 gold badges22 silver badges24 bronze badges 6
  • So you're keeping track of Context.User.Identity and Context.ConnectionId somewhere? How is your use case different from the example in the link you provided? – mrtig Commented Jan 11, 2014 at 23:11
  • No, I don't have much. Just a simple Hub and javascript connecting to it. – hatcyl Commented Jan 11, 2014 at 23:53
  • Then I'd suggest taking a closer look at the example that you linked. They use a collection to maintain user names and connections, which allows them to invoke client methods on specific connections. – mrtig Commented Jan 11, 2014 at 23:56
  • 2 Uhm ... maybe I misunderstood their documentation. Before you could manually sync user ids to connection ids and broadcast appropriately.I thought the point of the UserID Provider was to eliminate this manual tracking. I though it would somehow know who the user was based on the the same User that the normal Authentication uses. – hatcyl Commented Jan 12, 2014 at 0:06
  • 2 The point is exactly that, if you have the user name, you don't have to track anything. By default it'll use the user name (if you're using some form of auth that sets the principal) – davidfowl Commented Jan 12, 2014 at 2:55
 |  Show 1 more ment

2 Answers 2

Reset to default 2

I have not looked into SignalR 2.0 but I think this is an extension of what the previous versions of SignalR used to have. When you connect to the hub you can decorate it with an Authorize attribute

[HubName("myhub")]
[Authorize]
public class MyHub1 : Hub
{
    public override System.Threading.Tasks.Task OnConnected()
    {
        var identity = Thread.CurrentPrincipal.Identity;
        var request = Context.Request;
        Clients.Client(Context.ConnectionId).sayhello("Hello " + identity.Name);
        return base.OnConnected();
    }
}

As you can see you are able to access the Identity of the user accessing the Hub. I believe the new capability would be nothing more than an extension of this. Since the connection is always kept alive between the client and the hub you will always have the principal identity which will give you the UserId.

I believe this can help you: (linked from here)

A specific user, identified by userId.

Clients.User(userid).addContosoChatMessageToPage(name, message);

The userId can be determined using the IUserId interface:

public interface IUserIdProvider
{
    string GetUserId(IRequest request);
}

The default implementation of IUserIdProvider is PrincipalUserIdProvider. To use this default implementation, first register it in GlobalHost when the application starts up:

var idProvider = new PrincipalUserIdProvider();
GlobalHost.DependencyResolver.Register (typeof(IUserIdProvider), () => idProvider);

The user name can then be determined by passing in the Request object from the client.

发布评论

评论列表(0)

  1. 暂无评论