As far as I can tell, the connection token is just an ID and the username. The ID is randomly generated. In early versions of SignalR, you could customize it by implementing the IConnectionIdFactory
interface, but that hasn't been possible since 2013.
Now, to answer the question "how is it generated", let's delve deep into SignalR's source. I am using ILSpy to search the source code. It's available for free online. You can see my ILSpy window here.
The interesting code is in Microsoft.AspNet.SignalR.Infrastructure.ConnectionManager
:
public IPersistentConnectionContext GetConnection(Type type)
{
if (type == null)
{
throw new ArgumentNullException("type");
}
string fullName = type.FullName;
string persistentConnectionName = PrefixHelper.GetPersistentConnectionName(fullName);
IConnection connectionCore = this.GetConnectionCore(persistentConnectionName);
return new PersistentConnectionContext(connectionCore, new GroupManager(connectionCore, PrefixHelper.GetPersistentConnectionGroupName(fullName)));
}
That leads us to:
internal Connection GetConnectionCore(string connectionName)
{
IList<string> signals = (connectionName == null) ? ListHelper<string>.Empty : new string[]
{
connectionName
};
string connectionId = Guid.NewGuid().ToString();
return new Connection(this._resolver.Resolve<IMessageBus>(), this._resolver.Resolve<IJsonSerializer>(), connectionName, connectionId, signals, ListHelper<string>.Empty, this._resolver.Resolve<ITraceManager>(), this._resolver.Resolve<IAckHandler>(), this._resolver.Resolve<IPerformanceCounterManager>(), this._resolver.Resolve<IProtectedData>());
}
So there you are. The connection id is simply a random Guid
, and the token is the ID plus the username.