Pergunta

Examples I've looked in a few places (including Microsoft SignalR official website), but do not understand how this works.

I implemented this in my class to get a different id: (I'm not using IPrincipal.Identity.Name)

public class CustomUserIdProvider : IUserIdProvider
{
    public string GetUserId(IRequest request)
    {
        //for example
        return Guid.NewGuid().ToString();
    }
}

But I did not understand how or where I can get that new generated id? Someone could show me? Or make me understand how it all works!

Thank you in advance.

**UPDATE**

Now I know how this works. Actually I was confused ... I was all confused. Client.Clients with Clients.User. So not getting the expected result, I was using the ConncetionId Clients.User(Context.ConnectionId) in User, which has nothing to do.

Sorry for anything.

Foi útil?

Solução

There is no way to retrieve the new generated id. Since you have an IRequest object available to you inside of Hub methods and inside of PersistentConnection.OnReceived, the idea is you should be able to derive the caller's UserId from the IRequest in your Hub methods the same way you did in your custom GetUserId method.

Your example implementation would not be useful because you do not return the same id for every request made by the same caller. One option you might have is to identify your user is by extracting their user id from the query string which you can customize. However, you will have to be careful about how you do this if you don't want it to be possible to spoof UserIds.

Once you have a way to extract the same UserId from all IRequests coming from he same caller, you should be able to do something like the following:

public class CustomUserIdProvider : IUserIdProvider
{
    public string GetUserId(IRequest request)
    {
        //for example
        return MyStatic.ExtractUserId(request);
    }
}

public class MyHub : Hub
{
    public string GetMyUserId()
    {
        return MyStatic.ExtractUserId(Context.Request);
    }

    public void SendToUser(string userId, string message)
    {
        Clients.User(userId).send(message);
    }
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top