asp.net mvc 5 - SignalR C# MVC Mapping Anonymous User to Client ID -
question: how manage anonymous users multiple tabs in single browser updated when hub sends out response?
the scenario follows:
i integrate signalr project anonymous users can live chat operators. user's have authenticated iidentity mapped via client.user(username) command. anonymous user browsing site.com/tools , site.com/nottools can not send messages tabs connectionid. 1 tab gathers response.
i have tried using iwc patch tool doesn't account saving chat information database , think passing variables via ajax isn't secure way read/write database.
i have looked in following: managing signalr connections anonymous user
however creating base such , using sessions seems less secure owin.
i had idea use client.user() creating false account each time user connects , delete when disconnect. rather not fill aspnetusers db context garbage accounts seems unnecessary.
is possible use userhandler.connectedids.add(context.connectionid);
map fake username? im @ loss.
would make sense use iuserid provider?
public interface iuseridprovider { string getuserid(irequest request); }
then create database stores ip addresses connectionids , single usernames?
database:
users: fk connection ids
|userid|username|ip |connectionids| | 1 | user1 |127.0.0.1| 1 | | 2 | user2 |127.0.0.2| 2 |
connectionids:
|connectionid|signalrid|connectionids| | 1 | xx.x.x.x| 1 | | 2 | xx.xxx.x| 2 | | 3 | xx.xxxxx| 2 | | 4 | xxxxx.xx| 2 |
then possibly write logic around connection?
public override task onconnected() { if(context.identity.username != null){ //add connection based on logged in user. } else{ ///save new user database? } }
but question still remains how handle multiple tabs when sending command on websocket?
update clear, intent create live chat/support tool allows in browser anonymous access @ times.
the client wants similar http://www.livechatinc.com
i have created javascript plugin sends , receives hub, regardless of domain on. (my client has multisites) missing piece puzzle simplest, managing anonymous users allow multi-tabbed conversations.
a solution adopted make user register kind of id connection , on onconnected.
public override task onconnected() { clients.caller.register(); return base.onconnected(); }
and user returns call kind of own id logic
from clients register method
public void register(guid userid) { s_connectioncache.add(userid, guid.parse(context.connectionid)); }
and keep user ids in static dictionary ( take care of locks since need thread safe;
static readonly iconnectioncache s_connectioncache = new connectionscache();
here
public class connectionscache :iconnectioncache { private readonly dictionary<guid, userconnections> m_userconnections = new dictionary<guid, userconnections>(); private readonly dictionary<guid,guid> m_connectionstousersmapping = new dictionary<guid, guid>(); readonly object m_userlock = new object(); readonly object m_connectionlock = new object(); #region public public userconnections this[guid index] => m_userconnections.containskey(index) ?m_userconnections[index]:new userconnections(); public void add(guid userid, guid connectionid) { lock (m_userlock) { if (m_userconnections.containskey(userid)) { if (!m_userconnections[userid].contains(connectionid)) { m_userconnections[userid].add(connectionid); } } else { m_userconnections.add(userid, new userconnections() {connectionid}); } } lock (m_connectionlock) { if (m_connectionstousersmapping.containskey(connectionid)) { m_connectionstousersmapping[connectionid] = userid; } else { m_connectionstousersmapping.add(connectionid, userid); } } } public void remove(guid connectionid) { lock (m_connectionlock) { if (!m_connectionstousersmapping.containskey(connectionid)) { return; } var userid = m_connectionstousersmapping[connectionid]; m_connectionstousersmapping.remove(connectionid); m_userconnections[userid].remove(connectionid); } }
a sample call register form android app
mchathub.invoke("register", prefutils.my_user_id).get();
for js kind of same
chat.client.register = function () { chat.server.register(some_user_id); }
Comments
Post a Comment